Skip to content
Snippets Groups Projects
Commit a003d7c2 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

remove castApi

parent 90046db4
No related branches found
No related tags found
No related merge requests found
Pipeline #
/*
* 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_ */
#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 );
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment