utility.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Boost.Units - A C++ library for zero-overhead dimensional analysis and
  2. // unit/quantity manipulation and conversion
  3. //
  4. // Copyright (C) 2003-2008 Matthias Christian Schabel
  5. // Copyright (C) 2008 Steven Watanabe
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_UNITS_UTILITY_HPP
  11. #define BOOST_UNITS_UTILITY_HPP
  12. #include <cstdlib>
  13. #include <typeinfo>
  14. #include <string>
  15. #if defined(__GLIBCXX__) || defined(__GLIBCPP__)
  16. #define BOOST_UNITS_USE_DEMANGLING
  17. #include <cxxabi.h>
  18. #endif // __GNUC__
  19. #ifdef BOOST_UNITS_USE_DEMANGLING
  20. #include <boost/algorithm/string/replace.hpp>
  21. namespace boost {
  22. namespace units {
  23. namespace detail {
  24. inline
  25. std::string
  26. demangle(const char* name)
  27. {
  28. // need to demangle C++ symbols
  29. char* realname;
  30. std::size_t len;
  31. int stat;
  32. realname = abi::__cxa_demangle(name,NULL,&len,&stat);
  33. if (realname != NULL)
  34. {
  35. std::string out(realname);
  36. std::free(realname);
  37. boost::replace_all(out,"boost::units::","");
  38. return out;
  39. }
  40. return std::string("demangle :: error - unable to demangle specified symbol");
  41. }
  42. } // namespace detail
  43. template<class L>
  44. std::string simplify_typename(const L& /*source*/)
  45. {
  46. const std::string demangled = detail::demangle(typeid(L).name());
  47. return demangled;
  48. }
  49. } // namespace units
  50. } // namespace boost
  51. #else // BOOST_UNITS_USE_DEMANGLING
  52. namespace boost {
  53. namespace units {
  54. namespace detail {
  55. inline
  56. std::string
  57. demangle(const char* name)
  58. {
  59. return name;
  60. }
  61. } // namespace detail
  62. template<class L>
  63. std::string simplify_typename(const L& /*source*/)
  64. {
  65. return std::string(typeid(L).name());
  66. }
  67. } // namespace units
  68. } // namespace boost
  69. // To get system-specific predefined macros:
  70. // gcc -arch ppc -dM -E - < /dev/null | sort
  71. #endif // BOOST_UNITS_USE_DEMANGLING
  72. #endif // BOOST_UNITS_UTILITY_HPP