OMCompiler/SimulationRuntime/cpp/Core/Utils/extension/shared_library.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Boost.Extension / shared_library: | ||
| 3 | * Functions for shared_library loading. | ||
| 4 | * A basic wrapper around the OS-specific calls. | ||
| 5 | * | ||
| 6 | * (C) Copyright Jeremy Pack 2008 | ||
| 7 | * Distributed under the Boost Software License, Version 1.0. (See | ||
| 8 | * accompanying file LICENSE_1_0.txt or copy at | ||
| 9 | * http://www.boost.org/LICENSE_1_0.txt) | ||
| 10 | * | ||
| 11 | * See http://www.boost.org/ for latest version. | ||
| 12 | */ | ||
| 13 | |||
| 14 | |||
| 15 | #ifndef BOOST_EXTENSION_SHARED_LIBRARY_HPP | ||
| 16 | #define BOOST_EXTENSION_SHARED_LIBRARY_HPP | ||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | #include <string> | ||
| 21 | |||
| 22 | #include <Core/Utils/extension/common.hpp> | ||
| 23 | #include <Core/Utils/extension/impl/library_impl.hpp> | ||
| 24 | |||
| 25 | namespace boost { | ||
| 26 | namespace extensions { | ||
| 27 | template <class TypeInfo> | ||
| 28 | class basic_type_map; | ||
| 29 | |||
| 30 | /** \brief A wrapper around OS-specific shared library functions. | ||
| 31 | |||
| 32 | \note This class is inherently not type-safe. Carefully | ||
| 33 | check the signature of the function or type in the shared library | ||
| 34 | against the template arguments of the get functions. | ||
| 35 | */ | ||
| 36 |
3/3✓ Branch 1 taken 558 times.
✓ Branch 4 taken 558 times.
✓ Branch 8 taken 413 times.
|
2087 | class shared_library { |
| 37 | public: | ||
| 38 | shared_library() | ||
| 39 | 558 | {} | |
| 40 | /** shared_library constructor | ||
| 41 | * \param location The relative or absolute path of the shared library. | ||
| 42 | * \param auto_close An optional parameter which defaults to false. | ||
| 43 | * If set to true, the destructor will close this shared library | ||
| 44 | * if necessary. | ||
| 45 | */ | ||
| 46 | shared_library(const std::string& location, bool auto_close = false) | ||
| 47 |
1/1✓ Branch 2 taken 558 times.
|
558 | : location_(location), handle_(0), auto_close_(auto_close) { |
| 48 | } | ||
| 49 | |||
| 50 | /** shared_library destructor | ||
| 51 | * If auto_close_ was set to true in the constructor, this closes | ||
| 52 | * the library if it is currently open. | ||
| 53 | */ | ||
| 54 | 2645 | ~shared_library() { | |
| 55 |
3/4✓ Branch 0 taken 2087 times.
✓ Branch 1 taken 558 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2087 times.
|
2645 | if (handle_ && auto_close_) |
| 56 | close(); | ||
| 57 | 2645 | } | |
| 58 | |||
| 59 | /** \return true if the shared library is currently open | ||
| 60 | * and referenced by this object. | ||
| 61 | * \pre None. | ||
| 62 | * \post None. | ||
| 63 | */ | ||
| 64 |
1/2✓ Branch 0 taken 413 times.
✗ Branch 1 not taken.
|
413 | bool is_open() const { return handle_ != 0; } |
| 65 | |||
| 66 | /** \brief Open the shared library. | ||
| 67 | * \return true if the shared library was opened successfully. | ||
| 68 | * \pre None. | ||
| 69 | * \post If true is returned, the shared library is opened and | ||
| 70 | * get() can be called. | ||
| 71 | */ | ||
| 72 | 558 | bool open() { | |
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 558 times.
|
558 | if (handle_) |
| 74 | close(); | ||
| 75 | 558 | return (handle_ = impl::load_shared_library(location_.c_str())) != 0; | |
| 76 | } | ||
| 77 | |||
| 78 | /** \brief Close the shared library. | ||
| 79 | * This calls the OS specific close function for shared libraries. | ||
| 80 | * Usually, this decrements the reference count of the shared library. | ||
| 81 | * Once a shared library has a reference count of 0, it can be actually | ||
| 82 | * unloaded at any time. | ||
| 83 | * \return true if the close function was successful. | ||
| 84 | * \pre is_open() == true. | ||
| 85 | * \post is_open() == false. | ||
| 86 | */ | ||
| 87 | bool close() { | ||
| 88 | ✗ | return impl::close_shared_library(handle_); | |
| 89 | } | ||
| 90 | |||
| 91 | /** \brief Call a special Extension function in the library. | ||
| 92 | * | ||
| 93 | * There is a special function called | ||
| 94 | * boost_extension_exported_type_map_function which is commonly | ||
| 95 | * used by shared libraries. The call function attempts to find | ||
| 96 | * and call that function, given a type_map. | ||
| 97 | * | ||
| 98 | * \return true on success. | ||
| 99 | * \param types A type_map that will be sent to the function. | ||
| 100 | * \pre is_open() == true | ||
| 101 | * \post None. | ||
| 102 | */ | ||
| 103 | template <class TypeInfo> | ||
| 104 | bool call(basic_type_map<TypeInfo>& types) { | ||
| 105 | void (*func)(basic_type_map<TypeInfo>&); | ||
| 106 | func = get<void, basic_type_map<TypeInfo>&> | ||
| 107 | ("boost_extension_exported_type_map_function"); | ||
| 108 | if (func) { | ||
| 109 | (*func)(types); | ||
| 110 | return true; | ||
| 111 | } else { | ||
| 112 | return false; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | // If Doxygen is being run, use more readable definitions for the | ||
| 116 | // documentation. | ||
| 117 | #ifdef BOOST_EXTENSION_DOXYGEN_INVOKED | ||
| 118 | /** \brief Get a function reference. | ||
| 119 | * | ||
| 120 | * A templated function taking as template arguments the | ||
| 121 | * type of the return value and parameters of a function | ||
| 122 | * to look up in the shared library. | ||
| 123 | * | ||
| 124 | * This function must have been declared with the same | ||
| 125 | * parameters and return type and marked as extern "C". | ||
| 126 | * | ||
| 127 | * Depending on platform and compiler settings, it may also | ||
| 128 | * be necessary to prefix the function with BOOST_EXTENSION_DECL, | ||
| 129 | * to make it externally visible. | ||
| 130 | * | ||
| 131 | * \warning If the function signature does not match, strange errors | ||
| 132 | * can occur. | ||
| 133 | * \pre is_open() == true. | ||
| 134 | * \post None. | ||
| 135 | */ | ||
| 136 | template <class RetValue, class Params...> | ||
| 137 | FunctionPtr<ReturnValue (Params...)> | ||
| 138 | get(const std::string& name) const { | ||
| 139 | } | ||
| 140 | #else | ||
| 141 | template <class ReturnValue, class... Params> | ||
| 142 | ReturnValue (*get(const std::string& name) const)(Params...) { | ||
| 143 | // Cast the handle or pointer to the function to the correct type. | ||
| 144 | // This is NOT typesafe. See the documentation of shared_library::get | ||
| 145 | return reinterpret_cast<ReturnValue (*)(Params...)> | ||
| 146 |
1/2✓ Branch 1 taken 558 times.
✗ Branch 2 not taken.
|
558 | (impl::get_function(handle_, name.c_str())); |
| 147 | } | ||
| 148 | #endif | ||
| 149 | protected: | ||
| 150 | std::string location_; | ||
| 151 | impl::library_handle handle_; | ||
| 152 | bool auto_close_; | ||
| 153 | }; | ||
| 154 | } // namespace extensions | ||
| 155 | } // namespace boost | ||
| 156 | #endif // BOOST_EXTENSION_SHARED_LIBRARY_HPP | ||
| 157 |