martin-olivier / dylib

C++ cross-platform wrapper around dynamic loading of shared libraries (dll, so, dylib)
https://conan.io/center/recipes/dylib
MIT License
293 stars 44 forks source link

dylib::get_function() is "neither here nor there" #54

Open eyalroz opened 2 years ago

eyalroz commented 2 years ago

I'm looking at this kind of line, which we would now write with dylib (2.0):

auto mod = lib.get_function<float *, const char *>("driver::factory");

I find that's not good enough. It's neither here nor there. If you want something fully dynamic, then don't even pass the type via a template, but as a string, e.g.

lib.get_function("driver::factory(float *, const char *)");

where users would manage the calling conventions themselves using a void*.

Similarly, you would also accept

lib.get_function("foo driver::factory(float *, const char *)");

for any foo, i.e. be willing to ignore the return type in the string (but also to check it if that's at all possible).

However, if you do have the namespace, name and signature at compile-time, then I would expect something like:

namespace driver {
foo (*factory)(float *, const char *) = nullptr;
} // namespace driver
lib.populate(driver::factory);

But I'm not sure the latter can be easily implemented in C++. Perhaps a macro would help here (allowing us to use both the value and the name.