Dynamic library loading as part of STL. #1387
LouChiSoft
started this conversation in
Suggestions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This is probably going to be a bit of an off-topic suggestion. But honestly I don't know where better to suggest it so here goes:
One of the goals of Cpp2 is to simplify the language and the library. One of the things I think could be simplified and added to the STL is runtime loading of dynamic libraries. While not a hugely common practice, it still comes up often enough for me at work and in my own personal projects that a standardised version would make a tangible improvement to my code. I also think that with a bit of creative coding and metaclasses you could get standardised plugin library as well.
Disclaimer: Not a compiler engineer, what I suggest might not be possible as I suggest so I am open to feedback to make the idea better.
Premise:
Instead of using
dlopen/LoadLibrary
to open a library and then have to usedlsym/GetProcAddress
to find functions you should be able to do the following:Then once you have your library loaded you should be able to load functions based on the function name and signature. For example, if your library declared a function called
foo
that took two ints and return a float it could be done so as follows:That's the basic premise for the library functions. And to be honest most of that is just standard C++ that doesn't need anything from Cpp2. But I think with Cpp2 and metaclasses you could go further and support plugins. Take the following use case; a user application that supports extending the application via plugins akin to Unreal Engine or Blender. Using metaclasses I think the process of developing a plugin framework could be simplified enough that it could in theory be made part of the standard library. Warning: this is where the suggestion goes from "I'm pretty certain this can be done" to "I'm not experienced enough in compiler development to know if this is possible" so feedback is appreciated
The first step would be to provide the
@plugin
metaclass as part of the library. The second step would be to define a plugin specification in a library somewhere. E.g.Some things of note here, first off
@plugin
is essentially a fancy version of@interface
. It also usesmetafunctions
instead ofmetaclasses
which I know aren't officially supported, but they have been experimented with by people in the community. This example uses them here to define a function as the initializer AKA automatically called when the plugin is loaded and a terminator AKA automatically called when the plugin is unloaded.Then with your plugin spec defined a user can link against your library to implement their plugin:
Again, similar to the a regular
@interface
if the user were to link against your application library and try to compile their plugin without implementing eitherstart_up
orshut_down
they would get a compiler error and message telling them they need to implement it. Or IntelliSense error before that.Finally the application itself would link to the application library and use the plugin type to load plugins at runtime
If you've reached this far, I thank you for reading my ramblings and appreciate it. Like I said before, I'm not a compiler/toolchain engineer so I don't know if this is actually feasible. But I personally think it would be a large step up above C++ in it's current state. I do have other ideas for making linking/loading shared libraries in C++ a better experience as a whole but that goes from "kinda off-topic" to "wildly off-topic" for the context of Cpp2 so I shan't share them here.
Thanks for reading
Beta Was this translation helpful? Give feedback.
All reactions