From: Gerd v. Egidy Date: Wed, 23 Jul 2008 17:01:15 +0000 (+0000) Subject: libt2n: (gerd) more progress with T2nSingletonWrapper (compiles but not complete... X-Git-Tag: v0.4~20 X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=commitdiff_plain;h=0ea6d528901a0eeb0fce1c2a71ae69c277798142 libt2n: (gerd) more progress with T2nSingletonWrapper (compiles but not complete yet) --- diff --git a/src/client_wrapper.hxx b/src/client_wrapper.hxx index 08cd397..0822af0 100644 --- a/src/client_wrapper.hxx +++ b/src/client_wrapper.hxx @@ -30,30 +30,36 @@ #include #include -#ifndef T2NWRAPPER_MAX_ARGS -#define T2NWRAPPER_MAX_ARGS 9 +#ifndef T2N_SINGLETON_WRAPPER_MAX_ARGS +#define T2N_SINGLETON_WRAPPER_MAX_ARGS 9 #endif namespace libt2n { -class ConnectionInitData +class ConnectionWrapper { public: - virtual std::auto_ptr createConnection()=0; + virtual client_connection* get_connection()=0; - virtual ~ConnectionInitData() - { } -}; + virtual long long get_command_timeout_usec(void) + { + return command_client::command_timeout_usec_default; + } -class TrivialHandler -{ - public: - static void handle( boost::function< void() > f) + virtual long long get_hello_timeout_usec(void) + { + return command_client::hello_timeout_usec_default; + } + + virtual void handle(command_client* stubBase, boost::function< void() > f) { f(); } + + virtual ~ConnectionWrapper() + { } }; // contains the internal stuff needed for T2nWrapper @@ -107,19 +113,20 @@ namespace detail }; } // eo namespace detail -template< - class Client, - class Handler = TrivialHandler -> -class T2nWrapper +class T2nSingletonWrapperMessages { - private: + protected: + static const char* NotInitializedMessage; +}; - std::auto_ptr conndConn; - std::auto_ptr client; +template< class Client > +class T2nSingletonWrapper : public T2nSingletonWrapperMessages +{ + private: + std::auto_ptr Stub; - static std::auto_ptr singletonObject; - static std::auto_ptr initData; + static std::auto_ptr SingletonObject; + static std::auto_ptr WrappedConnection; // create a prep-method for each possible number of parameters #define _GEN_ARG(z,n,d) Arg ## n arg ##n @@ -138,47 +145,50 @@ class T2nWrapper ); \ } // eo prep - BOOST_PP_REPEAT( BOOST_PP_ADD(T2NWRAPPER_MAX_ARGS,1) , _GEN_PREP, ~ ) + BOOST_PP_REPEAT( BOOST_PP_ADD(T2N_SINGLETON_WRAPPER_MAX_ARGS,1) , _GEN_PREP, ~ ) #undef _GEN_PREP #undef _GEN_ARG - static void ensureSingletonThere(void) + T2nSingletonWrapper() { - if (singletonObject.get() == NULL) - { - // we need to initialize - - if (initData.get() == NULL) - throw std::logic_error("T2nWrapper singleton used before setting initData"); + if (WrappedConnection.get() == NULL) + throw std::logic_error(NotInitializedMessage); - std::auto_ptr myConndConn=initData->createConnection(); - - } + Stub=std::auto_ptr(new Client(*(WrappedConnection->get_connection()), + WrappedConnection->get_command_timeout_usec(), + WrappedConnection->get_hello_timeout_usec())); } template< typename R > static typename detail::TypeWrap::type real_exec( boost::function< R(Client*) > f) { - ensureSingletonThere(); + ensure_singleton_there(); typename detail::TypeWrap::type result; - detail::Call call( boost::bind( f, &(*(singletonObject->client))), result ); - Handler::handle( call ); + detail::Call call( boost::bind( f, SingletonObject->Stub.get()), result ); + WrappedConnection->handle(SingletonObject->Stub.get(),call); return result; } public: - static void setInitData(std::auto_ptr newInitData) - { initData=newInitData; } - static ConnectionInitData* getInitData(void) - { return initData.get(); } + static void set_connection(std::auto_ptr wrappedConnection) + { + WrappedConnection=wrappedConnection; + + // reset the singleton to NULL because the singleton must be constructed with current WrappedConnection + if (SingletonObject.get() != NULL) + SingletonObject.reset(); + } + static ConnectionWrapper* get_connection(void) + { return WrappedConnection.get(); } - T2nWrapper() + static void ensure_singleton_there(void) { - client=std::auto_ptr(new Client(*conndConn)); + if (SingletonObject.get() == NULL) + SingletonObject=std::auto_ptr(new T2nSingletonWrapper()); } // create an exec-method for each possible number of parameters @@ -192,21 +202,37 @@ class T2nWrapper { \ boost::function(*p)(R(Client::*)( BOOST_PP_ENUM_PARAMS(n,Arg) ) \ BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n,Arg)) \ - = &T2nWrapper::template prep; \ + = &T2nSingletonWrapper::template prep; \ return boost::bind \ ( \ - T2nWrapper::template real_exec, \ + T2nSingletonWrapper::template real_exec, \ boost::bind( p, f BOOST_PP_COMMA_IF(n) \ BOOST_PP_ENUM(n, _GEN_PLACEHOLDER, ~ ) ) \ ); \ } // eo exec - BOOST_PP_REPEAT( BOOST_PP_ADD(T2NWRAPPER_MAX_ARGS,1), _GEN_EXEC, ~ ) + BOOST_PP_REPEAT( BOOST_PP_ADD(T2N_SINGLETON_WRAPPER_MAX_ARGS,1), _GEN_EXEC, ~ ) #undef _GEN_EXEC #undef _GEN_PLACEHOLDER }; +// create an t2n_exec-method for each possible number of parameters +#define _GEN_EXEC(z,n,d) \ + template< class Client, typename R BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n,typename Arg) > \ + static boost::function< R( BOOST_PP_ENUM_PARAMS(n,Arg) ) > t2n_exec \ + ( \ + R(Client::*f)( BOOST_PP_ENUM_PARAMS(n,Arg) ) \ + ) \ + { \ + return T2nSingletonWrapper::exec(f); \ + } // eo exec + + BOOST_PP_REPEAT( BOOST_PP_ADD(T2N_SINGLETON_WRAPPER_MAX_ARGS,1), _GEN_EXEC, ~ ) + +#undef _GEN_EXEC +#undef _GEN_PLACEHOLDER + } #endif diff --git a/src/command_client.hxx b/src/command_client.hxx index 035e270..d420102 100644 --- a/src/command_client.hxx +++ b/src/command_client.hxx @@ -28,7 +28,7 @@ namespace libt2n /// a client sending out commands to a server class command_client { - protected: + public: static const long long command_timeout_usec_default=90000000; static const long long hello_timeout_usec_default=30000000;