Skip to main content
aboutsummaryrefslogblamecommitdiffstats
blob: c23be3521de6f81a6ed9c0868c25771ef4f8a778 (plain) (tree)
1
2
3
4
5
6
7
8


                                                                                
                                                                       
                                                           


                                         







                                                                                 



                                                          
                                                            
                                                       
                                                    
 





                                
                                       
                                     

                                        
                                     

                                   
                                                                             
                                             


                                                        
                                                                                                                      


                                                    
                                                                                                                      
                 

         
                                                                       











                                                       
                                                 










                                                                                 
                

                                                                            



                                             






















                                                                                       
 
/*******************************************************************************
 * 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