I'm wondering if anyone knows how to accomplish the following:
Let's say I have a bunch of data stored in SQL, lets say one of the fields could be called funcName, function name would contain data similar to "myFunction" What I'm wondering is, is there a way I can than in turn extract the function name and actually call that function?
There's a few ways I can think of to accomplish this, one is changing funcName to funcId and linking up with an array or similar, but I'm looking for something a bit more dynamic that would allow me to add the data on fly without having to update the actual source code every time I add a call to a new function assuming of course that the function already exists and is accessible via scope location we call it from.
Any help would be greatly appreciated.
Approach #1 (most complicated):
Develop plugin system consisting of several dynamically linked libraries (*.dll on win, *.so on linux), with a few public functions that will tell your application which this plugin supports, function names, and pointers. Scan for plugins on startup, build function list that will map function names to their pointers.
Expect problems with variable number of parameters.
Approach #2 (brute force): Store all functions that you should be able to call within one dynamically linked library. When function name is extracted from database, get function pointer using GetProcAddress (win32) or dlsym(?) on linux.
Expect problems with variable number of parameters. Not the fastest, and not a smartest way.
Approach #3 (most flexible): Embed lua or python interpreter in your app.
This way you'll be able to store entire functions within database and should not have problems calling one of those. You'll still have to declare list of function you'll want to be accessable.
This won't be fool/hack proof, though, it will be possible to screw up entire app from within database, if you aren't careful enough.
Setup a mapping of names to function pointers or functors at program startup, then call by name via map lookup. Can also use dynamic libraries for extensions.
In the compiler/interpreter world it's called a symbol table, which usually also holds type (classes/structs), data declarations (variables), and function descriptors (argument and return types, etc.) It can also be nested according to the scope.
You can use the text you recevied from SQL and call GetProcAddress()
this will give you a function pointer you can call (assuming you are using Windows).
For Linux, take a look at dlsym
Use a macro to define new functions that register themselves automatically.
And use it like this:
And this:
You could do it with function ptrs instead of CallableFunction object, but my syntax on that is hazy. Either way, add error checking for duplicate registrations and lookup not found.