Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hot-pluggable C++ library possible?

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.

like image 729
Nektarios Avatar asked Jun 23 '10 16:06

Nektarios


People also ask

Is Hot Plug the same as hot swap?

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.

What are hotplug devices?

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.


2 Answers

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).

like image 160
Alexander Gessler Avatar answered Sep 18 '22 11:09

Alexander Gessler


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.

like image 45
Reed Copsey Avatar answered Sep 18 '22 11:09

Reed Copsey