Let's say that I want that I have a generic function F
taking arguments of types Args
and returning R
. I want to wrap this function so that it fits the form:
using gen_type = void (*)(struct value *values,
size_t num_args,
struct value *return_value)
I also have functions converting to and from a generic struct value
:
template<T> T from_value(struct value);
// or have something like from_value_(struct value, int&)
// called from from_value
template<> from_value<int>(struct value);
struct value to_value(int i);
Now, can I have a function like:
template<typename F, typename ... Args>
gen_type wrap(F func) {
return [](struct value *values,
size_t num_args,
struct value *return_value) {
if (num_args != sizeof...(Args)) { /* error */ }
auto res = func(/* apply from_value<> to each argument type */)
*return_value = to_value(res);
}
}
The question is what should go on where the comment is.
(Side question: is there any way to avoid having to describe the argument types of F when invoking wrap
?)
Below is an example how it could be done using Boost.Hana with func
as a template argument.
#include <iostream>
#include <math.h>
#include <boost/hana.hpp>
namespace hana=boost::hana;
// sample value type
struct value
{
size_t content;
};
// sample conversion functions for sample value type
template <typename T>
T from_value(const value& val)
{
return T(val.content);
}
template <>
int from_value<int>(const value& val)
{
return val.content*2;
}
template <>
float from_value<float>(const value& val)
{
return val.content*3.0;
}
template <typename T>
value to_value(const T& val)
{
return value{static_cast<size_t>(round(val))};
}
// concatenate results of from_value to tuple
template <typename ...>
struct concat_values
{
};
template <typename T>
struct concat_values<T>
{
template <typename ArrT>
static auto apply(size_t index,const ArrT& arr)
{
return hana::make_tuple(from_value<T>(arr[index]));
}
};
template <typename T, typename ... Types>
struct concat_values<T,Types...>
{
template <typename ArrT>
static auto apply(size_t index,const ArrT& arr)
{
return hana::prepend(concat_values<Types...>::apply(index+1,arr),
from_value<T>(arr[index])
);
}
};
// wrap lambda
template <typename FuncT, FuncT func, typename ... Args>
auto wrap()
{
return [](value *values,
size_t num_args,
value *return_value)
{
if (num_args != sizeof...(Args)) { throw std::runtime_error("Invalid number of arguments!"); }
auto res=hana::unpack(
concat_values<Args...>::apply(0,values),
*func
);
*return_value = to_value(res);
};
}
// try it
// sample func
double sample_sum(size_t a, int b, float c)
{
return a+b*2+c*3;
}
// sample function with C-style signature that accepts wrapped function
void sample_invoke(void (*f)(value*,size_t,value*))
{
value inputs[3]={{1},{2},{3}};
value result{0};
(*f)(inputs,3,&result);
std::cout<<"Result "<<result.content<<std::endl;
}
// run
int main()
{
auto wrapped=wrap<decltype(&sample_sum),&sample_sum,size_t,int,float>();
sample_invoke(wrapped);
return 0;
}
Prints:
Result 36
See Demo.
UPDATE
Another implementation with std::index_sequence
:
// apply function
template <typename ... Args, typename FuncT, std::size_t... Idx>
auto apply_func(FuncT func,value* values,std::index_sequence<Idx...>)
{
return func(from_value<Args>(values[Idx])...);
}
// wrap lambda
template <typename FuncT, FuncT func, typename ... Args>
auto wrap()
{
return [](value *values,
size_t num_args,
value *return_value)
{
if (num_args != sizeof...(Args)) { throw std::runtime_error("Invalid number of arguments!"); }
auto res=apply_func<Args...>(*func,values,std::index_sequence_for<Args...>());
*return_value = to_value(res);
};
}
Live Demo.