2015-03-09 22:50:20 +00:00
|
|
|
#include <fc/api.hpp>
|
|
|
|
|
#include <fc/log/logger.hpp>
|
2015-03-10 13:14:33 +00:00
|
|
|
//#include <fc/rpc/api_server.hpp>
|
2015-03-09 22:50:20 +00:00
|
|
|
|
|
|
|
|
class calculator
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
int32_t add( int32_t a, int32_t b ); // not implemented
|
|
|
|
|
int32_t sub( int32_t a, int32_t b ); // not implemented
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FC_API( calculator, (add)(sub) )
|
|
|
|
|
|
2015-03-10 13:14:33 +00:00
|
|
|
using namespace fc;
|
|
|
|
|
|
2015-03-09 22:50:20 +00:00
|
|
|
class some_calculator
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
int32_t add( int32_t a, int32_t b ) { return a+b; }
|
|
|
|
|
int32_t sub( int32_t a, int32_t b ) { return a-b; }
|
|
|
|
|
};
|
|
|
|
|
class variant_calculator
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
double add( 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(); }
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-10 13:14:33 +00:00
|
|
|
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() ); };
|
|
|
|
|
}
|
2015-03-09 22:50:20 +00:00
|
|
|
|
|
|
|
|
int main( int argc, char** argv )
|
|
|
|
|
{
|
|
|
|
|
some_calculator calc;
|
|
|
|
|
variant_calculator vcalc;
|
|
|
|
|
fc::api<calculator> api_calc( &calc );
|
|
|
|
|
fc::api<calculator> api_vcalc( &vcalc );
|
|
|
|
|
fc::api<calculator> api_nested_calc( api_calc );
|
|
|
|
|
wdump( (api_calc->add(5,4)) );
|
|
|
|
|
wdump( (api_calc->sub(5,4)) );
|
|
|
|
|
wdump( (api_vcalc->add(5,4)) );
|
|
|
|
|
wdump( (api_vcalc->sub(5,4)) );
|
|
|
|
|
wdump( (api_nested_calc->sub(5,4)) );
|
|
|
|
|
wdump( (api_nested_calc->sub(5,4)) );
|
|
|
|
|
|
2015-03-10 13:14:33 +00:00
|
|
|
variants v = { 4, 5 };
|
|
|
|
|
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 );
|
2015-03-09 22:50:20 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|