demangle_util.h
Go to the documentation of this file.
00001 #ifndef DEMANGLE_UTIL_H
00002 #define DEMANGLE_UTIL_H
00003 
00004 #include <string>
00005 
00006 
00007 #if defined( __clang__ ) && defined( __has_include )
00008 # if __has_include(<cxxabi.h>)
00009 #  define HAS_CXXABI_H
00010 # endif
00011 #elif defined( __GLIBCXX__ ) || defined( __GLIBCPP__ )
00012 # define HAS_CXXABI_H
00013 #endif
00014 
00015 #if defined( HAS_CXXABI_H )
00016 # include <cxxabi.h>
00017 # include <cstdlib>
00018 # include <cstddef>
00019 #endif
00020 
00021 namespace BT
00022 {
00023 
00024 inline char const * demangle_alloc( char const * name ) noexcept;
00025 inline void demangle_free( char const * name ) noexcept;
00026 
00027 class scoped_demangled_name
00028 {
00029 private:
00030     char const * m_p;
00031 
00032 public:
00033     explicit scoped_demangled_name( char const * name ) noexcept :
00034         m_p( demangle_alloc( name ) )
00035     {
00036     }
00037 
00038     ~scoped_demangled_name() noexcept
00039     {
00040         demangle_free( m_p );
00041     }
00042 
00043     char const * get() const noexcept
00044     {
00045         return m_p;
00046     }
00047 
00048     scoped_demangled_name( scoped_demangled_name const& ) = delete;
00049     scoped_demangled_name& operator= ( scoped_demangled_name const& ) = delete;
00050 };
00051 
00052 
00053 #if defined( HAS_CXXABI_H )
00054 
00055 inline char const * demangle_alloc( char const * name ) noexcept
00056 {
00057     int status = 0;
00058     std::size_t size = 0;
00059     return abi::__cxa_demangle( name, NULL, &size, &status );
00060 }
00061 
00062 inline void demangle_free( char const * name ) noexcept
00063 {
00064     std::free( const_cast< char* >( name ) );
00065 }
00066 
00067 inline std::string demangle( char const * name )
00068 {
00069     scoped_demangled_name demangled_name( name );
00070     char const * const p = demangled_name.get();
00071     if( p )
00072     {
00073         return p;
00074     }
00075     else
00076     {
00077         return name;
00078     }
00079 }
00080 
00081 #else
00082 
00083 inline char const * demangle_alloc( char const * name ) noexcept
00084 {
00085     return name;
00086 }
00087 
00088 inline void demangle_free( char const * ) noexcept
00089 {
00090 }
00091 
00092 inline std::string demangle( char const * name )
00093 {
00094     return name;
00095 }
00096 
00097 #endif
00098 
00099 } // namespace BT
00100 
00101 #undef HAS_CXXABI_H
00102 
00103 #endif // DEMANGLE_UTIL_H


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:10