Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c23be3521de6f81a6ed9c0868c25771ef4f8a778 (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
/*******************************************************************************
 * Copyright (c) 2013 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)
 * 
 *******************************************************************************/

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

import com.google.inject.Inject
import org.eclipse.etrice.generator.generic.RoomExtensions
import org.eclipse.etrice.core.genmodel.etricegen.Root
import org.eclipse.etrice.core.room.EnumerationType
import org.eclipse.etrice.generator.base.io.IGeneratorFileIO
import org.eclipse.etrice.generator.generic.TypeHelpers
import org.eclipse.etrice.core.room.util.RoomHelpers

/**
 * @author Henrik Rentz-Reichert
 *
 */
class EnumerationTypeGen {

	@Inject IGeneratorFileIO fileIO
	@Inject extension RoomHelpers
	@Inject extension CExtensions
	@Inject extension RoomExtensions
	@Inject extension TypeHelpers
	
	def doGenerate(Root root) {
		root.enumClasses.filter[!isDeprecatedGeneration].forEach[et |
			val path = et.getPath
			var file = et.getCHeaderFileName

			// header file
			fileIO.generateFile("generating Enumeration header", path + file, root.generateHeaderFile(et))

			// header file
			file = et.getCSourceFileName
			fileIO.generateFile("generating Enumeration source", path + file, root.generateSourceFile(et))
		]
	}
	
	private def generateHeaderFile(Root root, EnumerationType et) {
		'''
		/**
		 * @author generated by eTrice
		 *
		 * Header File of Enumeration «et.name»
		 * 
		 */

		«generateIncludeGuardBegin(et)»
		
		#include "etDatatypes.h"
		
		«IF et.getPrimitiveType()!==null»
			«FOR lit: et.literals»
				#define «et.name»_«lit.name» «lit.castedValue»
			«ENDFOR»
		«ELSE»
			typedef enum «et.name» {
				«FOR lit: et.literals SEPARATOR ","»
					«et.name»_«lit.name» = «lit.literalValue»
				«ENDFOR»
			}
			«et.name»;
		«ENDIF»
		
		const char* «et.name»_getLiteralName(«et.typeName» literal);
		
		«generateIncludeGuardEnd(et)»
		'''
	}
	
	private def generateSourceFile(Root root, EnumerationType et) {
		'''
		/**
		 * @author generated by eTrice
		 *
		 * Source File of ProtocolClass «et.name»
		 * 
		 */

		#include "«et.getCHeaderFileName»"
		
		const char* «et.name»_getLiteralName(«et.typeName» literal) {
			switch(literal) {
				«FOR lit: et.literals»
					case «et.name»_«lit.name»: return "«lit.name»";
				«ENDFOR»
			}
			return NULL;
		}
		
		'''
	}
	
}

Back to the top