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

make casting throw streamable exceptions

parent 377c5a48
No related branches found
No related tags found
No related merge requests found
......@@ -11,9 +11,11 @@
#include <functional>
#include <map>
#include <string>
#include <type_traits>
 
#include "../object/ObjectBase.h"
#include "../object/Object.h"
#include "../exception/CommonException.h"
 
namespace alib {
 
......@@ -22,12 +24,22 @@ struct castApi {
protected:
std::map < std::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 ) {
std::map < std::type_index, std::function < alib::Object ( const alib::ObjectBase & ) > >::iterator res = castFunctions.find ( std::type_index ( typeid ( from ) ) );
 
if ( res == castFunctions.end ( ) )
throw std::bad_cast ( );
if ( res == castFunctions.end ( ) ) {
char* name = std::type_name(typeid(from));
std::string fromType(name);
std::free(name);
throw exception::CommonException ( "Bad cast: From: " + fromType + " To: " + toType());
}
 
return res->second ( from );
}
......@@ -59,6 +71,13 @@ struct castApi {
return castFunctions.count ( std::type_index ( typeid ( From ) ) );
}
 
std::string toType ( ) {
char* name = std::type_name<To>();
std::string toType(name);
std::free(name);
return toType;
}
};
 
private:
......@@ -103,7 +122,7 @@ public:
std::map < std::string, CastPoolBase * >::iterator res = castFunctionsByString ( ).find ( tagName );
 
if ( res == castFunctionsByString ( ).end ( ) )
throw std::invalid_argument ( "Casting to type " + tagName + " not available." );
throw exception::CommonException ( "Casting to type " + tagName + " not available." );
else
return * res->second;
}
......@@ -111,9 +130,13 @@ public:
static CastPoolBase & getCastPool ( std::type_index typeId ) {
std::map < std::type_index, CastPoolBase * >::iterator res = castFunctionsById ( ).find ( typeId );
 
if ( res == castFunctionsById ( ).end ( ) )
throw std::invalid_argument ( "Casting to type ? not available." );
else
if ( res == castFunctionsById ( ).end ( ) ) {
char* name = std::type_name( typeId );
std::string toType(name);
std::free(name);
throw exception::CommonException ( "Casting to type " + toType + " not available." );
} else
return * res->second;
}
 
......
/*
* istream.cpp
*
* Created on: Apr 14, 2016
* Author: Jan Travnicek
*/
#include "../type_traits"
namespace std {
char* type_name(const std::type_info& type) {
int status;
return abi::__cxa_demangle(type.name(), 0, 0, &status);
}
char* type_name(const std::type_index& type) {
int status;
return abi::__cxa_demangle(type.name(), 0, 0, &status);
}
}
......@@ -56,6 +56,9 @@ namespace std {
return abi::__cxa_demangle(typeid(T).name(), 0, 0, &status);
}
 
char* type_name(const std::type_info& type);
char* type_name(const std::type_index& type);
} /* namespace std */
 
#endif // TYPE_TRAITS_HPP_
......@@ -6,6 +6,7 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <typeindex>
#include "extensions/type_traits.hpp"
 
#endif /* __TYPE_TRAITS_HEADER_WRAPPER_ */
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