44c3fe7f729bb21e19bd89e6ad8fe6629cfc010a
[roobuilder] / src / ccode / valaccodeifsection.vala
1 /* valaccodeifsection.vala
2  *
3  * Copyright (C) 2013  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  *      Marc-André Lurau <marcandre.lureau@redhat.com>
21  */
22
23 using GLib;
24
25 /**
26  * Represents a section that should be processed on condition.
27  */
28 public class Vala.CCodeIfSection : CCodeFragment {
29         /**
30          * The conditional expression, or null if there is no condition.
31          */
32         public string? expression { get; set; }
33
34         CCodeIfSection? else_section;
35         bool is_else_section;
36
37         public CCodeIfSection (string? expr) {
38                 expression = expr;
39                 is_else_section = false;
40         }
41
42         public unowned CCodeIfSection append_else (string? expr = null) {
43                 else_section = new CCodeIfSection (expr);
44                 else_section.is_else_section = true;
45                 return else_section;
46         }
47
48         public override void write (CCodeWriter writer) {
49                 if (is_else_section) {
50                         if (expression != null) {
51                                 writer.write_string ("#elif ");
52                                 writer.write_string (expression);
53                         } else {
54                                 writer.write_string ("#else");
55                         }
56                 } else if (expression != null) {
57                         writer.write_string ("#if ");
58                         writer.write_string (expression);
59                 }
60                 writer.write_newline ();
61                 foreach (CCodeNode node in get_children ()) {
62                         node.write_combined (writer);
63                 }
64                 if (else_section != null) {
65                         else_section.write_combined (writer);
66                 } else {
67                         writer.write_string ("#endif");
68                         writer.write_newline ();
69                 }
70         }
71
72         public override void write_declaration (CCodeWriter writer) {
73         }
74
75         public override void write_combined (CCodeWriter writer) {
76                 write (writer);
77         }
78 }