浏览代码

Remove magic numbers from C Code via defines (#1390)

Rene Beckmann 8 年之前
父节点
当前提交
34828993e3

+ 8 - 2
plugins/org.yakindu.sct.generator.c/src/org/yakindu/sct/generator/c/Naming.xtend

@@ -16,8 +16,10 @@ import org.eclipse.emf.ecore.EObject
 import org.yakindu.base.types.Enumerator
 import org.yakindu.base.types.Event
 import org.yakindu.base.types.Operation
+import org.yakindu.base.types.Property
 import org.yakindu.sct.generator.core.types.ICodegenTypeSystemAccess
 import org.yakindu.sct.model.sexec.ExecutionFlow
+import org.yakindu.sct.model.sexec.ExecutionState
 import org.yakindu.sct.model.sexec.Step
 import org.yakindu.sct.model.sexec.TimeEvent
 import org.yakindu.sct.model.sexec.naming.INamingService
@@ -158,11 +160,15 @@ class Naming {
 	def lastStateID() {
 		separator + "last" + separator + "state"
 	}
+	
+	def stateVectorDefine(ExecutionState it) {
+		'''SCVI_«shortName»'''.toString.toUpperCase
+	}
 
 	def ExecutionFlow execution_flow(EObject element) {
 		var ret = element;
 
-		while (ret != null) {
+		while (ret !== null) {
 			if (ret instanceof ExecutionFlow) {
 				return ret as ExecutionFlow
 			} else {
@@ -244,7 +250,7 @@ class Naming {
 		if (isConst) '''«it.constantName»''' else '''«scHandle»->«scope.instance».«name.asEscapedIdentifier»'''
 	}
 
-	def dispatch access(org.yakindu.base.types.Property it) {
+	def dispatch access(Property it) {
 		'''«name.asEscapedIdentifier»'''
 	}
 

+ 9 - 0
plugins/org.yakindu.sct.generator.c/src/org/yakindu/sct/generator/c/StatemachineHeader.xtend

@@ -190,6 +190,9 @@ class StatemachineHeader implements IContentTemplate {
 			/*! Define dimension of the state configuration vector for history states. */
 		#define «type.toUpperCase»_MAX_HISTORY_STATES «historyVector.size»«ENDIF»
 		
+		/*! Define indices of states in the StateConfVector */
+		«stateConfVectorDefines»
+		
 		/*! 
 		 * Type definition of the data structure for the «type» state machine.
 		 * This data structure has to be allocated by the client code. 
@@ -205,6 +208,12 @@ class StatemachineHeader implements IContentTemplate {
 			«ENDFOR»			
 		} «type»;
 	'''
+	
+	def stateConfVectorDefines(ExecutionFlow it) '''
+		«FOR state : states»
+		#define «state.stateVectorDefine» «state.stateVector.offset»
+		«ENDFOR»
+	'''
 
 	def dispatch scopeFunctionPrototypes(StatechartScope it) '''
 		«FOR d : declarations»

+ 3 - 3
plugins/org.yakindu.sct.generator.c/src/org/yakindu/sct/generator/c/StatemachineSource.xtend

@@ -204,9 +204,9 @@ class StatemachineSource implements IContentTemplate {
 			{
 				«FOR s : states»
 				case «s.shortName» :
-					result = (sc_boolean) («IF s.leaf»«scHandle»->stateConfVector[«s.stateVector.offset»] == «s.shortName»
-					«ELSE»«scHandle»->stateConfVector[«s.stateVector.offset»] >= «s.shortName»
-						&& «scHandle»->stateConfVector[«s.stateVector.offset»] <= «s.subStates.last.shortName»«ENDIF»);
+					result = (sc_boolean) («IF s.leaf»«scHandle»->stateConfVector[«s.stateVectorDefine»] == «s.shortName»
+					«ELSE»«scHandle»->stateConfVector[«s.stateVectorDefine»] >= «s.shortName»
+						&& «scHandle»->stateConfVector[«s.stateVectorDefine»] <= «s.subStates.last.shortName»«ENDIF»);
 					break;
 				«ENDFOR»
 				default: