Skip to main content
summaryrefslogtreecommitdiffstats
blob: c09d28670f3408b7ef07d70bca2fd3931f003749 (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
/*******************************************************************************
 * 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)
 * 		Thomas Schuetz (refactoring, adapted for other target languages)
 * 
 *******************************************************************************/

package org.eclipse.etrice.generator.generic

import com.google.inject.Inject
import com.google.inject.Singleton
import java.util.List
import org.eclipse.etrice.core.room.Attribute
import org.eclipse.etrice.core.room.DetailCode
import org.eclipse.etrice.core.room.Operation

@Singleton
class ProcedureHelpers {

	@Inject extension ILanguageExtension languageExt
	@Inject extension TypeHelpers

	def UserCode(DetailCode dc) {'''
		«IF dc!=null»
			//--------------------- begin user code
			«FOR command : dc.commands»	«command»
			«ENDFOR»
			//--------------------- end user code
		«ENDIF»
	'''
	}
	
	// Attributes 
	def Attributes(List<Attribute> attribs) {'''
		//--------------------- attributes
		«FOR attribute : attribs»
			«IF attribute.size==0»
				«languageExt.accessLevelProtected()»«attribute.type.name» «attribute.name»«IF attribute.defaultValueLiteral==null» = «attribute.type.defaultValue»«ELSE» = «attribute.defaultValueLiteral»«ENDIF»;
			«ELSE»
				«languageExt.accessLevelProtected()»«attribute.type.name»[] «attribute.name»«IF attribute.defaultValueLiteral==null» = «attribute.type.defaultValue»[«attribute.size»];«ELSE» = «attribute.defaultValueLiteral»;«ENDIF»
			«ENDIF» 
		«ENDFOR»
	'''
	}

	def attributeInitialization(List<Attribute> attribs) {
		'''
			// initialize attributes
			«FOR a : attribs»
				«IF a.defaultValueLiteral!=null»
					«IF a.size==0»
						«a.name» = «a.defaultValueLiteral»;
					«ELSE»
						for (int i=0;i<«a.size»;i++){
							«a.name»[i] = «a.defaultValueLiteral»
						}
					«ENDIF»
				«ELSE»
					«IF a.size==0»
						«a.name» = «a.type.defaultValue»;
					«ELSE»
						for (int i=0;i<«a.size»;i++){
							«a.name»[i] = «a.type.defaultValue»
						}
					«ENDIF»
				«ENDIF»
			«ENDFOR»
		'''
	}
	
	// Attribute setters & getters
	def AttributeSettersGettersDeclaration(List<Attribute> attribs, String classname) {'''
		//--------------------- attribute setters and getters
		«FOR attribute : attribs»
			«SetterHeader(attribute, classname)»;
			«GetterHeader(attribute, classname)»;
		«ENDFOR»
	'''
	}
	
	def AttributeSettersGettersImplementation(List<Attribute> attribs, String classname) {'''
		//--------------------- attribute setters and getters
		«FOR attribute : attribs»«SetterHeader(attribute, classname)» {
			 «languageExt.memberAccess()»«attribute.name» = «attribute.name»;
		}
		«GetterHeader(attribute, classname)» {
			return «languageExt.memberAccess()»«attribute.name»;
		}«ENDFOR»
	'''
	}
	
	def private SetterHeader(Attribute attribute, String classname){'''
		«languageExt.accessLevelPublic()»void set«attribute.name.toFirstUpper()» («languageExt.selfPointer(classname, 1)»«attribute.type.name»«IF attribute.size!=0»[]«ENDIF» «attribute.name»)'''
	}
	def private GetterHeader(Attribute attribute, String classname){'''
		«languageExt.accessLevelPublic()»«attribute.type.name»«IF attribute.size!=0»[]«ENDIF» get«attribute.name.toFirstUpper()» («languageExt.selfPointer(classname, 0)»)'''
	}

	
	// Operations
	def OperationsDeclaration(List<Operation> operations, String classname) {'''
		//--------------------- operations
		«FOR operation : operations»«OperationHeader(operation, classname, true)»;
		«ENDFOR»
		'''
	}

	def OperationsImplementation(List<Operation> operations, String classname) {'''
		//--------------------- operations
		«FOR operation : operations»«OperationHeader(operation, classname, false)»{
		«FOR command : operation.detailCode.commands»	«command»
			«ENDFOR»
		}
		«ENDFOR»
		'''
	}
	
	def private OperationHeader(Operation operation, String classname, boolean isDeclaration) {'''
		«languageExt.accessLevelPublic()»«IF operation.returntype==null»void«ELSE»«operation.returntype.name»«ENDIF» «languageExt.operationScope(classname, isDeclaration)»«operation.name» («languageExt.selfPointer(classname, operation.arguments.size)»«FOR argument : operation.arguments SEPARATOR ", "»«argument.type.name» «argument.name»«ENDFOR»)'''
	}
	
}

Back to the top