Hello,
is it entirely possible to get 1) whether a function paramter has a default value and 2) what that value is?
Reason for this is that I'm writing an automatic function binding libary for my visual scripting system, so that I don't have to write the function declaration & do the paramter unpacking myself for every function I use there. This is working so far using a heavy load of template metaprogramming, which allows me to get the type of a certain function parameter:
/***************************
* getVariadicType
*
* allows access to the type of a certain variadic parameter
***************************/
template <size_t Index, typename... Types>
struct getVariadicType;
template <typename Type, typename... Types>
struct getVariadicType<0, Type, Types...>
{
using type = Type;
};
template <size_t Index, typename Type, typename... Types>
struct getVariadicType<Index, Type, Types...>
{
using type = typename getVariadicType<Index - 1, Types...>::type;
};
/***************************
* FunctionReflector
*
* takes a function pointer of any type, and allows processing its parameters via a Visitor class.
***************************/
template<typename Visitor, typename Return, typename... Args>
class FunctionReflector
{
static const size_t NUM_ARGS = sizeof...(Args);
public:
typedef Return(*FuncPointer)(Args...);
FunctionReflector(FuncPointer pFunction) : m_pFunction(pFunction)
{
}
void Visit(const Visitor& visitor) const
{
return HelpCall<NUM_ARGS, NUM_ARGS>::Call(m_pFunction, visitor);
}
private:
/***************************
* HelpCall
*
* Helper class for unpacking the variadic template args
***************************/
template<int Number, int Count, typename... CallArgs>
struct HelpCall
{
using ElementType = typename getVariadicType<Count - Number, Args...>::type;
static void Call(FuncPointer function, const Visitor& visitor)
{
visitor.Visit<ElementType, Count - Number>();
HelpCall<Number - 1, Count>::Call(function, visitor);
}
};
template<int Count>
struct HelpCall<0, Count>
{
static void Call(FuncPointer function, const Visitor& visitor)
{
}
};
FuncPointer m_pFunction;
};
Now I don't expect anyone to understand that template mindfuck, its just there if anyone needs a reference to what is possible in the current system. But it basically boils down to a Visit-method of a Visitor class being called for each function parameter:
struct DeclarationGenerator
{
DeclarationGenerator(Declaration::EntryVector& vEntries) : m_pEntries(&vEntries)
{
}
template<typename Type, size_t Element>
void Visit(void) const // this is called for each function parameter, Type is the parameter type, Element is the number of element
{
// add a new entry to the function declaration vector
m_pEntries->emplace_back(L"Test", core::generateTypeId<Type>(), core::isArray<Type>::value);
}
private:
Declaration::EntryVector* m_pEntries;
};
So only problem now is that, unlike before, I cannot supply a custom default value anymore. This would have been done by simply putting the value in the vector pushback instead, when I was creating the declaration manually:
m_pEntries->emplace_back(L"Test", 12); // default value for this parameter is "12", type is "int"
I think it should be clear what I'm looking for now: A "simple" check for default parameter presence like that:
void function(int arg1 = 10, int arg2)
{
}
std::has_default_parameter<function, 0>::value; // = true
std::has_default_parameter<function, 1>::value; // = false
std::default_parameter_value<function, 0>::value; // 10
std::default_parameter_value<function, 1>::value; // eigther undefined or 0 as default int value
eigther via the standard libary or some custom variant... any ideas? Is this even possible? If it is not, do you have any idea for how I can supply default parameters to my function declaration, while still handling the declaration generation automatically? Thanks for any help in advance!