Release 0.41.92
[vala-gnome.git] / ccode / valaccodestruct.vala
blob50af6a2bf0e479b9355dd4a06b0ad646445766e8
1 /* valaccodestruct.vala
3 * Copyright (C) 2006-2010 Jürg Billeter
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.
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.
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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Represents a struct declaration in the C code.
28 public class Vala.CCodeStruct : CCodeNode {
29 /**
30 * The struct name.
32 public string name { get; set; }
34 public bool is_empty { get { return declarations.size == 0; } }
36 private List<CCodeDeclaration> declarations = new ArrayList<CCodeDeclaration> ();
38 public CCodeStruct (string name) {
39 this.name = name;
42 /**
43 * Adds the specified declaration as member to this struct.
45 * @param decl a variable declaration
47 public void add_declaration (CCodeDeclaration decl) {
48 declarations.add (decl);
51 /**
52 * Adds a variable with the specified type and name to this struct.
54 * @param type_name field type
55 * @param name member name
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);
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);
72 writer.write_end_block ();
73 if (CCodeModifiers.DEPRECATED in modifiers) {
74 writer.write_string (" G_GNUC_DEPRECATED");
76 writer.write_string (";");
77 writer.write_newline ();
78 writer.write_newline ();