aboutsummaryrefslogtreecommitdiffstats
blob: 1baee4bce10522285c0660817650299cad74f528 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*******************************************************************************
 * 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)
 * 
 *******************************************************************************/

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

import com.google.inject.Inject
import com.google.inject.Singleton
import org.eclipse.etrice.core.room.ActorClass
import org.eclipse.etrice.core.genmodel.base.ILogger
import org.eclipse.etrice.core.genmodel.etricegen.ExpandedActorClass
import org.eclipse.etrice.core.genmodel.etricegen.Root
import org.eclipse.xtext.generator.JavaIoFileSystemAccess
import static extension org.eclipse.etrice.core.room.util.RoomHelpers.*

import org.eclipse.etrice.generator.base.AbstractGenerator
import org.eclipse.etrice.generator.generic.RoomExtensions
import org.eclipse.etrice.generator.generic.ProcedureHelpers
import org.eclipse.etrice.generator.generic.GenericActorClassGenerator
import java.util.ArrayList
import org.eclipse.xtend2.lib.StringConcatenation
import org.eclipse.etrice.core.room.Port
import org.eclipse.etrice.core.room.SAPRef
import org.eclipse.etrice.core.room.SPPRef

@Singleton
class ActorClassGen extends GenericActorClassGenerator {
	
	@Inject JavaIoFileSystemAccess fileAccess
	@Inject extension CppExtensions
	@Inject extension RoomExtensions
	
	@Inject extension ProcedureHelpers
//	@Inject extension StateMachineGen
	@Inject ILogger logger
	
	def doGenerate(Root root) {
		for (xpac: root.xpActorClasses) {
			var path = xpac.actorClass.generationTargetPath+xpac.actorClass.getPath

			logger.logInfo("generating ActorClass header '"+xpac.actorClass.getCppHeaderFileName+"' in '"+path+"'")
			fileAccess.setOutputPath(path)
			fileAccess.generateFile(xpac.actorClass.getCppHeaderFileName, root.generateHeaderFile(xpac, xpac.actorClass))

			logger.logInfo("generating ActorClass source '"+xpac.actorClass.getCppSourceFileName+"' in '"+path+"'")
			fileAccess.setOutputPath(path)
			fileAccess.generateFile(xpac.actorClass.getCppSourceFileName, root.generateSourceFile(xpac, xpac.actorClass))
			
		}
	}
	
	def generateHeaderFile(Root root, ExpandedActorClass xpac, ActorClass ac) {
		val ctor = ac.operations.filter(op|op.constructor).head
		val dtor = ac.operations.filter(op|op.destructor).head
		
	'''
			/**
		 * @author generated by eTrice
		 *
		 * Header File of ActorClass «ac.name»
		 * 
		 */

		«generateIncludeGuardBegin(ac.name)»
	
		#include "platforms/generic/etDatatypes.h"
		#include "common/messaging/IRTObject.h"
		#include "common/modelbase/PortBase.h"
		#include "common/modelbase/InterfaceItemBase.h"
		#include "common/modelbase/ActorClassBase.h"
		#include "common/modelbase/SubSystemClassBase.h"
		#include "common/messaging/Address.h"
		#include "common/messaging/IMessageReceiver.h"
		#include "common/debugging/DebuggingService.h"
		#include <string>
		#include <vector>
		
		«FOR model : root.getReferencedModels(ac)»
			#include "«model.name».h"
		«ENDFOR»
		
		«FOR pc : root.getReferencedProtocolClasses(ac)»
			#include "«pc.^package»/«pc.name».h"
		«ENDFOR»
		
		«ac.userCode(1)»
		
		
		class «ac.name» : public «IF ac.base!=null»«ac.base.name»«ELSE»etRuntime::ActorClassBase«ENDIF» {
		
			
			protected:
				//--------------------- ports
				«ac.endPorts.map(port | '''«port.portClassName» «port.name»;''').join()»
				//--------------------- saps
				«ac.strSAPs.map(sap | '''«sap.portClassName» «sap.name»;''').join()»
				//--------------------- services
				«ac.serviceImplementations.map(svc | '''«svc.portClassName» «svc.spp.name»;''').join()»
			
				//--------------------- interface item IDs
				«genInterfaceItemConstants(xpac, ac)»
					
				«ac.attributes.attributes»
				«ac.operationsImplementation»
		
			//--------------------- construction
			public:
				«ac.name»(etRuntime::IRTObject* parent, std::string name, std::vector<std::vector<etRuntime::Address>*> port_addr, 
																		  std::vector<std::vector<etRuntime::Address>*> peer_addr)
				:«ac.generateConstructorInitalizerList»
				{
					setClassName("«ac.name»");
				
					«ac.attributes.attributeInitialization(false)»
				
					«IF ctor!=null»
					{
						// user defined constructor body
						«AbstractGenerator::getInstance().getTranslatedCode(ctor.detailCode)»
					}
					«ENDIF»
				}
			«attributeSettersGettersImplementation(ac.attributes, ac.name)»
			//--------------------- port getters
			«FOR ep : ac.getEndPorts()»
				«ep.portClassName.getterImplementation(ep.name, ac.name)»
			«ENDFOR»
			«FOR sap : ac.strSAPs»
				«sap.portClassName.getterImplementation(sap.name, ac.name)»
			«ENDFOR»
			«FOR svc : ac.serviceImplementations»
				«svc.portClassName.getterImplementation(svc.spp.name, ac.name)»
			«ENDFOR»
		
			//--------------------- lifecycle functions
			public:
			void init(){
				initUser();
			}
		
			void start(){
				startUser();
			}
		
			«IF !ac.overridesStop()»
			void stop(){
				stopUser();
			}
			«ENDIF»
			
			void destroy(){
				«IF dtor!=null»
					
						// user defined destructor body
						«AbstractGenerator::getInstance().getTranslatedCode(dtor.detailCode)»
				«ENDIF»
			}
		
			«IF ac.hasNonEmptyStateMachine»
«««				«xpac.genStateMachine()»
			«ELSEIF !xpac.hasStateMachine()»
				//--------------------- no state machine
				public: 
				void receiveEvent(etRuntime::InterfaceItemBase ifitem, int evt, void* data) {
					handleSystemEvent(ifitem, evt, data);
				}
				
				void executeInitTransition(){
				}
			«ENDIF»

			«ac.userCode(2)»

		};
		
		
		«generateIncludeGuardEnd(ac.name)»
	'''
	}
	def generateConstructorInitalizerList(ActorClass ac) { 
		var initializerList = new ArrayList<CharSequence>();
		if (ac.base==null) {
			initializerList.add('''etRuntime::ActorClassBase(parent, name, (*port_addr[0])[0], (*peer_addr[0])[0])''')
		}
		else {
			initializerList.add('''«ac.base.name»(parent, name, port_addr, peer_addr)''')
		}
	    // own ports
		for ( ep : ac.getEndPorts() ) {
			initializerList.add('''«ep.name»(this, "«ep.name»", IFITEM_«ep.name», «IF ep.multiplicity==1»0, «ENDIF»port_addr[IFITEM_«ep.name»]«IF ep.multiplicity==1»[0]«ENDIF», peer_addr[IFITEM_«ep.name»]«IF ep.multiplicity==1»[0]«ENDIF»)''');
		}
		// own saps
		for ( sap : ac.strSAPs ) {
			initializerList.add('''«sap.name»(this, "«sap.name»", IFITEM_«sap.name», 0, port_addr[IFITEM_«sap.name»][0], peer_addr[IFITEM_«sap.name»][0])''');
		}
		// own service implementations
		for (svc : ac.serviceImplementations) {
			initializerList.add('''«svc.spp.name»(this, "«svc.spp.name»", IFITEM_«svc.spp.name», port_addr[IFITEM_«svc.spp.name»], peer_addr[IFITEM_«svc.spp.name»])''');
		}
		return initializerList.join(',\n')
	}

	
	def generateSourceFile(Root root, ExpandedActorClass xpac, ActorClass ac) {
		''''''
	}
	
	
}