From e1d57d4d91df4bf3c2554c0392dde168ad34cdbc Mon Sep 17 00:00:00 2001 From: Valentin Noel Date: Wed, 4 Sep 2013 17:31:59 +0200 Subject: [PATCH] add modif for shared_ptr support in ExpressionParser class (and unit tests) issue #27 --- .../src/Tools/ExpressionParser.tcc | 28 ++++++++++++++ .../tests/tools/expressionParserTests.hpp | 38 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/libraries/basicElement/src/Tools/ExpressionParser.tcc b/libraries/basicElement/src/Tools/ExpressionParser.tcc index 961b092..1fea1b2 100644 --- a/libraries/basicElement/src/Tools/ExpressionParser.tcc +++ b/libraries/basicElement/src/Tools/ExpressionParser.tcc @@ -42,6 +42,34 @@ void ExpressionParser::setVariables( const std::map < std::string, NumberType >& } } +template< > +void ExpressionParser::setVariables( const std::map < std::string, std::shared_ptr< Element > >& elementMap ) +{ + for( std::pair< std::string, std::shared_ptr< Element > > elem : elementMap ) + { + std::ostringstream oss; + std::string value; + switch( elem.second->getNumberSubType() ) + { + case eNumberTypeUnknown : throw std::runtime_error( "Invalid element subtype!"); break; + case eNumberTypeInt8 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::int8 > >( elem.second )->toString(); break; + case eNumberTypeUInt8 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::uint8 > >( elem.second )->toString(); break; + case eNumberTypeInt16 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::int16 > >( elem.second )->toString(); break; + case eNumberTypeUInt16 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::uint16 > >( elem.second )->toString(); break; + case eNumberTypeInt32 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::int32 > >( elem.second )->toString(); break; + case eNumberTypeUInt32 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::uint32 > >( elem.second )->toString(); break; + case eNumberTypeInt64 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::int64 > >( elem.second )->toString(); break; + case eNumberTypeUInt64 : value = std::static_pointer_cast< be::number_element::Number< be::number_element::uint64 > >( elem.second )->toString(); break; + case eNumberTypeFloat : value = std::static_pointer_cast< be::number_element::Number< float > >( elem.second )->toString(); break; + case eNumberTypeDouble : value = std::static_pointer_cast< be::number_element::Number< double > >( elem.second )->toString(); break; + case eNumberTypeIeeeExtended : value = std::static_pointer_cast< be::number_element::Number< be::number_element::ieeeExtended > >( elem.second )->toString(); break; + } + oss << elem.first << " = " << value << std::endl; + _contextString += oss.str(); + } +} + + template< typename ResultType > ResultType ExpressionParser::getExpressionResult( const std::string& expression ) { diff --git a/libraries/basicElement/tests/tools/expressionParserTests.hpp b/libraries/basicElement/tests/tools/expressionParserTests.hpp index 33794ea..5921373 100644 --- a/libraries/basicElement/tests/tools/expressionParserTests.hpp +++ b/libraries/basicElement/tests/tools/expressionParserTests.hpp @@ -126,4 +126,42 @@ BOOST_AUTO_TEST_CASE( basic_element_expression_parser_get_expression_result ) } } + +BOOST_AUTO_TEST_CASE( basic_element_expression_parser_shared_ptr ) +{ + LOG_WARNING( ">>> basic_element_expression_parser_shared_ptr <<<" ); + { + std::shared_ptr< be::number_element::Number< int > > num1( new be::number_element::Number< int > ); + std::shared_ptr< be::number_element::Number< short > > num2( new be::number_element::Number< short > ); + std::shared_ptr< be::number_element::Number< float > > num3( new be::number_element::Number< float > ); + + char buffer1[] = { 0x00, 0x00, 0x00, 0x01 }; + char buffer2[] = { 0x00, 0x02 }; + char buffer3[] = { 0x3f, 0x7f, 0x00, 0x00 }; + + num1->setData( buffer1, 4 ); + num2->setData( buffer2, 2 ); + num3->setData( buffer3, 4 ); + + BOOST_CHECK_EQUAL( num1->toString(), "1" ); + BOOST_CHECK_EQUAL( num2->toString(), "2" ); + BOOST_CHECK_EQUAL( num3->toString(), "0.996094" ); + + std::map< std::string, std::shared_ptr< be::Element > > elementList; + elementList.insert( std::make_pair( "num1", num1 ) ); + elementList.insert( std::make_pair( "num2", num2 ) ); + elementList.insert( std::make_pair( "num3", num3 ) ); + + bep::ExpressionParser expParser = bep::ExpressionParser( elementList ); + BOOST_CHECK_EQUAL( expParser.getExpressionResult< int >( "num1" ), 1 ); + BOOST_CHECK_EQUAL( expParser.getExpressionResult< int >( "num2" ), 2 ); + BOOST_CHECK_EQUAL( expParser.getExpressionResult< short >( "num1 + num2" ), 3 ); + BOOST_CHECK_EQUAL( expParser.getExpressionResult< int >( "num2 - num1" ), 1 ); + BOOST_CHECK_EQUAL( expParser.getExpressionResult< float >( "num1 - num2" ), -1 ); + BOOST_CHECK_EQUAL( expParser.getExpressionResult< int >( "num1 * num2" ), 2 ); + BOOST_CHECK_CLOSE( expParser.getExpressionResult< float >( "num1 * num3" ), 0.996094, 0.001 ); + BOOST_CHECK_CLOSE( expParser.getExpressionResult< float >( "num3 / num2" ), 0.498047, 0.001 ); + } +} + BOOST_AUTO_TEST_SUITE_END()