I'm writing a proxy class that loads a shared library with dlopen()
and forwards its member functions to the appropriate members of the proxied class instance inside the loaded shared object behind the scenes.
For example, the shared object has a Person
class:
class Person
{
...
void setName(std::string name);
};
I've added a wrapper file that includes the person.h
header and defines the following symbol:
extern "C" {
void Person_setName(void* instancePointer, std::string name);
}
This call just forwards to the person object which is the first argument, extern "C"
to avoid the whole name mangling issue. On the client side I've written a Person
class with the same members that holds a pointer to the wrapped class and forwards all calls.
Now some question arise:
- is there a better way to instantiate and use a class from a loaded shared object? I've found some other solution that can live without the wrappers, but it's discouraged on Usenet and highly dependent on GCC and its version, and undefined behavior, so I decided against that route.
- is there a way to automate the creation of those wrappers? They are all just adding a first argument that is a pointer to an instance to each method and forward to the real thing. There has to be some template magic for that, no? Currently I'm using some macros for the job but I'd be interested in a template solution.
- Maybe there is some tool that can do such a thing automatically, like SWIG? As far as I've seen SWIG is just for exporting a C++ interface to high level languages.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…