I posted this question. SpongeBobFan
said the answer is reflection. I found Boost.Reflect and was wondering how the heck I can pull this off using that library or another C++ reflection library. Please explain your answer, as I cannot just glance at code and figure out what's going on. My question was this:
Ok, I have a question. Say I have this code:
int myfunc(int arg-a, int arg-b);
int mywrapperfunc(obj a, obj b);
mywrapperfunc
is supposed to wrap myfunc
. mywrapperfunc
discards the first
argument and takes the second, which is an array. I then uses the array items as
parameters. But say I don't know how many parameters myfunc
takes, nor do I know
how many items are in the array-type object (b
). How would I programatically call
myfunc
with the correct number of args?
The number of args handed over would be the same as the number of items in the
array-type object.
EDIT: arg-a
and arg-b
are supposed to come from the array-type
object. I split the object into the args.
EDIT: Ok, ok, I'm trying to wrap the Python C API with some sense involved, hiding most background jobs.
Have you looked at Reflex? This tool uses GCC-XML to create a reflection library that is similar to the one in Java.
Here's my initial take on it (I currently don't have a lot of free time to flush it out further):
// Use the Reflex library to look up "myfunc" reflectively
Type t = Type::ByName("myfunc");
// The Reflex type 'Type" has a "FunctionParameterSize()" that tells
// you how many args in a function
size_t num_params = t.FunctionParameterSize();
// Use this information to call "myfunc" with objects from "b"
If Reflex only provides the invocation capability on a member of a class, then you could make myfunc()
a static class member, and try something like this:
// Load MyClass reflectively (where MyClass contains a static member function "myfunc")
Type t = Type::ByName("MyClass");
// Find the member function "myfunc"
Member m = t.MemberByName("myfunc");
// Pack the parameters from "b" into a vector
// (Assumes "b" has indexing operators, and a "length()" function)
std::vector params(&b[0], &b[b.length()-1]);
// Invoke the method "myfunc" reflectively
m.Invoke(params);
You'll have to fiddle with this, I'm not sure if I'm doing the parameter passing correctly, etc. but hopefully this gives you some new ideas.