irccd  3.0.3
plugin_service.hpp
1 /*
2  * plugin_service.hpp -- plugin service
3  *
4  * Copyright (c) 2013-2019 David Demelier <markand@malikania.fr>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef IRCCD_DAEMON_PLUGIN_SERVICE_HPP
20 #define IRCCD_DAEMON_PLUGIN_SERVICE_HPP
21 
27 #include <irccd/sysconfig.hpp>
28 
29 #include <cassert>
30 #include <functional>
31 #include <memory>
32 #include <string>
33 #include <string_view>
34 #include <vector>
35 
36 #include "plugin.hpp"
37 
38 namespace irccd {
39 
40 class config;
41 
42 namespace daemon {
43 
44 class bot;
45 
51 public:
55  using plugins = std::vector<std::shared_ptr<plugin>>;
56 
60  using plugin_loaders = std::vector<std::unique_ptr<plugin_loader>>;
61 
62 private:
63  bot& bot_;
64  plugins plugins_;
65  plugin_loaders loaders_;
66 
67 public:
73  plugin_service(bot& bot) noexcept;
74 
78  virtual ~plugin_service();
79 
85  auto list() const noexcept -> plugins;
86 
93  auto has(std::string_view id) const noexcept -> bool;
94 
101  auto get(std::string_view id) const noexcept -> std::shared_ptr<plugin>;
102 
110  auto require(std::string_view id) const -> std::shared_ptr<plugin>;
111 
119  void add(std::shared_ptr<plugin> plg);
120 
127  void add_loader(std::unique_ptr<plugin_loader> loader);
128 
135  auto get_options(std::string_view id) -> plugin::map;
136 
143  auto get_templates(std::string_view id) -> plugin::map;
144 
153  auto get_paths(std::string_view id) -> plugin::map;
154 
165  auto open(std::string_view id, std::string_view path) -> std::shared_ptr<plugin>;
166 
173  auto find(std::string_view id) -> std::shared_ptr<plugin>;
174 
184  void load(std::string_view id, std::string_view path = "");
185 
191  void unload(std::string_view id);
192 
199  void reload(std::string_view id);
200 
212  template <typename Func, typename... Args>
213  void exec(std::shared_ptr<plugin> plugin, Func&& fn, Args&&... args)
214  {
215  assert(plugin);
216 
217  try {
218  std::invoke(std::forward<Func>(fn), *plugin, std::forward<Args>(args)...);
219  } catch (const std::exception& ex) {
220  throw plugin_error(plugin_error::exec_error, plugin->get_id(), ex.what());
221  } catch (...) {
223  }
224  }
225 
233  template <typename Func, typename... Args>
234  void exec(std::string_view name, Func&& fn, Args&&... args)
235  {
236  auto plugin = find(name);
237 
238  if (!plugin)
240 
241  exec(std::move(plugin), std::forward<Func>(fn), std::forward<Args>(args)...);
242  }
243 
247  void clear() noexcept;
248 
254  void load(const config& cfg) noexcept;
255 };
256 
257 namespace logger {
258 
259 template <typename T>
260 struct type_traits;
261 
266 template <>
274  static auto get_category(const plugin& plugin) -> std::string_view;
275 
282  static auto get_component(const plugin& plugin) -> std::string_view;
283 };
284 
285 } // !logger
286 
287 } // !daemon
288 
289 } // !irccd
290 
291 #endif // !IRCCD_DAEMON_PLUGIN_SERVICE_HPP
irccd::daemon::plugin_service::add
void add(std::shared_ptr< plugin > plg)
irccd::daemon::plugin_service
Manage plugins.
Definition: plugin_service.hpp:50
irccd::daemon::logger::type_traits
Traits for loggable objects.
Definition: logger.hpp:78
irccd::daemon::plugin::get_id
auto get_id() const noexcept -> const std::string &
irccd::daemon::plugin_service::require
auto require(std::string_view id) const -> std::shared_ptr< plugin >
irccd::config
Read .ini configuration file for irccd.
Definition: config.hpp:39
irccd::daemon::plugin_service::open
auto open(std::string_view id, std::string_view path) -> std::shared_ptr< plugin >
irccd::daemon::plugin
Abstract plugin.
Definition: plugin.hpp:61
irccd::daemon::plugin_service::clear
void clear() noexcept
irccd::daemon::plugin_service::get_paths
auto get_paths(std::string_view id) -> plugin::map
irccd::daemon::plugin_service::~plugin_service
virtual ~plugin_service()
irccd::daemon::plugin_service::add_loader
void add_loader(std::unique_ptr< plugin_loader > loader)
irccd::daemon::plugin_service::unload
void unload(std::string_view id)
irccd::daemon::plugin_service::find
auto find(std::string_view id) -> std::shared_ptr< plugin >
irccd::daemon::plugin_service::load
void load(std::string_view id, std::string_view path="")
irccd::daemon::plugin_service::get_options
auto get_options(std::string_view id) -> plugin::map
irccd::daemon::plugin_service::list
auto list() const noexcept -> plugins
irccd::daemon::bot
Irccd main instance.
Definition: bot.hpp:58
irccd::daemon::plugin_error::not_found
@ not_found
The plugin was unable to run the function.
Definition: plugin.hpp:401
irccd::daemon::plugin_service::get_templates
auto get_templates(std::string_view id) -> plugin::map
irccd::daemon::plugin_service::reload
void reload(std::string_view id)
irccd::daemon::plugin_service::plugin_loaders
std::vector< std::unique_ptr< plugin_loader > > plugin_loaders
List of loaders.
Definition: plugin_service.hpp:60
irccd
Parent namespace.
Definition: acceptor.hpp:43
std
Definition: bot.hpp:253
irccd::daemon::plugin_error
Plugin error.
Definition: plugin.hpp:388
irccd::daemon::plugin_loader
Abstract interface for searching plugins.
Definition: plugin.hpp:328
irccd::daemon::plugin_service::get
auto get(std::string_view id) const noexcept -> std::shared_ptr< plugin >
irccd::daemon::plugin_error::exec_error
@ exec_error
The plugin is already loaded.
Definition: plugin.hpp:404
irccd::daemon::plugin_service::exec
void exec(std::shared_ptr< plugin > plugin, Func &&fn, Args &&... args)
Definition: plugin_service.hpp:213
irccd::daemon::plugin_service::plugins
std::vector< std::shared_ptr< plugin > > plugins
Map of plugins.
Definition: plugin_service.hpp:55
irccd::daemon::plugin_service::has
auto has(std::string_view id) const noexcept -> bool
irccd::daemon::plugin_service::plugin_service
plugin_service(bot &bot) noexcept
irccd::daemon::plugin_service::exec
void exec(std::string_view name, Func &&fn, Args &&... args)
Definition: plugin_service.hpp:234