Scarab  v1.6.1
Project 8 C++ Utility Library
typename.cc
Go to the documentation of this file.
1 /*
2  * typename.cc
3  *
4  * Created on: Feb 10, 2016
5  * Author: nsoblath
6  */
7 
8 #include "typename.hh"
9 
10 #ifdef __GNUG__
11 #include <cstdlib>
12 #include <memory>
13 #include <cxxabi.h>
14 #endif
15 
16 
17 namespace scarab
18 {
19 
20 #ifdef __GNUG__
21 #ifdef USE_CPP11
22 
23  std::string demangle( const char* a_name )
24  {
25 
26  int t_status = -4; // some arbitrary value to eliminate the compiler warning
27 
28  std::unique_ptr< char, void(*)( void* ) > res( abi::__cxa_demangle( a_name, NULL, NULL, &t_status ), std::free );
29 
30  return ( t_status==0 ) ? res.get() : a_name ;
31  }
32 
33 #else // USE_CPP11
34 
35  struct handle
36  {
37  char* f_ptr;
38  handle( char* a_ptr ) : f_ptr( a_ptr ) {}
39  ~handle() { std::free( f_ptr ); }
40  };
41 
42 
43  std::string demangle( const char* a_name )
44  {
45 
46  int t_status = -4; // some arbitrary value to eliminate the compiler warning
47 
48  handle result( abi::__cxa_demangle( a_name, NULL, NULL, &t_status ) );
49 
50  return ( t_status==0 ) ? result.f_ptr : a_name ;
51  }
52 
53 #endif // USE_CPP11
54 
55 #else // __GNUG__
56 
57  // does nothing if not g++
58  std::string demangle( const char* a_name ) {
59  return a_name;
60  }
61 
62 #endif // __GNUG__
63 
64 }
std::string demangle(const char *a_name)
Definition: typename.cc:58