📜 ⬆️ ⬇️

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, our solved much faster and more elegant, you just need to want to realize the conceived idea, and be able to apply in practice the sweets that we are spoiled by the guys from WG21 .
? : #, )) ...


Prehistory


The theme of plug-ins is quite interesting because it allows you to add new functionality to the Software (software) without making changes to the program's core, but the interface for interaction should be thought out and written: Software <-> Plug-in Manager <-> Plug-ins .


I have an experience (unsuccessful) of an interview with a company that has its own plugin 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 it turned out for me is to judge you, dear habrovchane ...


Overview


Library features



pros



Minuses



Creating a plugin


 class myplugin : public micro::iplugin { public: myplugin(int ver, const std::string& nm):micro::iplugin(ver, nm) { //     subscribe<2>("sum2", [](std::any a, std::any b)-> std::any { return std::any_cast<int>(a) + std::any_cast<int>(b); }, "  " ); } }; 

Plugin loading


 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, you can use it in prode, let the more authoritative C ++ 's take a look and say their word, the sources are well documented and there are not many of them - about 1000 lines.


Link to the project


microplugins


')

Source: https://habr.com/ru/post/422401/


All Articles