diff --git a/alib2common/src/core/castApi.hpp b/alib2common/src/core/castApi.hpp deleted file mode 100644 index 9e5787d85caf3ac4cef96a5b1d4983d8a54e0bcf..0000000000000000000000000000000000000000 --- a/alib2common/src/core/castApi.hpp +++ /dev/null @@ -1,155 +0,0 @@ -/* - * CastApi.hpp - * - * Created on: Apr 1, 2013 - * Author: Jan Travnicek - */ - -#ifndef CAST_API_HPP_ -#define CAST_API_HPP_ - -#include <functional> -#include <map> -#include <string> -#include <type_traits> - -#include "../object/ObjectBase.h" -#include "../object/Object.h" -#include "../exception/CommonException.h" - -namespace alib { - -struct castApi { -private: - class CastPoolBase { - protected: - ext::map < ext::type_index, std::function < alib::Object ( const alib::ObjectBase & ) > > castFunctions; - - virtual std::string toType ( ) = 0; - - public: - virtual ~CastPoolBase ( ) { - } - - alib::Object cast ( const alib::ObjectBase & from ) { - ext::map < ext::type_index, std::function < alib::Object ( const alib::ObjectBase & ) > >::iterator res = castFunctions.find ( ext::type_index ( typeid ( from ) ) ); - - if ( res == castFunctions.end ( ) ) { - std::string fromType = ext::to_string ( typeid ( from ) ); - - throw exception::CommonException ( "Bad cast: From: " + fromType + " To: " + toType ( ) ); - } - - return res->second ( from ); - } - - bool castAvailable ( ext::type_index from ) { - return castFunctions.count ( from ); - } - - }; - - template < class To > - class CastPool : public CastPoolBase { - public: - template < class From > - void add ( std::function < To ( const From & ) > castFunction ) { - bool res = castFunctions.insert ( std::make_pair ( ext::type_index ( typeid ( From ) ), [=] ( const alib::ObjectBase & from ) { - return alib::Object ( castFunction ( static_cast < const From & > ( from ) ) ); - } ) ).second; - if ( ! res ) { - std::string fromName = ext::to_string < From > ( ); - std::string toName = ext::to_string < To > ( ); - - throw::exception::CommonException ( "Casting from " + fromName + " to " + toName + " already registered." ); - } - } - - template < class From > - void add ( ) { - auto castFunction = [] ( const From & from ) -> To { - return To ( from ); - }; - - add < From > ( castFunction ); - } - - To explicitCast ( const alib::ObjectBase & from ) { - alib::Object res = cast ( from ); - To & target = static_cast < To & > ( res.getData ( ) ); - return To ( std::move ( target ) ); - } - - template < class From > - bool test ( ) { - return castFunctions.count ( ext::type_index ( typeid ( From ) ) ); - } - - std::string toType ( ) { - return ext::to_string < To > ( ); - } - - }; - -private: - class CastPoolBaseMap : public ext::map < ext::type_index, CastPoolBase * > { - public: - ~CastPoolBaseMap ( ) { - for ( const std::pair < const ext::type_index, CastPoolBase * > & item : * this ) - delete item.second; - } - - }; - - // INFO: Function exist to handle static order of initialisation - static ext::map < ext::type_index, CastPoolBase * > & castFunctionsById ( ) { - static CastPoolBaseMap res; - - return res; - } - -public: - static CastPoolBase & getCastPool ( ext::type_index typeId ) { - ext::map < ext::type_index, CastPoolBase * >::iterator res = castFunctionsById ( ).find ( typeId ); - - if ( res == castFunctionsById ( ).end ( ) ) { - std::string toType = ext::to_string ( typeId ); - - throw exception::CommonException ( "Casting to type " + toType + " not available." ); - } else { - return * res->second; - } - } - - template < class To > - static CastPool < To > & getCastPool ( ) { - ext::map < ext::type_index, CastPoolBase * >::iterator res = castFunctionsById ( ).find ( ext::type_index ( typeid ( To ) ) ); - - if ( res == castFunctionsById ( ).end ( ) ) { - CastPool < To > * castPool = new CastPool < To > ( ); - castPool->template add < To > ( ); - return * ( CastPool < To > * )( castFunctionsById ( ).insert ( std::make_pair ( ext::type_index ( typeid ( To ) ), castPool ) ).first->second ); - } else { - return * ( CastPool < To > * )res->second; - } - } - - static bool castAvailable ( ext::type_index to, ext::type_index from ) { - ext::map < ext::type_index, CastPoolBase * >::iterator res = castFunctionsById ( ).find ( to ); - - if ( res == castFunctionsById ( ).end ( ) ) - return false; - - return res->second->castAvailable ( from ); - } - - template < class To, class From > - static bool castAvailable ( ) { - return castAvailable ( ext::type_index ( typeid ( To ) ), ext::type_index ( typeid ( From ) ) ); - } - -}; - -} /* namespace alib */ - -#endif /* CAST_API_HPP_ */ diff --git a/alib2common/src/registration/CastRegistration.hpp b/alib2common/src/registration/CastRegistration.hpp index c89b2feb2e7c7522781537c2027397974526675f..ff3edb17398b7da449385a02cbad968a344ef4b8 100644 --- a/alib2common/src/registration/CastRegistration.hpp +++ b/alib2common/src/registration/CastRegistration.hpp @@ -1,7 +1,6 @@ #ifndef _CAST_REGISTRATION_HPP_ #define _CAST_REGISTRATION_HPP_ -#include <core/castApi.hpp> #include <abstraction/CastRegistry.hpp> #include <registration/NormalizationRegistration.hpp> @@ -12,15 +11,11 @@ template < class To, class From > class CastRegister { public: CastRegister ( ) { - alib::castApi::getCastPool < To > ( ).template add < From > ( ); - registration::NormalizationRegister < To > ( ); abstraction::CastRegistry::registerCast < To, From > ( ); } CastRegister ( To ( * castFunction ) ( const From & ) ) { - alib::castApi::getCastPool < To > ( ).template add < From > ( castFunction ); - registration::NormalizationRegister < To > ( ); abstraction::CastRegistry::registerCastAlgorithm < To, From > ( castFunction ); }