MetaFunction.hpp - C++ function bindings
What's going on here?
- MetaFunction is a class that is used to bind free/static C++ functions and C++ member functions so that they can be invoked uniformly and from a scripting language! The basic idea is that I'll take any function and transform it into a wrapper function that returns an "any" type object, Variant, and takes an array of pointers to "any" type objects, VariantRefs. If you think about it, that's all any function call is, a block of code that takes a variable number of arguments and returns a value; the return value can be "void" and the number of arguments could be "0" but the basic concept is the same. The only issue is that member functions take an extra argument, a pointer to the instance calling the function, so I had to make my wrapper function's signature account for that with an extra argument, the calling object's instance pointer; This pointer is null for free functions.
What would I change?
- Variadic templates would greatly reduce the amount of code I had to copy/paste...ahem...write. Unfortunately, this project was required to be run in vs2010, so that wasn't an option. So I arbitrarily decided to support up to 4 arguments for functions.
- MetaFunction is a class that is used to bind free/static C++ functions and C++ member functions so that they can be invoked uniformly and from a scripting language! The basic idea is that I'll take any function and transform it into a wrapper function that returns an "any" type object, Variant, and takes an array of pointers to "any" type objects, VariantRefs. If you think about it, that's all any function call is, a block of code that takes a variable number of arguments and returns a value; the return value can be "void" and the number of arguments could be "0" but the basic concept is the same. The only issue is that member functions take an extra argument, a pointer to the instance calling the function, so I had to make my wrapper function's signature account for that with an extra argument, the calling object's instance pointer; This pointer is null for free functions.
What would I change?
- Variadic templates would greatly reduce the amount of code I had to copy/paste...ahem...write. Unfortunately, this project was required to be run in vs2010, so that wasn't an option. So I arbitrarily decided to support up to 4 arguments for functions.
MetaFunctionTest.cpp - gmock tests for MetaFunction.hpp
Are these tests?! Tests?! O_o
- Yes. I find that testing my code allows me to refactor, freely and quickly. I also seem to make less mistakes and find bugs faster. So I test all of the code that I write; well, the majority of it.
That's a lot of test code though. Why?
- Since I couldn't use the compiler to generate code for me (as explained above) I had to handwrite a lot of what the compiler could've instantiated for me; this involved a lot of copy and paste so I wanted to make sure that I didn't make any silly typos.
What could you improve?
- I could refactor some of the code in the invocation tests into the test fixture which could get rid of a few lines of code.
- Yes. I find that testing my code allows me to refactor, freely and quickly. I also seem to make less mistakes and find bugs faster. So I test all of the code that I write; well, the majority of it.
That's a lot of test code though. Why?
- Since I couldn't use the compiler to generate code for me (as explained above) I had to handwrite a lot of what the compiler could've instantiated for me; this involved a lot of copy and paste so I wanted to make sure that I didn't make any silly typos.
What could you improve?
- I could refactor some of the code in the invocation tests into the test fixture which could get rid of a few lines of code.