MACE  1.0.0
 All Classes Namespaces Files Functions Variables Enumerations Defines
libs/rpc/include/mace/rpc/http/pending_result.hpp
00001 #ifndef _MACE_RPC_DETAIL_PENDING_RESULT_HPP_
00002 #define _MACE_RPC_DETAIL_PENDING_RESULT_HPP_
00003 #include <mace/cmt/future.hpp>
00004 
00005 namespace mace { namespace rpc { 
00006 
00007   namespace detail {
00008     class pending_result : public boost::enable_shared_from_this<pending_result> {
00009       public:
00010         typedef boost::shared_ptr<pending_result> ptr;
00011         virtual ~pending_result(){}
00012         virtual void handle_value( std::vector<char>&& d ) = 0;
00013         virtual void handle_error( int32_t code, std::vector<char>&& d ) = 0;
00014     };
00015     
00016     class raw_pending_result : public pending_result {
00017       public:
00018         raw_pending_result( const mace::cmt::promise<std::vector<char> >::ptr& p )
00019         :prom(p){}
00020         
00021         virtual void handle_value( std::vector<char>&& d ) = 0;
00022           if( prom->retain_count() > 1 ) 
00023               prom->set_value( std::forward<std::vector<char> >(d) );
00024         }
00025         virtual void handle_error( int32_t code, std::vector<char>&& d ) {
00027           //prom->
00028         }
00029       private:
00030         mace::cmt::promise<std::vector<char> >::ptr prom; 
00031     };
00032     
00033     template<typename R, typename Connection, typename IODelegate>
00034     class pending_result_impl : public pending_result {
00035       public:
00036         pending_result_impl( Connection& c, const mace::cmt::promise<R>::ptr p )
00037         :prom(p),con(c){}
00038     
00039         virtual void handle_value( std::vector<char>&& d ) = 0;
00040           if( prom->retain_count() > 1 ) {
00041               try {
00042                  R result;
00043                  IODelegate::unpack<R>( con, d );
00044                  prom->set_value( std::move( result ) );
00045               } catch ( ... ) {
00046                  prom->set_exception( boost::current_exception() );
00047               }
00048           } 
00049         }
00050         virtual void handle_error( int32_t code, std::vector<char>&& d ) {
00052           //prom->
00053         }
00054     
00055         mace::cmt::promise<R>::ptr prom;
00056         Connection&                con;
00057     };
00058 
00059   } // namespace detail
00060 
00061 } } 
00062 
00063 #endif