| Directory: | ./ |
|---|---|
| File: | include/hpp/core/plugin.hh |
| Date: | 2025-03-10 11:18:21 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 7 | 7 | 100.0% |
| Branches: | 2 | 4 | 50.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2019, Joseph Mirabel | ||
| 2 | // Authors: Joseph Mirabel (joseph.mirabel@laas.fr) | ||
| 3 | // | ||
| 4 | |||
| 5 | // Redistribution and use in source and binary forms, with or without | ||
| 6 | // modification, are permitted provided that the following conditions are | ||
| 7 | // met: | ||
| 8 | // | ||
| 9 | // 1. Redistributions of source code must retain the above copyright | ||
| 10 | // notice, this list of conditions and the following disclaimer. | ||
| 11 | // | ||
| 12 | // 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | // notice, this list of conditions and the following disclaimer in the | ||
| 14 | // documentation and/or other materials provided with the distribution. | ||
| 15 | // | ||
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| 20 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | ||
| 27 | // DAMAGE. | ||
| 28 | |||
| 29 | #ifndef HPP_CORE_PLUGIN_HH | ||
| 30 | #define HPP_CORE_PLUGIN_HH | ||
| 31 | |||
| 32 | #include <hpp/core/config.hh> | ||
| 33 | #include <hpp/core/fwd.hh> | ||
| 34 | #include <string> | ||
| 35 | |||
| 36 | namespace hpp { | ||
| 37 | namespace core { | ||
| 38 | /// \addtogroup hpp_core_plugin Plugins | ||
| 39 | /// \{ | ||
| 40 | |||
| 41 | /// Plugin mechanism to declare new features in ProblemSolver class | ||
| 42 | class ProblemSolverPlugin { | ||
| 43 | public: | ||
| 44 | const std::string& name() const { return name_; } | ||
| 45 | |||
| 46 | const std::string& version() const { return version_; } | ||
| 47 | |||
| 48 | 1 | bool initialize(ProblemSolverPtr_t ps) { | |
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (initialized_) return true; |
| 50 | 1 | initialized_ = impl_initialize(ps); | |
| 51 | 1 | return initialized_; | |
| 52 | } | ||
| 53 | |||
| 54 | 2 | virtual ~ProblemSolverPlugin() {} | |
| 55 | |||
| 56 | protected: | ||
| 57 | virtual bool impl_initialize(ProblemSolverPtr_t ps) = 0; | ||
| 58 | |||
| 59 | 1 | ProblemSolverPlugin(const std::string& name, const std::string& version) | |
| 60 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | : name_(name), version_(version), initialized_(false) {} |
| 61 | |||
| 62 | private: | ||
| 63 | std::string name_, version_; | ||
| 64 | bool initialized_; | ||
| 65 | }; // class ProblemSolver | ||
| 66 | |||
| 67 | /// To create a plugin, create a class that derives from | ||
| 68 | /// hpp::core::ProblemSolverPlugin and call this macro in the root namespace. | ||
| 69 | /// To load the plugin, use | ||
| 70 | /// \code | ||
| 71 | /// std::string filename; | ||
| 72 | /// hpp::core::ProblemSolver ps*; | ||
| 73 | /// hpp::core::plugin::loadPlugin (filename, ps); | ||
| 74 | /// \endcode | ||
| 75 | #define HPP_CORE_DEFINE_PLUGIN(PluginClassName) \ | ||
| 76 | extern "C" { \ | ||
| 77 | ::hpp::core::ProblemSolverPlugin* createProblemSolverPlugin() { \ | ||
| 78 | return new PluginClassName(); \ | ||
| 79 | } \ | ||
| 80 | } | ||
| 81 | |||
| 82 | namespace plugin { | ||
| 83 | /// Find the absolute path to a library named name. | ||
| 84 | /// \param name | ||
| 85 | /// \return name if it is an absolute path. | ||
| 86 | /// Otherwise, for each path in HPP_PLUGIN_DIRS environment variable, | ||
| 87 | /// look for path/hppPlugins/name. | ||
| 88 | /// Otherwise, for each path in LD_LIBRARY_PATH environment variable, | ||
| 89 | /// look for path/hppPlugins/name. | ||
| 90 | /// \throw std::invalid_argument if not valid file found. | ||
| 91 | std::string findPluginLibrary(const std::string& name); | ||
| 92 | |||
| 93 | /// Load a plugin into ProblemSolver | ||
| 94 | /// 1. Call \ref findPluginLibrary | ||
| 95 | /// 2. Call dlopen and handle errors | ||
| 96 | /// 3. ProblemSolverPlugin::initialize with \c ps | ||
| 97 | bool loadPlugin(const std::string& lib, ProblemSolverPtr_t ps); | ||
| 98 | } // namespace plugin | ||
| 99 | |||
| 100 | /// \} | ||
| 101 | |||
| 102 | } // namespace core | ||
| 103 | } // namespace hpp | ||
| 104 | |||
| 105 | #endif // HPP_CORE_PLUGIN_HH | ||
| 106 |