Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3e1c142ac8cd685eab8a3dad6f7c1d10d51e2155 (plain) (blame)
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
83
84
85
86
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 		Peter Karlitschek
 *
 *******************************************************************************/

package org.eclipse.etrice.generator.cpp.gen

import com.google.inject.Inject
import com.google.inject.Singleton
import org.eclipse.etrice.core.genmodel.fsm.fsmgen.ExpandedModelComponent
import org.eclipse.etrice.generator.cpp.Main
import org.eclipse.etrice.generator.generic.GenericStateMachineGenerator
import org.eclipse.etrice.generator.generic.RoomExtensions

@Singleton
class StateMachineGen extends GenericStateMachineGenerator {

	@Inject extension RoomExtensions

	override genExtra(ExpandedModelComponent xpac, boolean generateImplementation) {
		val states = newArrayList
		var ac = xpac.modelComponent
		val clsName = xpac.modelComponent.componentName

//		it is crucial that we obey the order that is used for state IDs
//		that means we have to collect base classes first and each base class list with leaf states last
		while (ac!=null) {
			states.addAll(0, ac.allBaseStates.leafStatesLast)
			ac = ac.base
		}
		if(generateImplementation)
			'''
				«IF Main::settings.generateMSCInstrumentation || Main::settings.generateWithVerboseOutput»
					// state names
					const std::string «clsName»::s_stateStrings[] = {
						"<no state>",
						"<top>",
						«FOR state : states SEPARATOR ","»
							"«state.genStatePathName»"
						«ENDFOR»
					};
				«ENDIF»
				const int «clsName»::s_numberOfStates = «2+states.size»;

				void «clsName»::setState(int new_state) {
					«IF Main::settings.generateMSCInstrumentation»
						DebuggingService::getInstance().addActorState(*this, s_stateStrings[new_state]);
					«ENDIF»
					«IF Main::settings.generateWithVerboseOutput»
						if (s_stateStrings[new_state] != "Idle") {
							std::cout << getInstancePath() << " -> " << s_stateStrings[new_state] << std::endl;
						}
					«ENDIF»
					m_state = new_state;
				}
			'''
		else
			'''
				«IF Main::settings.generateMSCInstrumentation || Main::settings.generateWithVerboseOutput»
				static const std::string s_stateStrings[];
				«ENDIF»
				static const int s_numberOfStates;

				int history[«2+states.size»];

				void setState(int new_state);
			'''
	}

	override public stateType() {
		"etInt16"
	}

	override boolType() {
		"etBool"
	}

}

Back to the top