Browse Source

Fixed errors in C++ operation call back interfaces.
Added testcases for operation callbacks for C++ generator.

markus.muehlbrandt@gmail.com 11 years ago
parent
commit
1d9f19d2f9

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

@@ -68,8 +68,8 @@ class Naming extends org.yakindu.sct.generator.c.Naming {
 	'''
 		«type.targetLanguageName» «name.asEscapedIdentifier»(«FOR parameter : parameters SEPARATOR ', '»«parameter.type.targetLanguageName» «parameter.identifier»«ENDFOR»)'''
 		
-	def protected OCB_InterfaceSetter(StatechartScope scope) '''
-		void set«scope.interfaceOCBName»(«scope.interfaceOCBName»* operationCallback)'''
+	def protected OCB_InterfaceSetterDeclaration(StatechartScope scope, boolean fqn) '''
+		void «IF fqn»«scope.flow.module»::«ENDIF»set«scope.interfaceOCBName»(«scope.interfaceOCBName»* operationCallback)'''
 	
 	def protected identifier(Parameter parameter) {
 		if (parameter.name.isKeyword) {

+ 14 - 2
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/StatemachineHeader.xtend

@@ -84,6 +84,9 @@ class StatemachineHeader extends Statemachine {
 				«functionPrototypes»
 		};
 		
+		«FOR s : it.scopes.filter(typeof(StatechartScope)) SEPARATOR StringConcatenation.DEFAULT_LINE_DELIMITER»
+			«s.createInlineOCB_Destructor»
+		«ENDFOR»
 		#endif /* «module().define»_H_ */
 	'''
 	
@@ -100,6 +103,13 @@ class StatemachineHeader extends Statemachine {
 		return interfaces;
 	}
 	
+	def protected createInlineOCB_Destructor(StatechartScope it) {
+		if (hasOperations) {
+			return '''inline «flow.module»::«interfaceOCBName»::~«interfaceOCBName»() {}'''
+		}
+		return ''''''
+	}
+	
 	def protected createPublicScope(Scope scope) {
 		switch scope {
 			InterfaceScope: scope.createPublicScope()
@@ -154,13 +164,15 @@ class StatemachineHeader extends Statemachine {
 			//! Inner class for «scope.simpleName» interface scope operation callbacks.
 			class «scope.interfaceOCBName» {
 				public:
+					virtual ~«scope.interfaceOCBName»() = 0;
+					
 					«FOR operation : scope.operations SEPARATOR StringConcatenation.DEFAULT_LINE_DELIMITER»
 						virtual «operation.signature» = 0;
 					«ENDFOR»
 			};
 			
 			/*! Set the working instance of the operation callback interface '«scope.interfaceOCBName»'. */
-			«scope.OCB_InterfaceSetter»;
+			«scope.OCB_InterfaceSetterDeclaration(false)»;
 		«ENDIF»
 		'''
 	}
@@ -184,7 +196,7 @@ class StatemachineHeader extends Statemachine {
 		
 		«FOR s : scopes.filter(typeof(StatechartScope))»
 			«s.interfaceName»* «s.instance»;
-			«IF s.hasOperations»«s.interfaceOCBName»* «s.OCB_Instance»«ENDIF»
+			«IF s.hasOperations»«s.interfaceOCBName»* «s.OCB_Instance»;«ENDIF»
 		«ENDFOR»
 	'''
 	

+ 2 - 1
plugins/org.yakindu.sct.generator.cpp/src/org/yakindu/sct/generator/cpp/StatemachineImplementation.xtend

@@ -74,6 +74,7 @@ class StatemachineImplementation {
 			
 			«FOR s : scopes.filter(typeof(StatechartScope))»
 			«s.instance» = new «s.interfaceName»();
+			«IF s.hasOperations»«s.OCB_Instance» = null;«ENDIF»
 		«ENDFOR»
 		«IF hasHistory»
 			
@@ -315,7 +316,7 @@ class StatemachineImplementation {
 				«ENDIF»
 			«ENDFOR»
 			«IF scope.hasOperations»
-				void «module»::«scope.OCB_InterfaceSetter» {
+				«scope.OCB_InterfaceSetterDeclaration(true)» {
 					«scope.OCB_Instance» = operationCallback;
 				}
 			«ENDIF»

+ 2 - 2
test-plugins/org.yakindu.sct.generator.cpp.test/all C++ generator tests.launch

@@ -14,7 +14,7 @@
 <booleanAttribute key="includeOptional" value="true"/>
 <stringAttribute key="location" value="${workspace_loc}/../junit-workspace"/>
 <listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
-<listEntry value="/org.yakindu.sct.generator.cpp.test/test-gen/org/yakindu/sct/generator/cpp/test/AllTestsTest.java"/>
+<listEntry value="/org.yakindu.sct.generator.cpp.test/src/org/yakindu/sct/generator/cpp/test/AllTestsTestCustom.java"/>
 </listAttribute>
 <listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
 <listEntry value="1"/>
@@ -28,7 +28,7 @@
 <stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/>
 <booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/>
 <stringAttribute key="org.eclipse.jdt.launching.JRE_CONTAINER" value="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
-<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.yakindu.sct.generator.cpp.test.AllTestsTest"/>
+<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.yakindu.sct.generator.cpp.test.AllTestsTestCustom"/>
 <stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl} -consoleLog"/>
 <stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="org.yakindu.sct.generator.cpp.test"/>
 <stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.pde.ui.workbenchClasspathProvider"/>

+ 8 - 0
test-plugins/org.yakindu.sct.generator.cpp.test/gtests/OperationsTest/Operations.sgen

@@ -0,0 +1,8 @@
+GeneratorModel for yakindu::cpp {
+	statechart Operations {
+		feature Outlet {
+			targetProject = "gtests"
+			targetFolder = "OperationsTest"
+		}
+	}
+}

+ 20 - 0
test-plugins/org.yakindu.sct.generator.cpp.test/gtests/OperationsTest/OperationsTest.cc

@@ -0,0 +1,20 @@
+/**
+* Copyright (c) 2014 committers of YAKINDU and others.
+* 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:
+*     committers of YAKINDU - initial API and implementation
+*/
+#include <string>
+#include "gtest/gtest.h"
+#include "Operations.h"
+
+TEST(StatemachineTest, OperationCallback) {
+	Operations* statechart = new Operations();
+	statechart->init();
+	statechart->enter();
+	delete statechart;
+}

+ 97 - 0
test-plugins/org.yakindu.sct.generator.cpp.test/gtests/OperationsTest/OperationsTestCustom.cc

@@ -0,0 +1,97 @@
+/**
+* Copyright (c) 2014 committers of YAKINDU and others.
+* 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:
+*     committers of YAKINDU - initial API and implementation
+*/
+#include <string>
+#include "gtest/gtest.h"
+#include "Operations.h"
+#include "Operations_OCB.h"
+
+bool internalOperation1Called;
+bool internalOperation2Called;
+sc_integer internalOp2Param;
+
+bool interfaceOperation1Called;
+bool interfaceOperation2Called;
+sc_integer interfaceOp2Param;
+
+bool unnamedInterfaceOperation1Called;
+bool unnamedInterfaceOperation2Called;
+sc_integer unnamedInterfaceOp2Param;
+
+TEST(StatemachineTest, OperationCallback) {
+	internalOperation1Called = false;
+	internalOperation2Called = false;
+	internalOp2Param = 0;
+
+	interfaceOperation1Called = false;
+	interfaceOperation2Called = false;
+	interfaceOp2Param = 0;
+
+	unnamedInterfaceOperation1Called = false;
+	unnamedInterfaceOperation2Called = false;
+	unnamedInterfaceOp2Param = 0;
+
+	Operations* statechart = new Operations();
+	statechart->setDefaultSCI_OCB(new DefaultCallBack());
+	statechart->setInternalSCI_OCB(new InternalCallBack());
+	statechart->setSCI_Interface1_OCB(new Interface1CallBack());
+
+	statechart->init();
+	statechart->enter();
+
+	statechart->runCycle();
+	EXPECT_TRUE(internalOperation1Called);
+	EXPECT_TRUE(internalOperation2Called);
+	EXPECT_TRUE(internalOp2Param == 4);
+
+	statechart->raise_ev();
+	statechart->runCycle();
+	EXPECT_TRUE(interfaceOperation1Called);
+	EXPECT_TRUE(interfaceOperation2Called);
+	EXPECT_TRUE(interfaceOp2Param == 4);
+
+	statechart->raise_ev();
+	statechart->runCycle();
+	EXPECT_TRUE(unnamedInterfaceOperation1Called);
+	EXPECT_TRUE(unnamedInterfaceOperation2Called);
+	EXPECT_TRUE(unnamedInterfaceOp2Param == 4);
+
+	delete statechart;
+}
+
+void InternalCallBack::internalOperation1() {
+	internalOperation1Called = true;
+}
+
+sc_boolean InternalCallBack::internalOperation2(sc_integer param1) {
+	internalOperation2Called = true;
+	internalOp2Param = param1;
+	return true;
+}
+
+void Interface1CallBack::interfaceOperation1() {
+	interfaceOperation1Called = true;
+}
+
+sc_boolean Interface1CallBack::interfaceOperation2(sc_integer param1) {
+	interfaceOperation2Called = true;
+	interfaceOp2Param = param1;
+	return true;
+}
+
+void DefaultCallBack::unnamedInterfaceOperation1() {
+	unnamedInterfaceOperation1Called = true;
+}
+
+sc_boolean DefaultCallBack::unnamedInterfaceOperation2(sc_integer param1) {
+	unnamedInterfaceOperation2Called = true;
+	unnamedInterfaceOp2Param = param1;
+	return true;
+}

+ 28 - 0
test-plugins/org.yakindu.sct.generator.cpp.test/gtests/OperationsTest/Operations_OCB.h

@@ -0,0 +1,28 @@
+#include "Operations.h"
+
+class InternalCallBack : public Operations::InternalSCI_OCB {
+
+	public:
+
+		void internalOperation1();
+
+		sc_boolean internalOperation2(sc_integer param1);
+};
+
+class Interface1CallBack : public Operations::SCI_Interface1_OCB {
+	public:
+
+		void interfaceOperation1();
+
+		sc_boolean interfaceOperation2(sc_integer param1);
+};
+
+class DefaultCallBack : public Operations::DefaultSCI_OCB {
+
+	public:
+
+		void unnamedInterfaceOperation1();
+
+		sc_boolean unnamedInterfaceOperation2(sc_integer param1);
+};
+

+ 13 - 1
test-plugins/org.yakindu.sct.generator.cpp.test/model/test.sgen

@@ -372,7 +372,19 @@ GeneratorModel for sctunit::cpp {
 			WrapToJUnit =  "true"
 		}			
 	}
-
+	
+	test Operations{
+		
+		feature Outlet{ 
+			targetProject = "org.yakindu.sct.generator.cpp.test" 
+			targetFolder = "test-gen"			
+		}		
+		
+		feature JUnitWrapper {
+			WrapToJUnit =  "true"
+		}			
+	}
+	
 	test OutEventLifeCycle{
 		
 		feature Outlet{ 

+ 1 - 0
test-plugins/org.yakindu.sct.generator.cpp.test/pom.xml

@@ -21,6 +21,7 @@
 				<configuration>
 					<includes>
 						<include>**/AllTestsTest.class</include>
+						<include>**/AllTestsTestCustom.class</include>
 					</includes> 
 					<useUIHarness>false</useUIHarness>
 					<useUIThread>false</useUIThread>

+ 19 - 0
test-plugins/org.yakindu.sct.generator.cpp.test/src/org/yakindu/sct/generator/cpp/test/AllTestsTestCustom.java

@@ -0,0 +1,19 @@
+/**
+ * Copyright (c) 2014 committers of YAKINDU and others.
+ * 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:
+ *     committers of YAKINDU - initial API and implementation
+ */
+package org.yakindu.sct.generator.cpp.test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+import org.junit.runners.Suite.SuiteClasses;
+
+@RunWith(Suite.class)
+@SuiteClasses({OperationsTestCustom.class})
+public class AllTestsTestCustom {
+}

+ 46 - 0
test-plugins/org.yakindu.sct.generator.cpp.test/src/org/yakindu/sct/generator/cpp/test/OperationsTestCustom.java

@@ -0,0 +1,46 @@
+/**
+ * Copyright (c) 2014 committers of YAKINDU and others.
+ * 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:
+ *     committers of YAKINDU - initial API and implementation
+ */
+package org.yakindu.sct.generator.cpp.test;
+
+import java.util.Collection;
+
+import org.junit.Before;
+import org.junit.runner.RunWith;
+import org.yakindu.sct.generator.c.gtest.GTest;
+import org.yakindu.sct.generator.c.gtest.GTestRunner;
+import org.yakindu.sct.generator.c.gtest.GTestHelper;
+
+@GTest(sourceFile = "gtests/OperationsTest/OperationsTestCustom.cc", program = "gtests/OperationsTest/Operations", model = "testmodels/SCTUnit/Operations.sct")
+@RunWith(GTestRunner.class)
+public class OperationsTestCustom {
+
+	protected final GTestHelper helper = new GTestHelper(this) {
+
+		@Override
+		protected void getTestDataFiles(Collection<String> files) {
+			super.getTestDataFiles(files);
+			files.add("gtests/OperationsTest/Operations_OCB.h");
+		}
+		
+		@Override
+		protected void getSourceFiles(Collection<String> files) {
+			super.getSourceFiles(files);
+			files.add(getFileName(getTestProgram()) + ".cpp");
+		}
+		
+	};
+
+	@Before
+	public void setUp() {
+		helper.generate();
+		helper.compile();
+	}
+}

+ 3 - 2
test-plugins/org.yakindu.sct.generator.cpp.test/test-gen/org/yakindu/sct/generator/cpp/test/AllTestsTest.java

@@ -26,8 +26,9 @@ import org.junit.runners.Suite.SuiteClasses;
 		InEventLifeCycleTest.class, IntegerExpressionsTest.class,
 		InternalEventLifeCycleTest.class, LocalReactionsTest.class,
 		LogicalAndTestsTest.class, LogicalOrTestsTest.class,
-		NamedInterfaceAccessTest.class, OutEventLifeCycleTest.class,
-		ParenthesisTest.class, PriorityValuesTest.class, RaiseEventTest.class,
+		NamedInterfaceAccessTest.class, OperationsTest.class,
+		OutEventLifeCycleTest.class, ParenthesisTest.class,
+		PriorityValuesTest.class, RaiseEventTest.class,
 		SameNameDifferentRegionTest.class, ShallowHistoryTest.class,
 		SimpleEventTest.class, StatechartLocalReactionsTest.class,
 		SimpleHierachyTest.class, StateIsActiveTest.class,

+ 39 - 0
test-plugins/org.yakindu.sct.generator.cpp.test/test-gen/org/yakindu/sct/generator/cpp/test/OperationsTest.java

@@ -0,0 +1,39 @@
+/**
+ * Copyright (c) 2014 committers of YAKINDU and others.
+ * 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:
+ *     committers of YAKINDU - initial API and implementation
+ */
+package org.yakindu.sct.generator.cpp.test;
+
+import java.util.Collection;
+
+import org.junit.Before;
+import org.junit.runner.RunWith;
+import org.yakindu.sct.generator.c.gtest.GTest;
+import org.yakindu.sct.generator.c.gtest.GTestRunner;
+import org.yakindu.sct.generator.c.gtest.GTestHelper;
+
+@GTest(sourceFile = "gtests/OperationsTest/OperationsTest.cc", program = "gtests/OperationsTest/Operations", model = "testmodels/SCTUnit/Operations.sct")
+@RunWith(GTestRunner.class)
+public class OperationsTest {
+
+	protected final GTestHelper helper = new GTestHelper(this) {
+
+		@Override
+		protected void getSourceFiles(Collection<String> files) {
+			super.getSourceFiles(files);
+			files.add(getFileName(getTestProgram()) + ".cpp");
+		}
+	};
+
+	@Before
+	public void setUp() {
+		helper.generate();
+		helper.compile();
+	}
+}