/*========================================================================= Program: bbtk Module: $RCSfile: bbtkRTTI.h,v $ Language: C++ Date: $Date: 2008/07/30 09:02:31 $ Version: $Revision: 1.3 $ 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/bbtk/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 __BBTKRTTI_H_INCLUDED__ #define __BBTKRTTI_H_INCLUDED__ #include "bbtkSystem.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 bbtk { 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 BBTK_DEMANGLE_TYPE_NAME(NAME) bbtk::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 bbtk { 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 BBTK_DEMANGLE_TYPE_NAME(NAME) bbtk::demangle_type_name(NAME) #else // OTHER #define BBTK_DEMANGLE_TYPE_NAME(NAME) NAME #endif #define BBTK_GET_CURRENT_OBJECT_NAME \ BBTK_DEMANGLE_TYPE_NAME(typeid(*this).name()) #define BBTK_GET_TYPE_NAME(A) \ BBTK_DEMANGLE_TYPE_NAME(typeid(A).name()) //----------------------------------------------------------------------------- namespace bbtk { /// Template method which returns the name of a type template inline std::string TypeName() { return BBTK_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 BBTK_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 BBTK_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 BBTK_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; } BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::string,"String"); BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed char,"Char"); BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed short,"Short"); BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed int,"Int"); BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned char,"UChar"); BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned short,"UShort"); BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned int,"UInt"); BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(float,"Float"); BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(double,"Double"); BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(bool,"Bool"); BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(long,"Long"); // Human readable strings for std::vector #define BBTK_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(); } BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int8_t); BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint8_t); BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int16_t); BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint16_t); BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int32_t); BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint32_t); BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(long); BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(float); BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(double); BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(std::string); /// The bbtk::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; BBTK_EXPORT void* run_time_up_or_down_cast( const std::type_info& target_type, const std::type_info& source_type, void* source_pointer ); BBTK_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