Background information: I'm writing an interpreter that translates all language specific functions to the following C++ function:
typedef boost::function<variant_ptr (const vector<variant_ptr> &args)> function_t;
Essentially when someone types the following in the scripting language
(add 3 4)
The symbol 'add' is looked up in the symbol table, appropriate function_t is found, 3 and 4 are converted to variants and passed to that function as arguments. So far so good. However, when I write a C++ function to implement 'add' I don't want to deal with variants. I merely want to write an 'add' function like so:
int add(int n1, int n2) { return n1 + n2; }
Problem at hand: create a registration function that takes a native C++ function and returns a function object function_t. For example:
// Function that wraps native functions in function_t
template<class fn_t>
function_t wrap_native_function(fn_t fn);
// Example of use:
int add(int n1, int n2) {return n1 + n2; }
symbol_table.put("add", wrap_native_function(add));
Now, how on earth do I make wrap_native_function generate an appropriate function object depending on the type of 'fn_t'? Boost offers a great
type traits facility that lets one discover the number of arguments to a function, the types of arguments and the type of the return value. Additionally
boost::mpl offers a large set of facilities for compile time metaprogramming. However, I can't figure out how to begin to create wrap_native_function. Boost::mpl is mind boggling. I am wondering if someone can provide a small example (or a set of examples) that would be helpful in this context and will get me started.
I hope I'm being clear. Thanks in advance!