Skip to content
Snippets Groups Projects
Commit de0592a8 authored by Martin Nolte's avatar Martin Nolte
Browse files

don't leak memory (the demangled asciiz string has to be freed!)

[[Imported from SVN: r6402]]
parent f3cae1c6
No related branches found
No related tags found
No related merge requests found
......@@ -8,38 +8,50 @@
* of a given object or type as a string
*/
#include <cstdlib>
#include <string>
#include <typeinfo>
#ifdef __GNUC__
#include <cxxabi.h>
#endif
#endif // #ifdef __GNUC__
namespace Dune {
/** \brief Provide the demangled class name of a given object as a string */
template <class T>
std::string className(T &t)
std::string className ( T &t )
{
std::string className = typeid( t ).name();
#ifdef __GNUC__
int status;
return abi::__cxa_demangle(typeid(t).name(),0,0,&status);
#else
return typeid(t).name();
#endif
char *demangled = abi::__cxa_demangle( className.c_str(), 0, 0, &status );
if( demangled )
{
className = demangled;
std::free( demangled );
}
#endif // #ifdef __GNUC__
return className;
};
/** \brief Provide the demangled class name of a type T as a string */
template <class T>
std::string className()
std::string className ()
{
std::string className = typeid( T ).name();
#ifdef __GNUC__
int status;
return abi::__cxa_demangle(typeid(T).name(),0,0,&status);
#else
return typeid(T).name();
#endif
char *demangled = abi::__cxa_demangle( className.c_str(), 0, 0, &status );
if( demangled )
{
className = demangled;
std::free( demangled );
}
#endif // #ifdef __GNUC__
return className;
};
}
} // namespace Dune
#endif // DUNE_CLASSNAME_HH
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