From b3097029ce03e8258a19321e6f7cc9711d57e247 Mon Sep 17 00:00:00 2001 From: Stephen Roderick Date: Mon, 20 Sep 2010 20:40:49 -0400 Subject: [PATCH 2/2] plugins/rtalloc: Preliminary implementation of rtstring constructors --- plugins/rtalloc/RTallocToolkit.cpp | 37 ++++++++++++++++++++++++++++++++- plugins/rtalloc/tests/state_test.cpp | 14 ++++++++---- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/plugins/rtalloc/RTallocToolkit.cpp b/plugins/rtalloc/RTallocToolkit.cpp index 9d08560..d2d45ae 100644 --- a/plugins/rtalloc/RTallocToolkit.cpp +++ b/plugins/rtalloc/RTallocToolkit.cpp @@ -7,6 +7,35 @@ #include #include +OCL::String charptr_to_rtstring(char* value) +{ + return OCL::String(value); +} + +OCL::String constcharptr_to_rtstring(const char* value) +{ + return OCL::String(value); +} + +OCL::String stdstring_to_rtstring(const std::string& value) +{ + return OCL::String(value.c_str()); +} + +struct string_ctor + : public std::unary_function +{ + mutable boost::shared_ptr< OCL::String > ptr; + typedef const OCL::String& (Signature)( int ); + string_ctor() + : ptr( new OCL::String() ) {} + const OCL::String& operator()( int size ) const + { + ptr->resize( size ); + return *(ptr); + } +}; + namespace RTT { using namespace RTT; @@ -45,9 +74,13 @@ namespace RTT bool RTallocPlugin::loadConstructors() { - // no constructors + TypeInfoRepository::shared_ptr ti = TypeInfoRepository::Instance(); - // \todo constructors + // no automatic conversion at all! + ti->type("rtstring")->addConstructor( newConstructor( &charptr_to_rtstring, false ) ); + ti->type("rtstring")->addConstructor( newConstructor( &constcharptr_to_rtstring, false ) ); +// ti->type("rtstring")->addConstructor( newConstructor( &stdstring_to_rtstring, false ) ); +// ti->type("rtstring")->addConstructor( newConstructor( string_ctor() ) ); return true; } diff --git a/plugins/rtalloc/tests/state_test.cpp b/plugins/rtalloc/tests/state_test.cpp index 5cecbc4..159f084 100644 --- a/plugins/rtalloc/tests/state_test.cpp +++ b/plugins/rtalloc/tests/state_test.cpp @@ -64,11 +64,15 @@ BOOST_AUTO_TEST_CASE( testCreateRtstring ) { // test creation of rtstring from char* string prog = string("StateMachine X {\n") - + " initial state INIT {\n" - + " var rtstring s = rtstring(\"hello world\");\n" - + " transitions { state FINI };\n" - + " }\n" - + " final state FINI {\n" + + " initial state INIT {\n" + + " var rtstring s1\n" + + " var rtstring s2(12)\n" + + " var rtstring s3(\"hello world\")\n" + + " var rtstring s4 = rtstring(\"hello world\")\n" + + " transitions { select FINI }\n" + + " }\n" + + " final state FINI {\n" + + " }\n" + " }\n" + " RootMachine X x\n" // instantiate a non hierarchical SC ; -- 1.7.0.2