Another C ++ plugin framework

Introduction


Who did not visit, using the open-source libraries, the thought: “Thanks to these guys for this cool liba! Someday I will write something worthwhile and put it in public so that others can use it! ”


Yes, everyone! Or no ?..


With the advent of the standard C ++ 17, ours хотелкиare solved much faster and more elegantly, you just need to want to realize the conceived idea, and be able to apply the tidbits that the children from WG21 spoil us with .
Не слишком ли много и? Хотя: #мыжпрограммисты, для нас это норма )) ...


Prehistory


Subject plugin is quite interesting, because it allows you to make the new features in the software (software) without changing the core of the program, but it has to be thought out and written interface to: ON <-> Plugin Manager <-> Plugins .


I have an experience (unsuccessful) of an interview with a company that has its own plug-in system for embedded systems, because of the complexity of the architecture of which I filled up a test task. There are a lot of macros inside the base classes from which it is inherited, and many other things that make the programmer’s everyday life gray when using such solutions ...


At one of the moments of self-education, and reading articles on C ++ 17, it was decided to consolidate the knowledge gained when writing a plugin system that would be understandable and easy to use.


How bad or good I did it is to judge you, dear habrovchane ...


Overview


Library features


  • Plugins as services (run in separate trades)
  • Automatic unloading of unused plugins

pros


  • Ease of use
  • Asynchronous task execution
  • Header-only design

Minuses


  • If a new plugin is added to the system, then it must be compiled with the same version of the compiler as its loading manager (as well as libstdc ++) for ABI compatibility
  • Permanent caches from std :: any to the types used?
  • Check futures for validity?

Creating a plugin


classmyplugin :public micro::iplugin {
public:
  myplugin(int ver, conststd::string& nm):micro::iplugin(ver, nm) {
    // регистрация лямбды в плагине
    subscribe<2>("sum2",
      [](std::any a, std::any b)-> std::any {
        returnstd::any_cast<int>(a) + std::any_cast<int>(b);
      },
      "справка к функции"
    );
  }
};

Loading plug


std::shared_ptr<micro::plugins> manager = micro::plugins::get();
std::shared_ptr<micro::iplugin> myplugin = manager->get_plugin("myplugin");
if (myplugin && myplugin->has<2>("sum2")) {
  std::shared_future<std::any> result = myplugin->run<2>("sum2", 25, 25);
  result.wait();
  std::cout << std::any_cast<int>(result.get()) << std::endl;
}

See more detailed examples on the project page.


Instead of conclusion


I am very pleased to read informative articles on Habré in C ++ (and not only in C ++),
I hope this article will be interesting to you and at least somewhat informative.


In my humble opinion, it is possible to use it in prode, let the more authoritative C ++ 's look and say their word, the source code is well documented and there are not many of them - about 1000 lines.


Link to the project


microplugins


Also popular now: