From ea9e651b97ed951fa1679e082054fed259df8aa3 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Sun, 12 Feb 2017 16:45:38 +0100 Subject: [PATCH] add various callbacks insertion checks --- alib2common/src/core/castApi.hpp | 25 +++++++++++++++++++---- alib2common/src/core/multipleDispatch.hpp | 6 ++++-- alib2common/src/core/xmlApi.hpp | 10 +++++++-- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/alib2common/src/core/castApi.hpp b/alib2common/src/core/castApi.hpp index 2eb566e19c..3afa288d3c 100644 --- a/alib2common/src/core/castApi.hpp +++ b/alib2common/src/core/castApi.hpp @@ -53,14 +53,26 @@ struct castApi { public: template < class From > void add ( ) { - castFunctions.insert ( std::make_pair ( std::type_index ( typeid ( From ) ), [] ( const alib::ObjectBase & from ) { return alib::Object ( To ( ( const From & ) from ) ); } ) ); + bool res = castFunctions.insert ( std::make_pair ( std::type_index ( typeid ( From ) ), [] ( const alib::ObjectBase & from ) { return alib::Object ( To ( ( const From & ) from ) ); } ) ).second; + if ( ! res ) { + std::string fromName = std::type_name < From > ( ); + std::string toName = std::type_name < To > ( ); + + throw::exception::CommonException ( "Casting from " + fromName + " to " + toName + " already registered." ); + } } template < class From > void add ( To ( * castFunction )( const From & ) ) { - castFunctions.insert ( std::make_pair ( std::type_index ( typeid ( From ) ), [=] ( const alib::ObjectBase & from ) { + bool res = castFunctions.insert ( std::make_pair ( std::type_index ( typeid ( From ) ), [=] ( const alib::ObjectBase & from ) { return alib::Object ( ( ( To ( * )( const alib::ObjectBase & ) )castFunction )( from ) ); - } ) ); + } ) ).second; + if ( ! res ) { + std::string fromName = std::type_name < From > ( ); + std::string toName = std::type_name < To > ( ); + + throw::exception::CommonException ( "Casting from " + fromName + " to " + toName + " already registered." ); + } } template < class From > @@ -146,7 +158,12 @@ public: class CastPoolStringBinder { public: CastPoolStringBinder ( std::string tagName ) { - castFunctionsByString ( ).insert ( std::make_pair ( tagName, & getCastPool < To > ( ) ) ); + bool res = castFunctionsByString ( ).insert ( std::make_pair ( tagName, & getCastPool < To > ( ) ) ).second; + if ( ! res ) { + std::string toName = std::type_name < To > ( ); + + throw::exception::CommonException ( "String cast pool for " + toName + "already exists." ); + } } }; diff --git a/alib2common/src/core/multipleDispatch.hpp b/alib2common/src/core/multipleDispatch.hpp index aa7356ab6b..4c37b7ce94 100644 --- a/alib2common/src/core/multipleDispatch.hpp +++ b/alib2common/src/core/multipleDispatch.hpp @@ -51,7 +51,8 @@ public: } RegistratorWrapper ( RealReturnType ( * callback ) ( FrontStaticParamTypes ..., typename std::match_cv_ref < DispatchedParameterTypes, RealParameterTypeBases >::type && ..., BackStaticParamTypes ... ) ) : m_callback ( callback ) { - if ( !getInstance ( ).registeredFunctions.insert ( std::make_pair ( std::make_tuple ( std::type_index ( typeid ( typename std::match_cv_ref < DispatchedParameterTypes, RealParameterTypeBases >::type ) ) ... ), this ) ).second ) { + bool res = getInstance ( ).registeredFunctions.insert ( std::make_pair ( std::make_tuple ( std::type_index ( typeid ( typename std::match_cv_ref < DispatchedParameterTypes, RealParameterTypeBases >::type ) ) ... ), this ) ).second; + if ( ! res ) { std::stringstream ss; ( void ) initializer_list < int > { ( ss << std::type_name < typename std::match_cv_ref < DispatchedParameterTypes, RealParameterTypeBases >::type > ( ), 0 ) ... }; @@ -145,7 +146,8 @@ public: } RegistratorWrapper ( RealReturnType ( * callback ) ( RealParametersType &&, RealParametersType && ) ) : m_callback ( callback ) { - if ( !getInstance ( ).registeredFunctions.insert ( std::make_pair ( std::type_index ( typeid ( RealParametersType ) ), this ) ).second ) { + bool res = getInstance ( ).registeredFunctions.insert ( std::make_pair ( std::type_index ( typeid ( RealParametersType ) ), this ) ).second; + if ( ! res ) { std::string paramsType = std::type_name < RealParametersType > ( ); std::string classType = std::type_name < Algorithm > ( ); diff --git a/alib2common/src/core/xmlApi.hpp b/alib2common/src/core/xmlApi.hpp index 9348d0bb69..485869a41f 100644 --- a/alib2common/src/core/xmlApi.hpp +++ b/alib2common/src/core/xmlApi.hpp @@ -116,8 +116,14 @@ public: std::function < Type ( std::deque < sax::Token >::iterator & ) > parseFunction; public: - ParserRegister ( ) : parseFunction ( Type::parse ) { - parseFunctions ( ).insert ( std::make_pair ( Type::getXmlTagName(), this ) ); + ParserRegister( ) : parseFunction ( Type::parse ) { + bool res = parseFunctions ( ).insert ( std::make_pair ( Type::getXmlTagName(), this ) ).second; + if ( ! res ) { + std::string groupName = std::type_name < Group > ( ); + std::string typeName = std::type_name < Type > ( ); + + throw::exception::CommonException ( "Parse callback of " + typeName + " already registered in group " + groupName + "." ); + } } virtual Group parse ( std::deque < sax::Token >::iterator & input ) { -- GitLab