views:

123

answers:

6

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.

+11  A: 

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 of it a bit, or look for existing implementations (some are mentioned in other responses).

Alexander Gessler
Nektarios
+2  A: 

Yes - it's certainly possible. In a previous role where we developed an 3D graphics API and applications we let the user choose the display driver "on the fly". The view had to be recreated, but the application itself didn't need to shut down.

ChrisF
+1  A: 

Though many parts of it are quite dated, Advanced C++ Programming Styles and Idioms (James Coplien) has a section on how to do such things that might be useful to read through (though I'm not sure I'd buy a copy just for this).

Jerry Coffin
+2  A: 

Check out Boost.Reflection and Boost.Extension - they were designed to address the various issues involved with attempting such things. I'm pretty sure it still doesn't let you work across compilers or versions, but it might be of aid to you.

Noah Roberts
+6  A: 

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.

Reed Copsey
A: 

And don't forget XPCOM. It's designed to be a cross-platform COM.

Jason
Firefox is based on it and a lot of people are using it everyday.Office is based on COM and more people are using it everyday.
Jason