PDA

View Full Version : Loading/Initializing classes



alexandernst
3rd December 2010, 09:37
I'm trying to port a project of mine from python to c++. In my python project I had a few classes that were kind of plugins. They all were in a folder called "plugins", and each plugin had it's folder and a main.py file.

I took the idea from here. http://www.evanfosmark.com/2009/07/simple-event-driven-plugin-system-in-python/
So, as you can see, each main file (aka each main file of each plugin) was "imported" (aka loaded/initialized).
Is there something similar in C++? How can I load all classes at once?

Thanks

PS: Yes, I already saw the PLUGIN_EXPORT2 & _LOAD, but I'm not really sure to want that exactly.

franz
3rd December 2010, 22:27
C++ does support dynamic loading of libraries. The Qt plug-in system is the easiest solution to what you want since it already implemented the necessary functionality. Else you will have to write library loading, function resolving and other tedious platform specific stuff yourself (which is obviously possible and a good way to learn the specifics).

alexandernst
5th December 2010, 17:16
So, after reading a lot I got to the conclusion that this is not possible as C++ doesn't have reflections like C# or Java.
I saw the factory pattern, but this is just a big if-else, and that's just ugly.

Thanks anyway!

franz
5th December 2010, 21:22
Well C++ is still a low-level language... You have to program the reflection into it ;)

wysota
6th December 2010, 00:17
I saw the factory pattern, but this is just a big if-else, and that's just ugly.
You don't need any big if-elses, it's just a matter of how you design your plugin system. The only "if" you need is the one to check if the plugin was loaded and initialized successfully.

alexandernst
6th December 2010, 12:24
So, do you have any links where I could get some examples? :)

tbscope
6th December 2010, 13:06
http://doc.qt.nokia.com/4.7/tools-plugandpaint.html