summaryrefslogtreecommitdiff
path: root/ccode/valaccodeswitchstatement.vala
blob: 2e63c1a55056ece376137631b8c5a45f90941d1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* valaccodeswitchstatement.vala
 *
 * Copyright (C) 2006-2007  Jürg Billeter
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Jürg Billeter <j@bitron.ch>
 */

using GLib;
using Gee;

/**
 * Represents a switch selection statement in the C code.
 */
public class Vala.CCodeSwitchStatement : CCodeStatement {
	/**
	 * The switch expression.
	 */
	public CCodeExpression! expression { get; set; }
	
	private Gee.List<CCodeCaseStatement> case_statements = new ArrayList<CCodeCaseStatement> ();
	private Gee.List<CCodeStatement> default_statements = new ArrayList<CCodeStatement> ();
	
	public CCodeSwitchStatement (construct CCodeExpression! expression) {
	}
	
	/**
	 * Adds the specified case statement to the list of switch sections.
	 *
	 * @param case_stmt a case statement
	 */
	public void add_case (CCodeCaseStatement! case_stmt) {
		case_statements.add (case_stmt);
	}

	/**
	 * Append the specified statement to the default clause.
	 *
	 * @param stmt a statement
	 */
	public void add_default_statement (CCodeStatement! stmt) {
		default_statements.add (stmt);
	}

	public override void write (CCodeWriter! writer) {
		writer.write_indent (line);
		writer.write_string ("switch (");
		expression.write (writer);
		writer.write_string (")");
		writer.write_begin_block ();
		
		foreach (CCodeCaseStatement case_stmt in case_statements) {
			case_stmt.write (writer);
		}

		if (default_statements.size > 0) {
			writer.write_indent ();
			writer.write_string ("default:");
			writer.write_newline ();

			foreach (CCodeStatement stmt in default_statements) {
				stmt.write (writer);
			}
		}

		writer.write_end_block ();
	}
}