code_junkie

Unmangling the result of std::type_info::name

Nov 12th, 2011
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #ifdef HAVE_CXA_DEMANGLE
  2. const char* demangle(const char* name)
  3. {
  4. char buf[1024];
  5. unsigned int size=1024;
  6. int status;
  7. char* res = abi::__cxa_demangle (name,
  8. buf,
  9. &size,
  10. &status);
  11. return res;
  12. }
  13. #else
  14. const char* demangle(const char* name)
  15. {
  16. return name;
  17. }
  18. #endif
  19.  
  20. __FUNCTION__
  21. __FILE__
  22. __LINE__
  23.  
  24. e.g.:
  25.  
  26. log(__FILE__, __LINE__, __FUNCTION__, mymessage);
  27.  
  28. const string demangle(const char* name) {
  29.  
  30. int status = -4;
  31.  
  32. char* res = abi::__cxa_demangle(name, NULL, NULL, &status);
  33.  
  34. const char* const demangled_name = (status==0)?res:name;
  35.  
  36. string ret_val(demangled_name);
  37.  
  38. free(res);
  39.  
  40. return ret_val;
  41. }
  42.  
  43. virtual bool mutex::do_unlock()
  44.  
  45. // KeithB's solution is good, but has one serious flaw in that unless buf is static
  46. // it'll get trashed from the stack before it is returned in res - and will point who-knows-where
  47. // Here's that problem fixed, but the code is still non-re-entrant and not thread-safe.
  48. // Anyone care to improve it?
  49.  
  50. #include <cxxabi.h>
  51.  
  52. // todo: javadoc this properly
  53. const char* demangle(const char* name)
  54. {
  55. static char buf[1024];
  56. size_t size = sizeof(buf);
  57. int status;
  58. // todo:
  59. char* res = abi::__cxa_demangle (name,
  60. buf,
  61. &size,
  62. &status);
  63. buf[sizeof(buf) - 1] = 0; // I'd hope __cxa_demangle does this when the name is huge, but just in case.
  64. return res;
  65. }
Add Comment
Please, Sign In to add comment