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