9630a37c4468dad1a71a12c96c77d113f1eb80aa
[roobuilder] / src / ccode / valaccodestruct.vala
1 /* valaccodestruct.vala
2  *
3  * Copyright (C) 2006-2010  Jürg Billeter
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
18  *
19  * Author:
20  *      Jürg Billeter <j@bitron.ch>
21  */
22
23 using GLib;
24
25 /**
26  * Represents a struct declaration in the C code.
27  */
28 public class Vala.CCodeStruct : CCodeNode {
29         /**
30          * The struct name.
31          */
32         public string name { get; set; }
33
34         public bool is_empty { get { return declarations.size == 0; } }
35
36         private List<CCodeDeclaration> declarations = new ArrayList<CCodeDeclaration> ();
37
38         public CCodeStruct (string name) {
39                 this.name = name;
40         }
41
42         /**
43          * Adds the specified declaration as member to this struct.
44          *
45          * @param decl a variable declaration
46          */
47         public void add_declaration (CCodeDeclaration decl) {
48                 declarations.add (decl);
49         }
50
51         /**
52          * Adds a variable with the specified type and name to this struct.
53          *
54          * @param type_name field type
55          * @param name      member name
56          */
57         public void add_field (string type_name, string name, CCodeModifiers modifiers = 0, CCodeDeclaratorSuffix? declarator_suffix = null) {
58                 var decl = new CCodeDeclaration (type_name);
59                 decl.add_declarator (new CCodeVariableDeclarator (name, null, declarator_suffix));
60                 decl.modifiers = modifiers;
61                 add_declaration (decl);
62         }
63
64         public override void write (CCodeWriter writer) {
65                 writer.write_string ("struct ");
66                 writer.write_string (name);
67                 writer.write_begin_block ();
68                 foreach (CCodeDeclaration decl in declarations) {
69                         decl.write_declaration (writer);
70                 }
71
72                 writer.write_end_block ();
73                 if (CCodeModifiers.DEPRECATED in modifiers) {
74                         writer.write_string (GNUC_DEPRECATED);
75                 }
76                 writer.write_string (";");
77                 writer.write_newline ();
78                 writer.write_newline ();
79         }
80 }