Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-19 08:18:39

0001 //  Copyright 2016 Klemens Morgenstern
0002 //
0003 // Distributed under the Boost Software License, Version 1.0.
0004 // (See accompanying file LICENSE_1_0.txt
0005 // or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 #ifndef BOOST_DLL_DETAIL_MANGLE_STORAGE_BASE_HPP_
0008 #define BOOST_DLL_DETAIL_MANGLE_STORAGE_BASE_HPP_
0009 
0010 #include <vector>
0011 #include <string>
0012 #include <map>
0013 #include <type_traits>
0014 
0015 #include <boost/dll/detail/demangling/demangle_symbol.hpp>
0016 #include <boost/dll/library_info.hpp>
0017 #include <boost/type_index/ctti_type_index.hpp>
0018 
0019 
0020 namespace boost { namespace dll { namespace detail {
0021 
0022 ///stores the mangled names with the demangled name.
0023 struct mangled_storage_base
0024 {
0025     struct entry
0026     {
0027         std::string mangled;
0028         std::string demangled;
0029         entry() = default;
0030         entry(const std::string & m, const std::string &d) : mangled(m), demangled(d) {}
0031         entry(const entry&) = default;
0032         entry(entry&&)         = default;
0033         entry &operator= (const entry&) = default;
0034         entry &operator= (entry&&)         = default;
0035     };
0036 protected:
0037     std::vector<entry> storage_;
0038     ///if a unknown class is imported it can be overloaded by this type
0039     std::map<boost::typeindex::ctti_type_index, std::string> aliases_;
0040 public:
0041     void assign(const mangled_storage_base & storage)
0042     {
0043         aliases_  = storage.aliases_;
0044         storage_  = storage.storage_;
0045     }
0046     void swap( mangled_storage_base & storage)
0047     {
0048         aliases_.swap(storage.aliases_);
0049         storage_.swap(storage.storage_);
0050     }
0051     void clear()
0052     {
0053         storage_.clear();
0054         aliases_.clear();
0055     }
0056     std::vector<entry> & get_storage() {return storage_;};
0057     template<typename T>
0058     std::string get_name() const
0059     {
0060         using boost::typeindex::ctti_type_index;
0061         auto tx = ctti_type_index::type_id<T>();
0062         auto val = (aliases_.count(tx) > 0) ? aliases_.at(tx) : tx.pretty_name();
0063         return val;
0064     }
0065 
0066     mangled_storage_base() = default;
0067     mangled_storage_base(mangled_storage_base&&) = default;
0068     mangled_storage_base(const mangled_storage_base&) = default;
0069 
0070     mangled_storage_base(const std::vector<std::string> & symbols) { add_symbols(symbols);}
0071 
0072     explicit mangled_storage_base(library_info & li) : mangled_storage_base(li.symbols()) {}
0073 
0074     explicit mangled_storage_base(
0075             const boost::dll::fs::path& library_path,
0076             bool throw_if_not_native_format = true)
0077         : mangled_storage_base(library_info(library_path, throw_if_not_native_format).symbols())
0078     {
0079 
0080     }
0081 
0082     void load(library_info & li) { storage_.clear(); add_symbols(li.symbols()); };
0083     void load(const boost::dll::fs::path& library_path,
0084             bool throw_if_not_native_format = true)
0085     {
0086         storage_.clear();
0087         add_symbols(library_info(library_path, throw_if_not_native_format).symbols());
0088     };
0089 
0090     /*! Allows do add a class as alias, if the class imported is not known
0091      * in this binary.
0092      * @tparam Alias The Alias type
0093      *  @param The name to create the alias for.
0094      *
0095      *  @note There can be multiple aliases, this is on purpose.
0096      */
0097     template<typename Alias> void add_alias(const std::string& name)
0098     {
0099         aliases_.emplace(
0100             boost::typeindex::ctti_type_index::type_id<Alias>(),
0101             name
0102             );
0103     }
0104     void add_symbols(const std::vector<std::string> & symbols)
0105     {
0106         for (auto & sym : symbols)
0107         {
0108             auto dm = demangle_symbol(sym);
0109             if (!dm.empty())
0110                 storage_.emplace_back(sym, dm);
0111             else
0112                 storage_.emplace_back(sym, sym);
0113         }
0114     }
0115 
0116 
0117 };
0118 
0119 
0120 }}}
0121 
0122 #endif /* BOOST_DLL_DETAIL_MANGLE_STORAGE_HPP_ */