From 2311a1f476c0b59f86b87a0b9fc9484ac75892e2 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Fri, 16 Nov 2012 12:40:28 -0500 Subject: [PATCH] updates to stream, optional, string, and actor --- include/fc/actor.hpp | 31 +++++++++++++++++++++++++------ include/fc/json_rpc_client.hpp | 15 +++++---------- include/fc/optional.hpp | 8 ++++++++ include/fc/sstream.hpp | 1 + include/fc/string.hpp | 3 ++- src/sstream.cpp | 6 ++++++ 6 files changed, 47 insertions(+), 17 deletions(-) diff --git a/include/fc/actor.hpp b/include/fc/actor.hpp index 2b26f6a..007b937 100644 --- a/include/fc/actor.hpp +++ b/include/fc/actor.hpp @@ -6,11 +6,29 @@ namespace fc { namespace detail { struct actor_member { - // TODO: expand for all method arity and constness.... - template - static std::function(A1)> functor( P&& p, R (C::*mem_func)(A1), fc::thread* t = nullptr) { - return [=](A1 a1){ return t->async( [=](){ return (p->*mem_func)(a1); } ); }; - } + #if 1 // BOOST_NO_VARIADIC_TEMPLATES + #define RPC_MEMBER_FUNCTOR(z,n,IS_CONST) \ + template \ + static std::function( BOOST_PP_ENUM_PARAMS(n,A) ) > \ + functor( P p, R (C::*mem_func)(BOOST_PP_ENUM_PARAMS(n,A)) IS_CONST, fc::thread* c = 0) { \ + return [=](BOOST_PP_ENUM_BINARY_PARAMS(n,A,a))->fc::future{ \ + return c->async( [=](){ return (p->*mem_func)(BOOST_PP_ENUM_PARAMS(n,a)); } ); }; \ + } + BOOST_PP_REPEAT( 8, RPC_MEMBER_FUNCTOR, const ) + BOOST_PP_REPEAT( 8, RPC_MEMBER_FUNCTOR, BOOST_PP_EMPTY() ) + #undef RPC_MEMBER_FUNCTOR + + #else // g++ has a bug that prevents lambdas and varidic templates from working together (G++ Bug 41933) + + template + static std::function(Args...)> functor( P&& p, R (C::*mem_func)(Args...), fc::thread* c ) { + return [=](Args... args)->fc::future{ c->async( [=]()->R { return p->*mem_func( fc::forward(args)... ); } ) }; + } + template + static std::function(Args...)> functor( P&& p, R (C::*mem_func)(Args...)const, fc::thread* c ){ + return [=](Args... args)->fc::future{ c->async( [=]()->R { return p->*mem_func( fc::forward(args)... ); } ) }; + } + #endif }; template @@ -34,13 +52,14 @@ namespace fc { template class actor : public ptr { public: + actor(){} + template actor( InterfaceType* p, fc::thread* t = &fc::thread::current() ) { this->_vtable.reset(new detail::vtable() ); this->_vtable->template visit( detail::actor_vtable_visitor(t, p) ); } - }; } // namespace fc diff --git a/include/fc/json_rpc_client.hpp b/include/fc/json_rpc_client.hpp index 826428b..59b1787 100644 --- a/include/fc/json_rpc_client.hpp +++ b/include/fc/json_rpc_client.hpp @@ -54,25 +54,20 @@ namespace fc { namespace json { template - class rpc_client : public ptr { + class rpc_client : public actor { //ptr { public: rpc_client(){} - rpc_client( const rpc_connection::ptr& c ):_con(c){ - init(); - } - rpc_client( const rpc_client& c ):_con(c._con){} + rpc_client( const rpc_connection::ptr& c ){ set_connection(c); } + //rpc_client( const rpc_client& c ):_con(c._con){} void set_connection( const rpc_connection::ptr& c ) { _con = c; - init(); + this->_vtable.reset(new fc::detail::vtable() ); + this->_vtable->template visit_other( fc::json::detail::vtable_visitor(_con) ); } const rpc_connection& connection()const { return _con; } private: - void init() { - this->_vtable.reset(new fc::detail::vtable() ); - this->_vtable->template visit_other( fc::json::detail::vtable_visitor(_con) ); - } rpc_connection::ptr _con; }; diff --git a/include/fc/optional.hpp b/include/fc/optional.hpp index 4902360..9954d6b 100644 --- a/include/fc/optional.hpp +++ b/include/fc/optional.hpp @@ -53,8 +53,16 @@ namespace fc { } // else !_valid && !o._valid == same! return *this; } + optional& operator=( optional&& o ) { + if( _valid && o._valid ) { **this = fc::move(*o); } + else if( !_valid && o._valid ) { + *this = fc::move(*o); + } + return *this; + } bool operator!()const { return !_valid; } + operator bool()const { return _valid; } T& operator*() { void* v = &_value[0]; return *static_cast(v); } const T& operator*()const { const void* v = &_value[0]; return *static_cast(v); } diff --git a/include/fc/sstream.hpp b/include/fc/sstream.hpp index 4029e1d..79baac3 100644 --- a/include/fc/sstream.hpp +++ b/include/fc/sstream.hpp @@ -8,6 +8,7 @@ namespace fc { public: stringstream(); stringstream( fc::string& s); + stringstream( const fc::string& s); ~stringstream(); fc::string str(); diff --git a/include/fc/string.hpp b/include/fc/string.hpp index c034382..6b4018a 100644 --- a/include/fc/string.hpp +++ b/include/fc/string.hpp @@ -1,6 +1,6 @@ #ifndef _FC_STRING_HPP_ #define _FC_STRING_HPP_ -#include +#include /** @@ -34,6 +34,7 @@ namespace fc { public: typedef char* iterator; typedef const char* const_iterator; + static const size_t npos = -1; string(); string( const std::string& s ); diff --git a/src/sstream.cpp b/src/sstream.cpp index bb05b13..97b3c88 100644 --- a/src/sstream.cpp +++ b/src/sstream.cpp @@ -9,6 +9,9 @@ namespace fc { impl( fc::string&s ) :ss( s ) { } + impl( const fc::string&s ) + :ss( s ) + { } impl(){} std::stringstream ss; @@ -17,6 +20,9 @@ namespace fc { stringstream::stringstream( fc::string& s ) :my(s) { } + stringstream::stringstream( const fc::string& s ) + :my(s) { + } stringstream::stringstream(){} stringstream::~stringstream(){}