Answer the question
In order to leave comments, you need to log in
Plugin system?
Hello. I am developing a project, I try to make it as modular as possible.
In connection with this, a question arose. What is the best way to support plugins?
I now have a base class for this, which all plugins inherit. Specific question: how to get a list of subclasses, from the plugin's base class. This is the approach I have used in other languages (higher level than C++).
Perhaps my approach is wrong, perhaps the plugin should register itself. Then when control reaches it, if the only entry point is main.
You can even, at the initial stage, consider not dynamic plugins (in * .so), but compiled into the binary itself. The set of plugins will be configured via ./configure when building the project.
I don't have much experience in C++. The issue of cross-platform is also of interest.
Answer the question
In order to leave comments, you need to log in
1. You design plugins as *.so, from which only one function sticks out: some “createPlugin ()” that creates and returns an object inherited from your base class.
2. Dump all plugins into one pre-known directory.
3. In the main program, go through all the files from this directory and load plugins with the dlopen() function
4. Having a handle, you can either get a pointer to your “createPlugin” function using the dlsym()
function 5. Pull the function, get the “plugin” object, save it somewhere in the list.
6. Go through the list and select the one you need by weight.
7. Unload unnecessary plugins.
You can start smoking the dock from here , and then by keywords.
If C++-way (and C++-only), then via pimpl. This is how they do it in Qt, for example.
That is, a binary-compatible class that lies in the header, arranged like this:
class Foo{
private:
unique_ptr<FooImp> impl;
public:
void DoSomethig();
};
//И .cpp файл, в котором лежит
struct FooImpl{
void ReallyDoSomethig(){doSomething;}
};
void Foo::DoSomething(){
impl->ReallyDoSomethig();
}
Foo::Foo() : impl(make_unique<FooImpl>()){
}
A little less C ++-way - an interface in a .h file,
Specific question: how to get a list of subclasses, from the plugin's base class.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question