test code
This commit is contained in:
parent
ee9c2ac4ed
commit
cf849b8b54
1 changed files with 35 additions and 3 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
#include <fc/api.hpp>
|
#include <fc/api.hpp>
|
||||||
#include <fc/log/logger.hpp>
|
#include <fc/log/logger.hpp>
|
||||||
#include <fc/rpc/api_server.hpp>
|
//#include <fc/rpc/api_server.hpp>
|
||||||
|
|
||||||
class calculator
|
class calculator
|
||||||
{
|
{
|
||||||
|
|
@ -11,6 +11,8 @@ class calculator
|
||||||
|
|
||||||
FC_API( calculator, (add)(sub) )
|
FC_API( calculator, (add)(sub) )
|
||||||
|
|
||||||
|
using namespace fc;
|
||||||
|
|
||||||
class some_calculator
|
class some_calculator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
@ -24,6 +26,29 @@ class variant_calculator
|
||||||
double sub( fc::variant a, fc::variant b ) { return a.as_double()-b.as_double(); }
|
double sub( fc::variant a, fc::variant b ) { return a.as_double()-b.as_double(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename R, typename Arg0, typename ... Args>
|
||||||
|
//std::function<R(Args...)> bind_first_arg( const std::function<R(Arg0,Args...)>& f, Arg0 ao )
|
||||||
|
std::function<R(Args...)> bind_first_arg( const std::function<R(Arg0,Args...)>& f, Arg0 ao )
|
||||||
|
{
|
||||||
|
return [=]( Args... args ) { return f( ao, args... ); };
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename R, typename Arg0>
|
||||||
|
R call_generic( const std::function<R(Arg0)>& f, variants::const_iterator a0, variants::const_iterator e )
|
||||||
|
{
|
||||||
|
return f(a0->as<Arg0>());
|
||||||
|
}
|
||||||
|
template<typename R, typename Arg0, typename ... Args>
|
||||||
|
R call_generic( const std::function<R(Arg0,Args...)>& f, variants::const_iterator a0, variants::const_iterator e )
|
||||||
|
{
|
||||||
|
return call_generic<R,Args...>( bind_first_arg( f, a0->as<Arg0>() ), a0+1, e );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename R, typename ... Args>
|
||||||
|
std::function<variant(const fc::variants&)> to_generic( const std::function<R(Args...)>& f )
|
||||||
|
{
|
||||||
|
return [=]( const variants& args ) { return call_generic( f, args.begin(), args.end() ); };
|
||||||
|
}
|
||||||
|
|
||||||
int main( int argc, char** argv )
|
int main( int argc, char** argv )
|
||||||
{
|
{
|
||||||
|
|
@ -39,7 +64,14 @@ int main( int argc, char** argv )
|
||||||
wdump( (api_nested_calc->sub(5,4)) );
|
wdump( (api_nested_calc->sub(5,4)) );
|
||||||
wdump( (api_nested_calc->sub(5,4)) );
|
wdump( (api_nested_calc->sub(5,4)) );
|
||||||
|
|
||||||
fc::api_server server;
|
variants v = { 4, 5 };
|
||||||
server.register_api( api_calc );
|
auto g = to_generic( api_calc->add );
|
||||||
|
auto r = call_generic( api_calc->add, v.begin(), v.end() );
|
||||||
|
wdump((r));
|
||||||
|
wdump( (g(v)) );
|
||||||
|
|
||||||
|
|
||||||
|
// fc::api_server server;
|
||||||
|
// server.register_api( api_calc );
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue