named parameter for json-rpc

This commit is contained in:
Daniel Larimer 2012-11-09 00:31:39 -05:00
parent 0e41cf98b5
commit c8b75ac96b
4 changed files with 51 additions and 8 deletions

View file

@ -39,12 +39,38 @@ namespace fc { namespace json {
~pending_result_impl(){}
};
template<typename T>
struct named_param {
typedef fc::false_type type;
static T cast( const value& v ) { return fc::value_cast<T>(v); }
template<typename X>
static value to_value( X&& x ) { return value(fc::forward<X>(x)); }
};
#define FC_JSON_NAMED_PARAMS( T ) \
namespace fc { namespace json {namespace detail { \
template<typename T> \
struct named_param< fc::tuple<T> > { \
typedef fc::true_type type; \
static tuple<T> cast( const value& v ) { return make_tuple(fc::value_cast<T>(v)); } \
template<typename X> \
static value to_value( X&& x ) { return value( x.a0 ); }\
}; \
template<typename T> \
struct named_param< fc::tuple<T&> > { \
typedef fc::true_type type; \
static tuple<T> cast( const value& v ) { return make_tuple(fc::value_cast<T>(v)); } \
template<typename X> \
static value to_value( X&& x ) { return value( x.a0 ); }\
}; \
} } }
template<typename R, typename ArgsTuple, typename Signature>
struct rpc_server_method_impl : public rpc_server_method {
rpc_server_method_impl( const std::function<Signature>& f ):func(f){}
virtual value call( const value& v ) {
return value( call_fused(func, fc::value_cast<ArgsTuple>( v ) ) );
return value( call_fused(func, named_param<ArgsTuple>::cast(v) ) );
}
std::function<Signature> func;
};
@ -89,7 +115,8 @@ namespace fc { namespace json {
future<R> invoke( const fc::string& method, Args&& a = nullptr ){
auto r = new detail::pending_result_impl<R>();
typename promise<R>::ptr rtn( r, true );
invoke( detail::pending_result::ptr(r), method, value(fc::forward<Args>(a)) );
invoke( detail::pending_result::ptr(r), method,
value(detail::named_param<typename fc::deduce<Args>::type>::to_value(a)) );
return rtn;
}

View file

@ -13,9 +13,14 @@ namespace std {
namespace fc {
template<typename T> struct remove_reference { typedef T type; };
template<typename T> struct remove_reference<T&> { typedef T type; };
template<typename T> struct remove_reference<const T&> { typedef const T type; };
template<typename T> struct remove_reference<T&&> { typedef T type; };
template<typename T> struct deduce { typedef T type; };
template<typename T> struct deduce<T&> { typedef T type; };
template<typename T> struct deduce<const T&> { typedef T type; };
template<typename T> struct deduce<T&&> { typedef T type; };
template<typename T> struct deduce<const T&&>{ typedef T type; };
template<typename T>
typename fc::remove_reference<T>::type&& move( T&& t ) { return static_cast<typename fc::remove_reference<T>::type&&>(t); }

View file

@ -5,10 +5,19 @@
#include <fc/ip.hpp>
#include <fc/iostream.hpp>
struct namep {
fc::string a;
double b;
};
FC_REFLECT( namep, (a)(b) )
FC_JSON_NAMED_PARAMS( namep )
struct test {
int add(int x){ return x+1; }
int sub(int x){ return x-1; }
int sub1(int x){ return 3; }
int namep_test(namep x){ return 3; }
int sub2(float x){ return 3; }
int sub3(double x, int y){ return x-y; }
int sub4(uint16_t x){ return 3; }
@ -19,7 +28,7 @@ struct test {
int sub9(int x){ return 3; }
};
FC_STUB( test, (add)(sub)(sub1)(sub2)(sub3)(sub4)(sub5)(sub6)(sub7)(sub8)(sub9) )
FC_STUB( test, (add)(namep_test)(sub)(sub1)(sub2)(sub3)(sub4)(sub5)(sub6)(sub7)(sub8)(sub9) )
int main( int argc, char** argv ) {
try {
@ -27,6 +36,7 @@ int main( int argc, char** argv ) {
fc::json::rpc_tcp_server serv;
serv.add_interface( t );
serv.listen(8001);
slog( "%s", fc::json::to_string( fc::json::detail::named_param<fc::tuple<namep> >::to_value(fc::tuple<namep>() ) ).c_str() );
slog("...");
{
wlog( "create new connection" );
@ -38,6 +48,7 @@ int main( int argc, char** argv ) {
fc::json::rpc_client<test> rpcc( con );
slog( "5+1=%d", rpcc->add(5).wait() );
slog( "sub3 4-5=%d", rpcc->sub3(4,5).wait() );
slog( "namep=%d", rpcc->namep_test(namep()).wait() );
}
slog( "exit serv" );
/*

View file

@ -2,7 +2,7 @@
#include <fc/exception.hpp>
#include <fc/log.hpp>
#include <fc/iostream.hpp>
#include <iostream>
//#include <iostream>
int main( int argc, char** argv ) {
try {
@ -11,10 +11,10 @@ int main( int argc, char** argv ) {
// return -1;
// }
fc::cout<<"password: ";
std::string pw;
std::cin>>pw;
// fc::string pw;
// std::cin>>pw;
fc::ssh::client c;
c.connect( "dlarimer", pw, "localhost" );
c.connect( "dlarimer", "yyRK@$p9", "localhost" );
fc::ssh::process proc = c.exec( "/bin/ls" );
while( !proc.out_stream().eof() ) {
fc::string line;