Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 17af4439ef01792fe751806b7947f6d344d81156 (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
/*******************************************************************************
 * Copyright (c) 2012 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.java.gen

import com.google.inject.Inject
import com.google.inject.Singleton
import java.util.ArrayList
import java.util.List
import org.eclipse.emf.ecore.EObject
import org.eclipse.etrice.core.room.ActorClass
import org.eclipse.etrice.core.room.Attribute
import org.eclipse.etrice.core.room.ComplexType
import org.eclipse.etrice.core.room.DataClass
import org.eclipse.etrice.core.room.PortClass
import org.eclipse.etrice.core.room.PrimitiveType
import org.eclipse.etrice.core.room.ProtocolClass
import org.eclipse.etrice.generator.base.IDataConfiguration
import org.eclipse.etrice.generator.generic.ILanguageExtension
import org.eclipse.etrice.generator.generic.ProcedureHelpers
import org.eclipse.etrice.generator.generic.RoomExtensions
import org.eclipse.etrice.generator.generic.TypeHelpers

@Singleton
class Initialization {

	@Inject extension TypeHelpers
	@Inject extension RoomExtensions
	@Inject ILanguageExtension languageExt
	@Inject IDataConfiguration dataConfigExt
	@Inject ProcedureHelpers procedureHelpers
	
	def attributeInitialization(List<Attribute> attribs, EObject roomClass, boolean useClassDefaultsOnly) {	
		var tmp = ''''''
		'''
			// initialize attributes
			«FOR a : attribs»
				«tmp = dataConfigurationInit(roomClass, new ArrayList<Attribute>.union(a))»
				«IF tmp.length==0»«(tmp = valueInit(new ArrayList<Attribute>.union(a), getRoomDefaulValue(a)))»«ENDIF»
				«IF tmp.length==0»«defaultInit(a, useClassDefaultsOnly)»«ENDIF»
			«ENDFOR»
		'''
	}
	
	def private dataConfigurationInit(EObject roomClass, List<Attribute> path){
		var a = path.last
		var aType = a.refType.type
		if(aType.dataClass){
			return '''
				«FOR e : (aType as DataClass).attributes»
					«dataConfigurationInit(roomClass, path.union(e))»
				«ENDFOR»
			'''
		}
		else if(aType.primitive)
		 	return valueInit(path, getDataConfigValue(roomClass, path))
		''''''
	}
	
	def private valueInit(List<Attribute> path, String value){
		if(value == null) return ''''''
		var a = path.last
		var aType = a.refType.type
		var getter = if(path.size > 1)procedureHelpers.invokeGetters(path.take(path.size-1), null)+"." else ""
		'''
			«IF a.size == 0 || aType.characterType»
					«getter»«procedureHelpers.invokeSetter(a.name,null,value)»;
				«ELSEIF value.startsWith("{")»
					«getter»«procedureHelpers.invokeSetter(a.name,null, '''new «aType.typeName»[] «value»''')»;
				«ELSE»
					{
						«aType.typeName»[] _«a.name» = new «aType.typeName»[«a.size»];
						for (int i=0;i<«a.size»;i++){
							_«a.name»[i] = «value»;
						}
						«getter»«procedureHelpers.invokeSetter(a.name,null,"_"+a.name)»;
					}
			«ENDIF»
		'''
	}
	
	def private defaultInit(Attribute a, boolean useClassDefaultsOnly){
		'''
			«IF a.refType.type instanceof ComplexType || a.size>1 || !useClassDefaultsOnly»
				«IF a.size==0»
					«IF a.refType.isRef»
						«a.name» = «languageExt.nullPointer()»;
					«ELSE»
						«a.name» = «languageExt.defaultValue(a.refType.type)»;
					«ENDIF»
				«ELSE»
					«a.name» = new «a.refType.type.typeName»[«a.size»];
					«IF !useClassDefaultsOnly»
						for (int i=0;i<«a.size»;i++){
							«a.name»[i] = «IF a.refType.isRef»«languageExt.nullPointer()»«ELSE»«languageExt.defaultValue(a.refType.type)»«ENDIF»;
						}
					«ENDIF»
				«ENDIF»
			«ENDIF»
		'''
	}
	
	def private getRoomDefaulValue(Attribute a){
		a.defaultValueLiteral
	}
	
	def private String getDataConfigValue(EObject roomClass, List<Attribute> path){
		var a = path.last
		if(a.refType.type.primitive){
			var aType = a.refType.type as PrimitiveType
			var result = switch roomClass {
				ActorClass: dataConfigExt.getAttrClassConfigValue(roomClass, path)
				PortClass: 
					if(roomClass.eContainer instanceof ProtocolClass){
						var pc = roomClass.eContainer as ProtocolClass
						dataConfigExt.getAttrClassConfigValue(pc, pc.regular.equals(roomClass), path)
					}
			}
			return if(result != null)
					if(a.size > 0 && !aType.characterType)
						'''{ «FOR e : result.split(',') SEPARATOR ", "»«languageExt.toValueLiteral(aType, e)»«ENDFOR»}'''
					else 
						languageExt.toValueLiteral(aType, result)
		}
	}
	
}

Back to the top