Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 774ace9e6efdd34b66a47b90dcc1d9f4663eea64 (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
/*******************************************************************************
 * 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 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:
 * 		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.etricegen.Root
import org.eclipse.etrice.core.room.DataClass
import org.eclipse.etrice.core.room.Operation
import org.eclipse.etrice.core.room.RoomModel
import org.eclipse.etrice.core.room.util.RoomHelpers
import org.eclipse.etrice.generator.base.io.IGeneratorFileIO
import org.eclipse.etrice.generator.generic.ProcedureHelpers
import org.eclipse.etrice.generator.generic.RoomExtensions
import org.eclipse.etrice.generator.base.logging.ILogger

@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.dataClasses) {
			val path = dc.getPath
			var file = dc.getCHeaderFileName

			// header file
			fileIO.generateFile("generating DataClass header", path + file, root.generateHeaderFile(dc))
			
			// utils file
			file = dc.getCUtilsFileName
			fileIO.generateFile("generating ProtocolClass utils", path + file, root.generateUtilsFile(dc))
			
			// source file
			file = dc.getCSourceFileName
			fileIO.generateFile("generating DataClass source", path + 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"

		/* include all referenced room classes */
«««		TODO: includes only for used DataClasses, also for other models
		«FOR rc : (root.getReferencedDataClasses(dc) + root.getReferencedEnumClasses(dc)).sortBy[name]»
			#include «rc.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 params = op.operationParams»
			«val args = op.operationArgs»
			#define «op.name»(«params») «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 operationParams(Operation op) {
		op.arguments.map[
			switch it {
				case isVarargs: '...'
				default: name
			}
		].join(', ')
	}
	
	private def operationArgs(Operation op) {
		op.arguments.map[
			switch it {
				case isVarargs: '__VA_ARGS__'
				default: name
			}
		].join(', ')
	}
	
	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