Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ba1eae7a758f1e90f9a330d7c5fbc11d11a577f8 (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
/*******************************************************************************
 * 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.room.DataClass
import org.eclipse.etrice.generator.base.ILogger
import org.eclipse.etrice.generator.etricegen.Root
import org.eclipse.xtext.generator.JavaIoFileSystemAccess
import org.eclipse.etrice.generator.extensions.RoomExtensions
import org.eclipse.etrice.generator.generic.ProcedureHelpers


@Singleton
class DataClassGen {

	@Inject extension JavaIoFileSystemAccess fileAccess
	@Inject extension CExtensions stdExt
	@Inject extension RoomExtensions roomExt
	@Inject extension ProcedureHelpers helpers
	@Inject ILogger logger
	
	def doGenerate(Root root) {
		for (dc: root.usedDataClasses) {
			var path = dc.generationTargetPath+dc.getPath

			// header file
			logger.logInfo("generating DataClass header '"+dc.getCHeaderFileName+"' in '"+path+"'")
			fileAccess.setOutputPath(path)
			fileAccess.generateFile(dc.getCHeaderFileName, root.generateHeaderFile(dc))
			
			// source file
			logger.logInfo("generating DataClass source '"+dc.getCSourceFileName+"' in '"+path+"'")
			fileAccess.setOutputPath(path)
			fileAccess.generateFile(dc.getCSourceFileName, root.generateSourceFile(dc))
			
		}
	}
	
	def generateHeaderFile(Root root, DataClass dc) {'''
		/**
		 * @author generated by eTrice
		 *
		 * Header File of DataClass dc.name
		 * 
		 */

		generateIncludeGuardBegin(dc.name)
		
		#include "etDatatypes.h"

		TODO: includes only for used DataClasses, also for other models
		FOR dataClass : root.getReferencedDataClasses(dc)
			#include "dataClass.name.h"
		ENDFOR
		
		helpers.userCode(dc.userCode1)
				
		typedef struct {
			helpers.attributes(dc.allAttributes)
		} dc.name
		
		TODO: do we need setters and getters for C and C++ ?
		
		helpers.operationsDeclaration(dc.operations, dc.name)
		
		/* deep copy */
		void dc.name_deepCopy(dc.name* source, dc.name* target);
		
		helpers.userCode(dc.userCode2)
		
		generateIncludeGuardEnd(dc.name)
		
		'''
	}
	
	def generateSourceFile(Root root, DataClass dc) {'''
		/**
		 * @author generated by eTrice
		 *
		 * Source File of DataClass dc.name
		 * 
		 */

		#include "dc.getCHeaderFileName"
		
		#include <string.h>
		
		helpers.userCode(dc.userCode3)
		
		TODO: do we need setters and getters for C and C++ ?
		
		helpers.operationsImplementation(dc.operations, dc.name)
		
		// deep copy
		void dc.name_deepCopy(dc.name* source, dc.name* target) {
			memcpy(target, source, sizeof(dc.name));
		}
		
		
	'''}
	
	
}

Back to the top