updates to stream, optional, string, and actor

This commit is contained in:
Daniel Larimer 2012-11-16 12:40:28 -05:00
parent ba4eb96d88
commit 2311a1f476
6 changed files with 47 additions and 17 deletions

View file

@ -6,11 +6,29 @@ namespace fc {
namespace detail {
struct actor_member {
// TODO: expand for all method arity and constness....
template<typename R, typename C, typename A1, typename P>
static std::function<fc::future<R>(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<typename R, typename C, typename P BOOST_PP_ENUM_TRAILING_PARAMS( n, typename A)> \
static std::function<fc::future<R>( 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<R>{ \
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<typename R, typename C, typename P, typename... Args>
static std::function<fc::future<R>(Args...)> functor( P&& p, R (C::*mem_func)(Args...), fc::thread* c ) {
return [=](Args... args)->fc::future<R>{ c->async( [=]()->R { return p->*mem_func( fc::forward<Args>(args)... ); } ) };
}
template<typename R, typename C, typename P, typename... Args>
static std::function<fc::future<R>(Args...)> functor( P&& p, R (C::*mem_func)(Args...)const, fc::thread* c ){
return [=](Args... args)->fc::future<R>{ c->async( [=]()->R { return p->*mem_func( fc::forward<Args>(args)... ); } ) };
}
#endif
};
template<typename ThisPtr>
@ -34,13 +52,14 @@ namespace fc {
template<typename Interface>
class actor : public ptr<Interface, detail::actor_member> {
public:
actor(){}
template<typename InterfaceType>
actor( InterfaceType* p, fc::thread* t = &fc::thread::current() )
{
this->_vtable.reset(new detail::vtable<Interface,detail::actor_member>() );
this->_vtable->template visit<InterfaceType>( detail::actor_vtable_visitor<InterfaceType*>(t, p) );
}
};
} // namespace fc

View file

@ -54,25 +54,20 @@ namespace fc { namespace json {
template<typename InterfaceType>
class rpc_client : public ptr<InterfaceType,fc::json::detail::rpc_member> {
class rpc_client : public actor<InterfaceType> { //ptr<InterfaceType,fc::json::detail::rpc_member> {
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<InterfaceType,fc::detail::actor_member>() );
this->_vtable->template visit_other<InterfaceType>( fc::json::detail::vtable_visitor(_con) );
}
const rpc_connection& connection()const { return _con; }
private:
void init() {
this->_vtable.reset(new fc::detail::vtable<InterfaceType,fc::json::detail::rpc_member>() );
this->_vtable->template visit_other<InterfaceType>( fc::json::detail::vtable_visitor(_con) );
}
rpc_connection::ptr _con;
};

View file

@ -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<T*>(v); }
const T& operator*()const { const void* v = &_value[0]; return *static_cast<const T*>(v); }

View file

@ -8,6 +8,7 @@ namespace fc {
public:
stringstream();
stringstream( fc::string& s);
stringstream( const fc::string& s);
~stringstream();
fc::string str();

View file

@ -1,6 +1,6 @@
#ifndef _FC_STRING_HPP_
#define _FC_STRING_HPP_
#include <stdint.h>
#include <fc/utility.hpp>
/**
@ -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 );

View file

@ -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(){}