MACE  1.0.0
 All Classes Namespaces Files Functions Variables Enumerations Defines
libs/rpc/include/mace/rpc/client.hpp
00001 #ifndef _MACE_RPC_CLIENT_HPP_
00002 #define _MACE_RPC_CLIENT_HPP_
00003 #include <mace/stub/ptr.hpp>
00004 #include <mace/rpc/client_interface.hpp>
00005 #include <mace/rpc/raw/tcp/connection.hpp>
00006 
00007 namespace mace { namespace rpc {  
00008 
00017   template<typename InterfaceType, typename ConnectionType = mace::rpc::raw::tcp::connection<raw_io> >
00018   class client : public mace::stub::ptr<InterfaceType, mace::rpc::client_interface< ConnectionType > > {
00019     public:
00020       typedef boost::shared_ptr<client>                     ptr;
00021       typedef mace::rpc::client_interface< ConnectionType > delegate_type;
00022 
00023       client(){}
00024 
00025       client( const client& c ):m_con(c.m_con) {
00026          delegate_type::set_vtable( *this, m_con );
00027       }
00028 
00029       client( client&& c ) {
00030         m_con = std::move(c.m_con);
00031       }
00032 
00033       bool operator!()const { return !m_con; }
00034 
00035       client& operator=( const client& c ) {
00036         if( &c != this )  {
00037             m_con = c.m_con;
00038             delegate_type::set_vtable( *this, m_con );
00039         }
00040         return *this;
00041       }
00042 
00043       client( const typename ConnectionType::ptr& c) 
00044       :m_con(c) {
00045         delegate_type::set_vtable( *this, m_con );
00046       }      
00047 
00048       template<typename T>
00049       void connect( T&& v ) {
00050         typename ConnectionType::ptr c( new ConnectionType( std::forward<T>(v) ) );
00051         m_con = c;
00052         delegate_type::set_vtable( *this, m_con );
00053       }
00054 
00055       template<typename T, typename T2>
00056       void connect( T&& v, T2&& v2 ) {
00057         typename ConnectionType::ptr c( new ConnectionType( std::forward<T>(v), std::forward<T>(v2)  ) );
00058         m_con = c;
00059         delegate_type::set_vtable( *this, m_con );
00060       }
00061 
00062       typename ConnectionType::ptr connection()const { return m_con; }
00063 
00064       void set_connection( const typename ConnectionType::ptr& c ) {
00065         m_con = c;
00066         delegate_type::set_vtable( *this, m_con );
00067       }
00068     private:
00069       typename ConnectionType::ptr m_con;
00070   };
00071 
00072 } } // mace::rpc::json
00073 
00074 #endif