/*========================================================================= Program: crea Module: $RCSfile: creaRTTI.h,v $ Language: C++ Date: $Date: 2008/09/26 14:09:55 $ Version: $Revision: 1.1 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See doc/license.txt or http://www.creatis.insa-lyon.fr/Public/crea/License.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ /** *\file *\brief RTTI tools (system dependent). */ #ifndef __CREARTTI_H_INCLUDED__ #define __CREARTTI_H_INCLUDED__ #include "creaSystem.h" #include //----------------------------------------------------------------------------- // RRTI type_info.name() demangling // For type_info.name() demangling (gcc >= 3.1, see http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaceabi.html) // Test for GCC > 3.1.0 // #if __GNUC__ > 3 || \ (__GNUC__ == 3 && (__GNUC_MINOR__ > 1 || \ (__GNUC_MINOR__ == 1 && \ __GNUC_PATCHLEVEL__ > 0))) #include #include namespace crea { inline std::string demangle_type_name(const char* name) { int status; char* dem = abi::__cxa_demangle(name, 0, 0, &status); std::string demangled(dem); free(dem); if (!status) return demangled; return name; } } #define CREA_DEMANGLE_TYPE_NAME(NAME) crea::demangle_type_name(NAME) //========================================================================== #elif defined(_WIN32) // WIN32 //#include "Windows.h" //#include "stdafx.h" #include #include // include the right library in the linker stage #pragma comment( lib, "imagehlp.lib" ) namespace crea { inline std::string demangle_type_name(const char* name) { char demangled[513]; if (UnDecorateSymbolName(name, demangled, sizeof(name), UNDNAME_COMPLETE)) { return name; //demangled; } else { return name; } } } #define CREA_DEMANGLE_TYPE_NAME(NAME) crea::demangle_type_name(NAME) #else // OTHER #define CREA_DEMANGLE_TYPE_NAME(NAME) NAME #endif #define CREA_GET_CURRENT_OBJECT_NAME \ CREA_DEMANGLE_TYPE_NAME(typeid(*this).name()) #define CREA_GET_TYPE_NAME(A) \ CREA_DEMANGLE_TYPE_NAME(typeid(A).name()) //----------------------------------------------------------------------------- namespace crea { /// Template method which returns the name of a type template inline std::string TypeName() { return CREA_DEMANGLE_TYPE_NAME(typeid(T).name()); } /// Template method which returns the name of the type of a variable template inline std::string TypeName(const T& t) { return CREA_DEMANGLE_TYPE_NAME(typeid(t).name()); } /// Specialisation of TypeName when the type passed is already a type_info : /// The user does not want to know the type of the type_info class but of /// the class whose type_info is passed ! template <> inline std::string TypeName(const std::type_info& t) { return CREA_DEMANGLE_TYPE_NAME(t.name()); } /// Template method which returns the human readable name of a type template inline std::string HumanTypeName() { return TypeName(); } /// Template method which returns the human readable name of the type of a variable template inline std::string HumanTypeName(const T& t) { return TypeName(t); } /// Specialisation of TypeName when the type passed is already a type_info : /// The user does not want to know the type of the type_info class but of /// the class whose type_info is passed ! template <> inline std::string HumanTypeName(const std::type_info& t) { return TypeName(t); } /// Macro to specialise the template function TypeName for certain types /// (typically highly template types, such as STL types) /// in order to return a **really** human readable string #define CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(TYPE,NAME) \ template <> inline std::string HumanTypeName< TYPE >() \ { return NAME; } \ template <> inline std::string HumanTypeName< TYPE >(const TYPE&) \ { return NAME; } CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(std::string,"String"); CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(signed char,"Char"); CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(signed short,"Short"); CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(signed int,"Int"); CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned char,"UChar"); CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned short,"UShort"); CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned int,"UInt"); CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(float,"Float"); CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(double,"Double"); CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(bool,"Bool"); CREA_DEFINE_HUMAN_READABLE_TYPE_NAME(long,"Long"); // Human readable strings for std::vector #define CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(TYPE) \ template <> inline std::string HumanTypeName< std::vector >() \ { return "Vector"+HumanTypeName(); } \ template <> inline std::string HumanTypeName< std::vector > \ (const std::vector&) { return "Vector"+HumanTypeName(); } CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int8_t); CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint8_t); CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int16_t); CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint16_t); CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int32_t); CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint32_t); CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(long); CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(float); CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(double); CREA_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(std::string); /// The crea::TypeInfo type is a const ref on std::type_info (which can only be manipulated as such (because typeid returns const std::type_info& and type_info has all constructors private)) typedef const std::type_info& TypeInfo; CREA_EXPORT void* run_time_up_or_down_cast( const std::type_info& target_type, const std::type_info& source_type, void* source_pointer ); CREA_EXPORT void* run_time_up_or_down_cast( const std::type_info& target_type, const std::type_info& source_type, const void* source_pointer ); } #endif