util.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*! \file util.hpp
  2. \brief Internal misc utilities
  3. \ingroup Internal */
  4. /*
  5. Copyright (c) 2014, Randolph Voorhies, Shane Grant
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in the
  13. documentation and/or other materials provided with the distribution.
  14. * Neither the name of the copyright holder nor the
  15. names of its contributors may be used to endorse or promote products
  16. derived from this software without specific prior written permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  21. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef CEREAL_DETAILS_UTIL_HPP_
  29. #define CEREAL_DETAILS_UTIL_HPP_
  30. #include <typeinfo>
  31. #include <string>
  32. #ifdef _MSC_VER
  33. namespace cereal
  34. {
  35. namespace util
  36. {
  37. //! Demangles the type encoded in a string
  38. /*! @internal */
  39. inline std::string demangle( std::string const & name )
  40. { return name; }
  41. //! Gets the demangled name of a type
  42. /*! @internal */
  43. template <class T> inline
  44. std::string demangledName()
  45. { return typeid( T ).name(); }
  46. } // namespace util
  47. } // namespace cereal
  48. #else // clang or gcc
  49. #include <cxxabi.h>
  50. #include <cstdlib>
  51. namespace cereal
  52. {
  53. namespace util
  54. {
  55. //! Demangles the type encoded in a string
  56. /*! @internal */
  57. inline std::string demangle(std::string mangledName)
  58. {
  59. int status = 0;
  60. char *demangledName = nullptr;
  61. std::size_t len;
  62. demangledName = abi::__cxa_demangle(mangledName.c_str(), 0, &len, &status);
  63. std::string retName(demangledName);
  64. free(demangledName);
  65. return retName;
  66. }
  67. //! Gets the demangled name of a type
  68. /*! @internal */
  69. template<class T> inline
  70. std::string demangledName()
  71. { return demangle(typeid(T).name()); }
  72. }
  73. } // namespace cereal
  74. #endif // clang or gcc branch of _MSC_VER
  75. #endif // CEREAL_DETAILS_UTIL_HPP_