demangle_symbol.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2015 Klemens Morgenstern
  2. //
  3. // This file provides a demangling for function names, i.e. entry points of a dll.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // See http://www.boost.org/LICENSE_1_0.txt
  7. #ifndef BOOST_DLL_DEMANGLE_SYMBOL_HPP_
  8. #define BOOST_DLL_DEMANGLE_SYMBOL_HPP_
  9. #include <boost/dll/config.hpp>
  10. #include <string>
  11. #include <algorithm>
  12. #include <memory>
  13. #if defined(BOOST_MSVC) || defined(BOOST_MSVC_FULL_VER)
  14. namespace boost
  15. {
  16. namespace dll
  17. {
  18. namespace detail
  19. {
  20. typedef void * (__cdecl * allocation_function)(std::size_t);
  21. typedef void (__cdecl * free_function)(void *);
  22. extern "C" char* __unDName( char* outputString,
  23. const char* name,
  24. int maxStringLength, // Note, COMMA is leading following optional arguments
  25. allocation_function pAlloc,
  26. free_function pFree,
  27. unsigned short disableFlags
  28. );
  29. inline std::string demangle_symbol(const char *mangled_name)
  30. {
  31. allocation_function alloc = [](std::size_t size){return static_cast<void*>(new char[size]);};
  32. free_function free_f = [](void* p){delete [] static_cast<char*>(p);};
  33. std::unique_ptr<char> name { __unDName(
  34. nullptr,
  35. mangled_name,
  36. 0,
  37. alloc,
  38. free_f,
  39. static_cast<unsigned short>(0))};
  40. return std::string(name.get());
  41. }
  42. inline std::string demangle_symbol(const std::string& mangled_name)
  43. {
  44. return demangle_symbol(mangled_name.c_str());
  45. }
  46. }}}
  47. #else
  48. #include <boost/core/demangle.hpp>
  49. namespace boost
  50. {
  51. namespace dll
  52. {
  53. namespace detail
  54. {
  55. inline std::string demangle_symbol(const char *mangled_name)
  56. {
  57. if (*mangled_name == '_')
  58. {
  59. //because it start's with an underline _
  60. auto dm = boost::core::demangle(mangled_name);
  61. if (!dm.empty())
  62. return dm;
  63. else
  64. return (mangled_name);
  65. }
  66. //could not demangled
  67. return "";
  68. }
  69. //for my personal convenience
  70. inline std::string demangle_symbol(const std::string& mangled_name)
  71. {
  72. return demangle_symbol(mangled_name.c_str());
  73. }
  74. }
  75. namespace experimental
  76. {
  77. using ::boost::dll::detail::demangle_symbol;
  78. }
  79. }}
  80. #endif
  81. #endif /* BOOST_DEMANGLE_HPP_ */