Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3d4ab1f914af73d1398e550edf3b105ede139926 (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
/*******************************************************************************
 * 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
import org.eclipse.etrice.core.room.VarDecl

import org.eclipse.etrice.generator.base.ILogger
import org.eclipse.emf.common.util.EList
import org.eclipse.etrice.core.room.RefableType



@Singleton
class ProcedureHelpers {

	@Inject extension ILanguageExtension languageExt
	@Inject extension TypeHelpers
	@Inject ILogger logger

	def userCode(DetailCode dc) {'''
		IF dc!=null
			/*--------------------- begin user code ---------------------*/
			FOR command : dc.commands	command
			ENDFOR
			/*--------------------- end user code ---------------------*/
		ENDIF
	'''
	}
	
	// Attributes 
	/* TODO: add ref type */
	def attributes(List<Attribute> attribs) {'''
		/*--------------------- attributes ---------------------*/
		FOR attribute : attribs
			IF attribute.size==0
				attribute.refType.type.typeName attribute.name;
			ELSE
				attribute.refType.type.typeName[] attribute.name;
			ENDIF 
		ENDFOR
	'''
	}

	def arrayInitializer(Attribute att) {
		var dflt = if (att.defaultValueLiteral!=null) att.defaultValueLiteral else att.refType.type.defaultValue

		if (dflt.startsWith("{")) {
			if (dflt.split(",").size!=att.size)
				logger.logInfo("WARNING: array size determined by initializer differs from attribute size ("+att.name+"["+att.size+"] <-> "+dflt+")")
				
			return dflt
		}

		var result = "{"
		var int i = 0
		while (i<att.size) {
			result = result + dflt
			i=i+1
			if (i<att.size)
				result = result + ", "
		}
		return result+"}"
	}
	
	def attributeInitialization(List<Attribute> attribs) {
		'''
			// initialize attributes
			FOR a : attribs
				IF a.defaultValueLiteral!=null
					IF a.size==0
						a.name = a.defaultValueLiteral;
					ELSEIF a.defaultValueLiteral.startsWith("{")
							a.name = new a.refType.type.typeName[] a.defaultValueLiteral;
					ELSE
						a.name = new a.refType.type.typeName[a.size];
						for (int i=0;i<a.size;i++){
							a.name[i] = a.defaultValueLiteral;
						}
					ENDIF
				ELSE
					IF a.size==0
						a.name = a.refType.type.defaultValue;
					ELSE
						a.name = new a.refType.type.typeName[a.size];
						for (int i=0;i<a.size;i++){
							a.name[i] = a.refType.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 : attribssetterHeader(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 setattribute.name.toFirstUpper() (languageExt.selfPointer(classname, true)attribute.refType.type.typeNameIF attribute.size!=0[]ENDIF attribute.name)'''
	}
	def private getterHeader(Attribute attribute, String classname){'''
		languageExt.accessLevelPublic()attribute.refType.type.typeNameIF attribute.size!=0[]ENDIF getattribute.name.toFirstUpper() (languageExt.selfPointer(classname, false))'''
	}
	
	def argList(List<Attribute> attributes) {
		'''FOR a : attributes SEPARATOR ", "a.refType.type.typeNameIF a.size>1[]ENDIF a.nameENDFOR'''
	}

	
	// Operations
	def operationsDeclaration(List<? extends Operation> operations, String classname) {'''
		/*--------------------- operations ---------------------*/
		FOR operation : operations
			operationSignature(operation, classname, true);
		ENDFOR
		'''
	}

	def operationsImplementation(List<? extends Operation> operations, String classname) {'''
		/*--------------------- operations ---------------------*/
		FOR operation : operations
			operationSignature(operation, classname, false) {
				FOR command : operation.detailCode.commands
					command
				ENDFOR
			}
		ENDFOR
		'''
	}
	
	
	def private operationSignature(Operation operation, String classname, boolean isDeclaration) {
		classOperationSignature(classname, operation.name, BuildArgumentList(operation.arguments).toString, dataTypeToString(operation.returntype), isDeclaration);
	}

	def private dataTypeToString(RefableType type) {
		return if (type==null)
			"void"
		else 
			type.type.typeName
	}

	/*
	 * builds comma separated argument list as string from EList<VarDecl> arguments
	 */
	def private BuildArgumentList(EList<VarDecl> arguments){
		'''FOR argument : arguments SEPARATOR ", "argument.refType.type.typeName argument.nameENDFOR'''
	}
	
	def private classOperationSignature(String classname, String operationname, String argumentList, String returnType, boolean isDeclaration){
		'''languageExt.accessLevelPublic()returnType memberInDeclaration(classname, operationname)(languageExt.selfPointer(classname, !argumentList.empty)argumentList)'''
	}
	
}

Back to the top