1 /* valaccodeswitchstatement.vala
3 * Copyright (C) 2006-2007 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
20 * Jürg Billeter <j@bitron.ch>
27 * Represents a switch selection statement in the C code.
29 public class Vala
.CCodeSwitchStatement
: CCodeStatement
{
31 * The switch expression.
33 public CCodeExpression expression
{ get; set; }
35 private Gee
.List
<CCodeCaseStatement
> case_statements
= new ArrayList
<CCodeCaseStatement
> ();
36 private Gee
.List
<CCodeStatement
> default_statements
= new ArrayList
<CCodeStatement
> ();
38 public CCodeSwitchStatement (CCodeExpression expression
) {
39 this
.expression
= expression
;
43 * Adds the specified case statement to the list of switch sections.
45 * @param case_stmt a case statement
47 public void add_case (CCodeCaseStatement case_stmt
) {
48 case_statements
.add (case_stmt
);
52 * Append the specified statement to the default clause.
54 * @param stmt a statement
56 public void add_default_statement (CCodeStatement stmt
) {
57 default_statements
.add (stmt
);
60 public override void write (CCodeWriter writer
) {
61 writer
.write_indent (line
);
62 writer
.write_string ("switch (");
63 expression
.write (writer
);
64 writer
.write_string (")");
65 writer
.write_begin_block ();
67 foreach (CCodeCaseStatement case_stmt
in case_statements
) {
68 case_stmt
.write (writer
);
71 if (default_statements
.size
> 0) {
72 writer
.write_indent ();
73 writer
.write_string ("default:");
74 writer
.write_newline ();
76 foreach (CCodeStatement stmt
in default_statements
) {
81 writer
.write_end_block ();