Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:43

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