mangled_storage_base.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2016 Klemens Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_DLL_DETAIL_MANGLE_STORAGE_BASE_HPP_
  7. #define BOOST_DLL_DETAIL_MANGLE_STORAGE_BASE_HPP_
  8. #include <vector>
  9. #include <string>
  10. #include <map>
  11. #include <boost/dll/detail/demangling/demangle_symbol.hpp>
  12. #include <boost/dll/library_info.hpp>
  13. #include <boost/type_index/ctti_type_index.hpp>
  14. #include <boost/type_traits/remove_reference.hpp>
  15. namespace boost { namespace dll { namespace detail {
  16. ///stores the mangled names with the demangled name.
  17. struct mangled_storage_base
  18. {
  19. struct entry
  20. {
  21. std::string mangled;
  22. std::string demangled;
  23. entry() = default;
  24. entry(const std::string & m, const std::string &d) : mangled(m), demangled(d) {}
  25. entry(const entry&) = default;
  26. entry(entry&&) = default;
  27. entry &operator= (const entry&) = default;
  28. entry &operator= (entry&&) = default;
  29. };
  30. protected:
  31. std::vector<entry> storage_;
  32. ///if a unknown class is imported it can be overloaded by this type
  33. std::map<boost::typeindex::ctti_type_index, std::string> aliases_;
  34. public:
  35. void assign(const mangled_storage_base & storage)
  36. {
  37. aliases_ = storage.aliases_;
  38. storage_ = storage.storage_;
  39. }
  40. void swap( mangled_storage_base & storage)
  41. {
  42. aliases_.swap(storage.aliases_);
  43. storage_.swap(storage.storage_);
  44. }
  45. void clear()
  46. {
  47. storage_.clear();
  48. aliases_.clear();
  49. }
  50. std::vector<entry> & get_storage() {return storage_;};
  51. template<typename T>
  52. std::string get_name() const
  53. {
  54. using boost::typeindex::ctti_type_index;
  55. auto tx = ctti_type_index::type_id<T>();
  56. auto val = (aliases_.count(tx) > 0) ? aliases_.at(tx) : tx.pretty_name();
  57. return val;
  58. }
  59. mangled_storage_base() = default;
  60. mangled_storage_base(mangled_storage_base&&) = default;
  61. mangled_storage_base(const mangled_storage_base&) = default;
  62. mangled_storage_base(const std::vector<std::string> & symbols) { add_symbols(symbols);}
  63. explicit mangled_storage_base(library_info & li) : mangled_storage_base(li.symbols()) {}
  64. explicit mangled_storage_base(
  65. const boost::dll::fs::path& library_path,
  66. bool throw_if_not_native_format = true)
  67. : mangled_storage_base(library_info(library_path, throw_if_not_native_format).symbols())
  68. {
  69. }
  70. void load(library_info & li) { storage_.clear(); add_symbols(li.symbols()); };
  71. void load(const boost::dll::fs::path& library_path,
  72. bool throw_if_not_native_format = true)
  73. {
  74. storage_.clear();
  75. add_symbols(library_info(library_path, throw_if_not_native_format).symbols());
  76. };
  77. /*! Allows do add a class as alias, if the class imported is not known
  78. * in this binary.
  79. * @tparam Alias The Alias type
  80. * @param The name to create the alias for.
  81. *
  82. * @note There can be multiple aliases, this is on purpose.
  83. */
  84. template<typename Alias> void add_alias(const std::string& name)
  85. {
  86. aliases_.emplace(
  87. boost::typeindex::ctti_type_index::type_id<Alias>(),
  88. name
  89. );
  90. }
  91. void add_symbols(const std::vector<std::string> & symbols)
  92. {
  93. for (auto & sym : symbols)
  94. {
  95. auto dm = demangle_symbol(sym);
  96. if (!dm.empty())
  97. storage_.emplace_back(sym, dm);
  98. else
  99. storage_.emplace_back(sym, sym);
  100. }
  101. }
  102. };
  103. }}}
  104. #endif /* BOOST_DLL_DETAIL_MANGLE_STORAGE_HPP_ */