Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a2e513e2b30ff87f8494e7ab152105d287da6305 (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
/*******************************************************************************
 * 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 (changed for C code generator)
 * 
 *******************************************************************************/

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

import com.google.inject.Inject
import com.google.inject.Singleton
import org.eclipse.etrice.core.genmodel.base.ILogger
import org.eclipse.etrice.core.genmodel.etricegen.Root
import org.eclipse.etrice.core.room.DataClass
import org.eclipse.etrice.generator.base.IGeneratorFileIo
import org.eclipse.etrice.generator.generic.ProcedureHelpers
import org.eclipse.etrice.generator.generic.RoomExtensions

import org.eclipse.etrice.core.room.util.RoomHelpers
import org.eclipse.etrice.core.room.RoomModel
import org.eclipse.etrice.core.room.Operation

@Singleton
class DataClassGen {

	@Inject IGeneratorFileIo fileIO
	@Inject extension RoomHelpers
	@Inject extension CExtensions
	@Inject extension RoomExtensions
	@Inject extension ProcedureHelpers
	@Inject ILogger logger
	
	def doGenerate(Root root) {
		for (dc: root.usedDataClasses) {
			val path = dc.generationTargetPath+dc.getPath
			val infopath = dc.generationInfoPath+dc.getPath
			var file = dc.getCHeaderFileName

			// header file
			fileIO.generateFile("generating DataClass header", path, infopath, file, root.generateHeaderFile(dc))
			
			// utils file
			file = dc.getCUtilsFileName
			fileIO.generateFile("generating ProtocolClass utils", path, infopath, file, root.generateUtilsFile(dc))
			
			// source file
			file = dc.getCSourceFileName
			fileIO.generateFile("generating DataClass header", path, infopath, file, root.generateSourceFile(dc))
			
		}
	}
	
	def generateHeaderFile(Root root, DataClass dc) {'''
		/**
		 * @author generated by eTrice
		 *
		 * Header File of DataClass «dc.name»
		 * 
		 */

		«generateIncludeGuardBegin(dc)»
		
		#include "etDatatypes.h"

«««		TODO: includes only for used DataClasses, also for other models
		«FOR dataClass : root.getReferencedDataClasses(dc)»
			#include «dataClass.includePath»
		«ENDFOR»
		«FOR enumClass : root.getReferencedEnumClasses(dc)»
			#include «enumClass.includePath»
		«ENDFOR»
		
		«dc.userCode(1)»
				
		typedef struct {
			«dc.allAttributes.attributes»
		} «dc.name»;
		
		«FOR a:dc.allAttributes»
			«IF a.defaultValueLiteral!=null»
				«logger.logInfo(dc.name+" "+a.name+": Attribute initialization not supported in C")»
			«ENDIF»
		«ENDFOR»
		
«««		TODO: do we need setters and getters for C and C++ ?
		
		«dc.operations.operationsDeclaration(dc.name)»
		
		/* deep copy */
		void «dc.name»_deepCopy(«dc.name»* source, «dc.name»* target);
		
		«dc.userCode(2)»
		
		«generateIncludeGuardEnd(dc)»
		
		'''
	}
	
	def private generateUtilsFile(Root root, DataClass dc) {
		val filename = (dc.eContainer as RoomModel).name.replaceAll("\\.","_")+"_"+dc.name+"_Utils"
	'''
		/**
		 * @author generated by eTrice
		 *
		 * Utils File of DataClass «dc.name»
		 * 
		 */
		
		«generateIncludeGuardBegin(filename)»
		
		#include «dc.includePath»
		
		/*
		 * access macros for operations and attributes
		*/
		
		/* operations */
		«FOR op : dc.allOperations»
			«val args = op.argList»
			#define «op.name»(«args») «dc.name»_«op.name»(self«IF !op.arguments.empty», «args»«ENDIF»)
		«ENDFOR»
		
		/* attributes */
		«FOR a : dc.allAttributes»
			#define «a.name» (self->«a.name»)
		«ENDFOR»
		
		«generateIncludeGuardEnd(filename)»
		
	'''
	}
	
	private def argList(Operation op) {
		'''«FOR a : op.arguments SEPARATOR ", "»«a.name»«ENDFOR»'''
	}
	
	def generateSourceFile(Root root, DataClass dc) {'''
		/**
		 * @author generated by eTrice
		 *
		 * Source File of DataClass «dc.name»
		 * 
		 */
		
		#include <string.h>

		#include "«dc.getCHeaderFileName»"
		#include "«dc.getCUtilsFileName»"
		
		«dc.userCode(3)»
		
«««		TODO: do we need setters and getters for C and C++ ?
		
		«dc.operations.operationsImplementation(dc.name)»
		
		void «dc.name»_deepCopy(«dc.name»* source, «dc.name»* target) {
			memcpy(target, source, sizeof(«dc.name»));
		}
		
		
	'''}
	
	
}

Back to the top