diff --git a/include/fc/json_rpc_connection.hpp b/include/fc/json_rpc_connection.hpp index 375bcbb..6f357b6 100644 --- a/include/fc/json_rpc_connection.hpp +++ b/include/fc/json_rpc_connection.hpp @@ -3,8 +3,16 @@ #include #include #include +#include namespace fc { namespace json { + class rpc_connection; + + struct rpc_server_function : public fc::retainable { + typedef fc::shared_ptr ptr; + virtual value call( const value& v ) = 0; + }; + namespace detail { struct pending_result : virtual public promise_base { typedef shared_ptr ptr; @@ -31,6 +39,28 @@ namespace fc { namespace json { protected: ~pending_result_impl(){} }; + + + template + struct rpc_server_function_impl : public rpc_server_function { + rpc_server_function_impl( const fc::function& f ):func(f){} + virtual value call( const value& v ) { + return value( func( fc::value_cast( v ) ) ); + } + fc::function func; + }; + + template + struct add_method_visitor { + public: + add_method_visitor( const fc::ptr& p, fc::json::rpc_connection& c ):_ptr(p){} + + template + void operator()( const char* name, fc::function& meth, Type ); + + const fc::ptr& _ptr; + fc::json::rpc_connection& _con; + }; } /** @@ -55,32 +85,36 @@ namespace fc { namespace json { /** note the life of i and o must be longer than rpc_connection's life */ void init( istream& i, ostream& o ); - /* - template - future invoke( const fc::string& method ) { - auto r = new detail::pending_result_impl(); - invoke( detail::pending_result::ptr(r), method, value(make_tuple()) ); - return promise::ptr( r, true ); - } */ - template future invoke( const fc::string& method, Args&& a = nullptr )const { auto r = new detail::pending_result_impl(); - slog( "%p", r ); typename promise::ptr rtn( r, true ); invoke( detail::pending_result::ptr(r), method, value(fc::forward(a)) ); return rtn; } + template + void add_interface( const fc::ptr& it ) { + it->template visit( detail::add_method_visitor( it, *this ) ); + } + + void add_method( const fc::string& name, const fc::json::rpc_server_function::ptr& func ); + private: void invoke( detail::pending_result::ptr&& p, const fc::string& m, const value& param )const; class impl; fc::shared_ptr my; }; + namespace detail { + template + template + void add_method_visitor::operator()( const char* name, fc::function& meth, Type ) { + _con.add_method( name, rpc_server_function::ptr( new rpc_server_function_impl(meth) ) ); + } + } // namespace detail } } // fc::json - diff --git a/include/fc/reflect.hpp b/include/fc/reflect.hpp index 469ca28..219351a 100644 --- a/include/fc/reflect.hpp +++ b/include/fc/reflect.hpp @@ -9,20 +9,12 @@ #define _FC_REFLECT_HPP_ #include -#include -//#include -#include #include #include #include -//#include -//#include #include -//#include -//#include - namespace fc { /**