typestr.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // (C) Copyright 2009 Andrew Sutton
  2. //
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0 (See accompanying file
  5. // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef ORIGIN_TYPESTR_HPP
  7. #define ORIGIN_TYPESTR_HPP
  8. #include <string>
  9. #include <cstring>
  10. #include <typeinfo>
  11. #if defined(__GNUC__)
  12. #include <cxxabi.h>
  13. #endif
  14. template<typename T> struct type_name { };
  15. /**
  16. * Return a string that describes the type of the given template parameter.
  17. * The type name depends on the results of the typeid operator.
  18. *
  19. * @todo Rewrite this so that demangle will dynamically allocate the memory.
  20. */
  21. template <typename T>
  22. std::string typestr() {
  23. #if defined(__GNUC__)
  24. std::size_t const BUFSIZE = 8192;
  25. std::size_t n = BUFSIZE;
  26. char buf[BUFSIZE];
  27. abi::__cxa_demangle(typeid(type_name<T>).name(), buf, &n, 0);
  28. return std::string(buf, ::strlen(buf));
  29. #else
  30. return typeid(type_name<T>).name();
  31. #endif
  32. }
  33. /**
  34. * Return a string that describes the type of the given parameter. The type
  35. * name depends on the results of the typeid operator.
  36. */
  37. template <typename T>
  38. inline std::string typestr(T const&)
  39. { return typestr<T>(); }
  40. #endif