1 /***************************************************************************
2 * Copyright (C) 2008 by Gerd v. Egidy and Reinhard Pfau *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Lesser General Public License version *
7 * 2.1 as published by the Free Software Foundation. *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19 #ifndef __LIBT2N_CLIENT_WRAPPER
20 #define __LIBT2N_CLIENT_WRAPPER
24 #include <boost/config.hpp>
25 #include <boost/bind.hpp>
26 #include <boost/function.hpp>
27 #include <boost/preprocessor.hpp>
30 #include <command_client.hxx>
32 #ifndef T2N_SINGLETON_WRAPPER_MAX_ARGS
33 #define T2N_SINGLETON_WRAPPER_MAX_ARGS 9
39 class ConnectionWrapper
42 long long command_timeout_usec;
43 long long hello_timeout_usec;
46 log_level_values log_level;
47 std::ostream *logstream;
48 void set_logging_on_connection(client_connection& c);
52 : log_level(none), logstream(NULL),
53 command_timeout_usec(command_client::command_timeout_usec_default),
54 hello_timeout_usec(command_client::hello_timeout_usec_default)
57 virtual ~ConnectionWrapper()
60 virtual client_connection* get_connection()=0;
62 virtual bool handle(command_client* stubBase, boost::function< void() > f)
68 long long get_command_timeout_usec(void)
69 { return command_timeout_usec; }
71 void set_command_timeout_usec(long long _command_timeout_usec)
72 { command_timeout_usec=_command_timeout_usec; }
74 long long get_hello_timeout_usec(void)
75 { return hello_timeout_usec; }
77 void set_hello_timeout_usec(long long _hello_timeout_usec)
78 { hello_timeout_usec=_hello_timeout_usec; }
80 void set_logging(std::ostream *_logstream, log_level_values _log_level);
82 std::ostream* get_logstream(log_level_values level);
85 // contains the internal stuff needed for T2nWrapper
89 template< typename T >
96 struct TypeWrap< void >
101 template< typename R >
104 typedef boost::function< R() > FuncType;
109 Call( FuncType f, R& res ) : function(f), result( res ) {}
120 typedef boost::function< void() > FuncType;
121 typedef TypeWrap< void >::type ResultType;
126 Call( FuncType f, ResultType& res ) : function(f), result( res ) {}
131 result= ResultType();
134 } // eo namespace detail
136 class T2nSingletonWrapperMessages
139 static const char* NotInitializedMessage;
142 template< class Client >
143 class T2nSingletonWrapper : public T2nSingletonWrapperMessages
146 std::auto_ptr<Client> Stub;
148 static std::auto_ptr<T2nSingletonWrapper> SingletonObject;
149 static std::auto_ptr<ConnectionWrapper> WrappedConnection;
151 // create a prep-method for each possible number of parameters
152 #define _GEN_ARG(z,n,d) Arg ## n arg ##n
153 #define _GEN_PREP(z,n,d) \
154 template< typename R BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n,typename Arg) > \
155 static boost::function< R(Client*) > prep \
157 R(Client::*f)( BOOST_PP_ENUM_PARAMS(n,Arg) ) \
158 BOOST_PP_COMMA_IF(n) \
159 BOOST_PP_ENUM( n, _GEN_ARG, ~ ) \
162 return boost::bind< R > \
164 f, _1 BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n,arg) \
168 BOOST_PP_REPEAT( BOOST_PP_ADD(T2N_SINGLETON_WRAPPER_MAX_ARGS,1) , _GEN_PREP, ~ )
173 T2nSingletonWrapper(std::auto_ptr<Client> stub)
180 if (WrappedConnection.get() == NULL)
181 throw std::logic_error(NotInitializedMessage);
183 std::auto_ptr<Client> stub(new Client(WrappedConnection->get_connection(),
184 WrappedConnection->get_command_timeout_usec(),
185 WrappedConnection->get_hello_timeout_usec()));
187 SingletonObject=std::auto_ptr<T2nSingletonWrapper>(new T2nSingletonWrapper(stub));
190 template< typename R >
192 typename detail::TypeWrap<R>::type real_exec( boost::function< R(Client*) > f)
194 ensure_singleton_there();
196 typename detail::TypeWrap<R>::type result;
198 // bind our Client-object and the local result
199 detail::Call<R> call( boost::bind( f, SingletonObject->Stub.get()), result );
201 // let the wrapper-handler call the fully-bound function
202 if (!WrappedConnection->handle(SingletonObject->Stub.get(),call))
204 // create an result with default-constructor if the handler could not
205 // successfully do a call but no exception occured
206 result=typename detail::TypeWrap<R>::type();
213 static void set_connection(std::auto_ptr<ConnectionWrapper> wrappedConnection)
215 WrappedConnection=wrappedConnection;
217 // reset the singleton to NULL because the singleton must be constructed with current WrappedConnection
218 if (SingletonObject.get() != NULL)
219 SingletonObject.reset();
221 static ConnectionWrapper* get_connection_wrapper(void)
222 { return WrappedConnection.get(); }
224 static void ensure_singleton_there(void)
226 if (SingletonObject.get() == NULL)
230 // create an exec-method for each possible number of parameters
231 #define _GEN_PLACEHOLDER(z,n,d) BOOST_PP_CAT(_,BOOST_PP_ADD(n,1))
232 #define _GEN_EXEC(z,n,d) \
233 template< typename R BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n,typename Arg) > \
234 static boost::function< R( BOOST_PP_ENUM_PARAMS(n,Arg) ) > exec \
236 R(Client::*f)( BOOST_PP_ENUM_PARAMS(n,Arg) ) \
239 boost::function<R(Client*)>(*p)(R(Client::*)( BOOST_PP_ENUM_PARAMS(n,Arg) ) \
240 BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n,Arg)) \
241 = &T2nSingletonWrapper::template prep<R BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n,Arg) >; \
244 T2nSingletonWrapper::template real_exec<R>, \
245 boost::bind( p, f BOOST_PP_COMMA_IF(n) \
246 BOOST_PP_ENUM(n, _GEN_PLACEHOLDER, ~ ) ) \
250 BOOST_PP_REPEAT( BOOST_PP_ADD(T2N_SINGLETON_WRAPPER_MAX_ARGS,1), _GEN_EXEC, ~ )
253 #undef _GEN_PLACEHOLDER
257 // create an t2n_exec-method for each possible number of parameters
258 #define _GEN_EXEC(z,n,d) \
259 template< class Client, typename R BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n,typename Arg) > \
260 static boost::function< R( BOOST_PP_ENUM_PARAMS(n,Arg) ) > t2n_exec \
262 R(Client::*f)( BOOST_PP_ENUM_PARAMS(n,Arg) ) \
265 return T2nSingletonWrapper<Client>::exec(f); \
268 BOOST_PP_REPEAT( BOOST_PP_ADD(T2N_SINGLETON_WRAPPER_MAX_ARGS,1), _GEN_EXEC, ~ )
271 #undef _GEN_PLACEHOLDER