I'm looking to "hot plug" a library of C++ code. I'm interested in having this technique work cross platform between Linux/Mac/Windows. Basically I want to have the main program #include "StateMachine.h" which defines all callable interfaces. Then at runtime and DURING EXECUTION load and unload StateMachineLibrary.a to have my application use different state machines.
One thought I have is maybe do something like write a wrapper that loads this compiled code in to my own malloc'd memory and creates function pointers in to that memory?
Motivation is that the State Machine portions of my project will be frequently changing and need recompilation, also would allow the main app to continue running with different State Machines being loaded. I'm hoping to use a "hot-pluggable" library INSTEAD OF something like Lua scripts due to some concerns, so considering that as an alternative has already been explored.
Hot plugging and hot swapping are often treated as interchangeable terms, but there is a difference. Hot plugging is the attachment of a system component while the system is running. Hot swapping is the replacement of a component while the computer is running.
Hot-plugging a device means the device is added or removed without shutting down the operating system or powering off the system. When you hot-plug a USB device, the device is immediately seen in the system's device hierarchy, as displayed in the prtconf command output.
Define a base interface and derive your implementations from it. Put these into dynamic libraries (DLL/SO) and load them at runtime. The library will just need a static factory function to deliver an instance of its implementation to you.
// shared
class Base {
public:
virtual void DoTheWork() = 0;
};
// within the DLL/SO
class Hotplugged : public Base {
public:
virtual void DoTheWork() {
std::cout<<"I got hotplugged!"<<std::endl;
}
};
extern "C" Base* CreateIt() {
return new Hotplugged();
}
// within the app (sample for Windows/MSVC)
... ::LoadLibrary("mydll");
Base* (*fpCreateIt)() = (Base*(*)())::GetProcAddress(... "CreateIt");
// call the function pointer to obtain a Base instance
Base* mybase = fpCreateIt();
// prints the above text
mybase->DoTheWork();
delete mybase;
Note: this is just a sketch. It has some flaws, for example I'm ignoring ownership semantics, and no actual checks are done if the DLL we just loaded is binary compatible with us. Think about it a bit, or look for existing implementations (some are mentioned in other responses).
This is possible. For cross-platform work (at least recompile only), you might want to look at some existing frameworks that do this.
OpenSceneGraph includes a full featured, "hot-pluggable" implementation for loading and unloading plugins.
Qt has a plugin framework, as well.
The "trick" is having a clean interface for your plugins, and just using dynamic libraries that can be loaded and unloaded. Nearly every platform (all major ones) support dynamic loading and unloading of libraries, so there is nothing that prevents this from working.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With