$darkmode
plugin.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
24 #pragma once
25 
26 #include <functional> // for function<>
27 #include <memory> // for unique_ptr<>
28 #include <string> // for string
29 
30 #include <cloe/core.hpp> // for Json, Error
31 #include <cloe/plugin.hpp> // for PluginManifest
32 
33 namespace cloe {
34 
39 class PluginError : public Error {
40  public:
41  PluginError(const std::string& path, const std::string& what) : Error(what), plugin_path_(path) {}
42  PluginError(const std::string& path, const char* what) : Error(what), plugin_path_(path) {}
43 
44  template <typename... Args>
45  PluginError(const std::string& path, const char* format, const Args&... args)
46  : Error(format, args...), plugin_path_(path) {}
47 
48  virtual ~PluginError() noexcept = default;
49 
55  const std::string& plugin_path() const { return plugin_path_; }
56 
57  private:
58  std::string plugin_path_;
59 };
60 
61 class Plugin {
62  public:
114  explicit Plugin(const std::string& plugin_path, const std::string& name = "");
115 
121  Plugin(const PluginManifest& m, std::function<ModelFactory*()> fn, const std::string& name = "");
122 
128  std::string path() const { return path_; }
129 
133  std::string name() const { return name_; }
134 
138  std::string type() const { return manifest_.plugin_type; }
139 
143  std::string type_version() const { return manifest_.plugin_type_version; }
144 
148  std::string required_type_version() const;
149 
153  Schema schema() const;
154 
158  bool is_builtin() const;
159 
163  bool is_type_known() const;
164 
171  bool is_compatible() const;
172 
178  template <typename F>
179  std::unique_ptr<F> make() const {
180  if (!is_compatible()) {
181  throw PluginError(path(), "cannot make factory from incompatible plugin");
182  }
183 
184  auto f = std::unique_ptr<F>(dynamic_cast<F*>(createf_()));
185  f->set_name(name());
186  return f;
187  }
188 
189  friend void to_json(Json& j, const Plugin& p) {
190  j = Json{
191  {"path", p.path()},
192  {"name", p.name()},
193  {"type", p.type()},
194  {"type_version", p.type_version()},
195  {"is_known_type", p.is_type_known()},
196  {"is_compatible", p.is_compatible()},
197  };
198  }
199 
200  private:
201  std::string path_;
202  std::string name_;
203  PluginManifest manifest_;
204  void* handle_;
205  std::function<ModelFactory*()> createf_;
206 };
207 
211 template <typename F>
212 std::shared_ptr<Plugin> make_plugin() {
213  PluginManifest manifest{
214  BASE_CLOE_FACTORY(F)::PLUGIN_TYPE,
215  BASE_CLOE_FACTORY(F)::PLUGIN_API_VERSION,
216  nullptr,
217  0,
218  };
219  auto fn = []() { return new F(); };
220  return std::make_shared<Plugin>(manifest, fn);
221 }
222 
223 } // namespace cloe
Definition: error.hpp:35
Definition: model.hpp:403
Definition: plugin.hpp:39
const std::string & plugin_path() const
Definition: plugin.hpp:55
Definition: plugin.hpp:61
bool is_compatible() const
Definition: plugin.cpp:174
Schema schema() const
Definition: plugin.cpp:167
bool is_builtin() const
std::string path() const
Definition: plugin.hpp:128
Plugin(const std::string &plugin_path, const std::string &name="")
Definition: plugin.cpp:122
std::string required_type_version() const
Definition: plugin.cpp:178
std::string type() const
Definition: plugin.hpp:138
std::string type_version() const
Definition: plugin.hpp:143
std::string name() const
Definition: plugin.hpp:133
bool is_type_known() const
Definition: plugin.cpp:172
std::unique_ptr< F > make() const
Definition: plugin.hpp:179
Definition: schema.hpp:173
std::shared_ptr< Plugin > make_plugin()
Definition: plugin.hpp:212
Definition: plugin.hpp:149
const char * plugin_type
Definition: plugin.hpp:153
const char * plugin_type_version
Definition: plugin.hpp:158