Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ebb1976f612201714efa88fec4a4efaed639ada2 (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
/*******************************************************************************
 * 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 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * CONTRIBUTORS:
 * 		Juergen Haug (initial contribution)
 * 
 *******************************************************************************/


package org.eclipse.etrice.generator.c.gen

import com.google.inject.Inject
import java.util.ArrayList
import java.util.List
import org.eclipse.etrice.core.genmodel.etricegen.ActorInstance
import org.eclipse.etrice.core.genmodel.etricegen.InstanceBase
import org.eclipse.etrice.core.genmodel.etricegen.InterfaceItemInstance
import org.eclipse.etrice.core.room.Attribute
import org.eclipse.etrice.core.room.DataClass
import org.eclipse.etrice.core.room.ExternalType
import org.eclipse.etrice.core.room.PrimitiveType
import org.eclipse.etrice.core.room.util.RoomHelpers
import org.eclipse.etrice.generator.generic.RoomExtensions
import org.eclipse.etrice.generator.generic.TypeHelpers
import org.eclipse.etrice.core.room.EnumerationType

class Initialization {
	
	@Inject CExtensions languageExt
	@Inject extension RoomHelpers
	@Inject extension RoomExtensions
	@Inject extension TypeHelpers
	
	def generateAttributeInit(InstanceBase instance, List<Attribute> attributes){'''
		«FOR a : attributes SEPARATOR ','»
			«initAttributeArray(instance, new ArrayList<Attribute>.union(a))»
		«ENDFOR»
		'''		
	}
	
	def private String initAttributeArray(InstanceBase instance, List<Attribute> path){
		var a = path.last
		var COMMENT = '''		/* «a.name»«IF a.size>1»[«a.size»]«ENDIF» */'''.toString
		if(a.size == 0 || (!a.type.ref && a.type.type.enumerationOrPrimitive))
			initAttribute(instance, path)+COMMENT
		else 
		'''
			{ «FOR Integer i:1..a.size SEPARATOR ', '»«initAttribute(instance, path)»«ENDFOR» } «COMMENT»
		'''
	}
	
	def private initAttribute(InstanceBase instance, List<Attribute> path) {
		var a = path.last
		var aType = a.type.type
		if(a.type.ref){
			return if(a.defaultValueLiteral !== null)
				a.defaultValueLiteral
			else
				languageExt.nullPointer
		}
		switch aType{
			DataClass:
				'''
					{
						«FOR subA : (aType as DataClass).allAttributes SEPARATOR ','»
							«initAttributeArray(instance, path.union(subA))»
						«ENDFOR»
					}'''
			ExternalType:
				if(a.defaultValueLiteral !== null)
					a.defaultValueLiteral
				else
					languageExt.defaultValue(aType)
			PrimitiveType: {
				var value = getPrimitiveValue(instance, path)
				if(a.size > 0 && !aType.characterType && !value.trim.startsWith('{'))
					'''{«FOR Integer i:1..a.size SEPARATOR ', '»«value»«ENDFOR»}'''
				else if (a.size > 0 && aType.characterType && value.startsWith("'"))
					"{"+value+"}"
				else
					value
			}
			EnumerationType: {
				var value = getPrimitiveValue(instance, path)
				if (a.size > 0 && !value.trim.startsWith('{'))
					'''{«FOR Integer i:1..a.size SEPARATOR ', '»«value»«ENDFOR»}'''
				else
					value
			}
		}
	}
	
	def private getPrimitiveValue(InstanceBase instance, List<Attribute> path){
		var value = path.getAttrInstanceConfigValue(instance)
		if(value === null)
			value = switch instance {
				ActorInstance: path.getAttrClassConfigValue(instance.actorClass, true)
				InterfaceItemInstance: path.getAttrClassConfigValue(getPortClass(instance.interfaceItem))
			}
		if(value === null)
			value = path.last.defaultValueLiteral
		
		var type = path.last.type.type
		return if(value !== null && type.primitive) languageExt.toValueLiteral(type as PrimitiveType, value)
				else if(value !== null && type.enumeration) languageExt.toEnumLiteral(type as EnumerationType, value)
				else languageExt.defaultValue(type)
	}
}

Back to the top