24ad09828f48caa0ce181261a2b3f9d1e41bf882
[roobuilder] / src / ccode / valaccodedefine.vala
1 /* valaccodedefine.vala
2  *
3  * Copyright (C) 2018  Dr. Michael 'Mickey' Lauer
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  *      Dr. Michael 'Mickey' Lauer <mickey@vanille-media.de>
21  *      Rico Tzschichholz <ricotz@ubuntu.com>
22  */
23
24 using GLib;
25
26 /**
27  * Represents a definition in the C code.
28  */
29 public class Vala.CCodeDefine : CCodeNode {
30         /**
31          * The name of this definition.
32          */
33         public string name { get; set; }
34
35         /**
36          * The value of this definition.
37          */
38         public string? value { get; set; }
39
40         /**
41          * The value expression of this definition.
42          */
43         public CCodeExpression? value_expression { get; set; }
44
45         public CCodeDefine (string name, string? value = null) {
46                 this.name = name;
47                 this.value = value;
48         }
49
50         public CCodeDefine.with_expression (string name, CCodeExpression expression) {
51                 this.name = name;
52                 this.value_expression = expression;
53         }
54
55         public override void write (CCodeWriter writer) {
56                 writer.write_indent ();
57                 writer.write_string ("#define ");
58                 writer.write_string (name);
59                 if (value != null) {
60                         writer.write_string (" ");
61                         writer.write_string (@value);
62                 } else if (value_expression != null) {
63                         writer.write_string (" ");
64                         value_expression.write_inner (writer);
65                 }
66                 writer.write_newline ();
67         }
68 }