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/any.hpp>
26 #include <boost/bind.hpp>
27 #include <boost/function.hpp>
28 #include <boost/preprocessor.hpp>
31 #include <command_client.hxx>
33 #ifndef T2N_SINGLETON_WRAPPER_MAX_ARGS
34 #define T2N_SINGLETON_WRAPPER_MAX_ARGS 9
40 class ConnectionWrapper
44 virtual client_connection* get_connection()=0;
46 virtual long long get_command_timeout_usec(void)
48 return command_client::command_timeout_usec_default;
51 virtual long long get_hello_timeout_usec(void)
53 return command_client::hello_timeout_usec_default;
56 virtual void handle(command_client* stubBase, boost::function< void() > f)
61 virtual ~ConnectionWrapper()
65 // contains the internal stuff needed for T2nWrapper
69 template< typename T >
76 struct TypeWrap< void >
81 template< typename R >
84 typedef boost::function< R() > FuncType;
89 Call( FuncType f, R& res ) : function(f), result( res ) {}
100 typedef boost::function< void() > FuncType;
101 typedef TypeWrap< void >::type ResultType;
106 Call( FuncType f, ResultType& res ) : function(f), result( res ) {}
111 result= ResultType();
114 } // eo namespace detail
116 class T2nSingletonWrapperMessages
119 static const char* NotInitializedMessage;
122 template< class Client >
123 class T2nSingletonWrapper : public T2nSingletonWrapperMessages
126 std::auto_ptr<Client> Stub;
128 static std::auto_ptr<T2nSingletonWrapper> SingletonObject;
129 static std::auto_ptr<ConnectionWrapper> WrappedConnection;
131 // create a prep-method for each possible number of parameters
132 #define _GEN_ARG(z,n,d) Arg ## n arg ##n
133 #define _GEN_PREP(z,n,d) \
134 template< typename R BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n,typename Arg) > \
135 static boost::function< R(Client*) > prep \
137 R(Client::*f)( BOOST_PP_ENUM_PARAMS(n,Arg) ) \
138 BOOST_PP_COMMA_IF(n) \
139 BOOST_PP_ENUM( n, _GEN_ARG, ~ ) \
142 return boost::bind< R > \
144 f, _1 BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n,arg) \
148 BOOST_PP_REPEAT( BOOST_PP_ADD(T2N_SINGLETON_WRAPPER_MAX_ARGS,1) , _GEN_PREP, ~ )
153 T2nSingletonWrapper()
155 if (WrappedConnection.get() == NULL)
156 throw std::logic_error(NotInitializedMessage);
158 Stub=std::auto_ptr<Client>(new Client(*(WrappedConnection->get_connection()),
159 WrappedConnection->get_command_timeout_usec(),
160 WrappedConnection->get_hello_timeout_usec()));
163 template< typename R >
165 typename detail::TypeWrap<R>::type real_exec( boost::function< R(Client*) > f)
167 ensure_singleton_there();
169 typename detail::TypeWrap<R>::type result;
170 detail::Call<R> call( boost::bind( f, SingletonObject->Stub.get()), result );
171 WrappedConnection->handle(SingletonObject->Stub.get(),call);
177 static void set_connection(std::auto_ptr<ConnectionWrapper> wrappedConnection)
179 WrappedConnection=wrappedConnection;
181 // reset the singleton to NULL because the singleton must be constructed with current WrappedConnection
182 if (SingletonObject.get() != NULL)
183 SingletonObject.reset();
185 static ConnectionWrapper* get_connection(void)
186 { return WrappedConnection.get(); }
188 static void ensure_singleton_there(void)
190 if (SingletonObject.get() == NULL)
191 SingletonObject=std::auto_ptr<T2nSingletonWrapper>(new T2nSingletonWrapper());
194 // create an exec-method for each possible number of parameters
195 #define _GEN_PLACEHOLDER(z,n,d) BOOST_PP_CAT(_,BOOST_PP_ADD(n,1))
196 #define _GEN_EXEC(z,n,d) \
197 template< typename R BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n,typename Arg) > \
198 static boost::function< R( BOOST_PP_ENUM_PARAMS(n,Arg) ) > exec \
200 R(Client::*f)( BOOST_PP_ENUM_PARAMS(n,Arg) ) \
203 boost::function<R(Client*)>(*p)(R(Client::*)( BOOST_PP_ENUM_PARAMS(n,Arg) ) \
204 BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n,Arg)) \
205 = &T2nSingletonWrapper::template prep<R BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n,Arg) >; \
208 T2nSingletonWrapper::template real_exec<R>, \
209 boost::bind( p, f BOOST_PP_COMMA_IF(n) \
210 BOOST_PP_ENUM(n, _GEN_PLACEHOLDER, ~ ) ) \
214 BOOST_PP_REPEAT( BOOST_PP_ADD(T2N_SINGLETON_WRAPPER_MAX_ARGS,1), _GEN_EXEC, ~ )
217 #undef _GEN_PLACEHOLDER
221 // create an t2n_exec-method for each possible number of parameters
222 #define _GEN_EXEC(z,n,d) \
223 template< class Client, typename R BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n,typename Arg) > \
224 static boost::function< R( BOOST_PP_ENUM_PARAMS(n,Arg) ) > t2n_exec \
226 R(Client::*f)( BOOST_PP_ENUM_PARAMS(n,Arg) ) \
229 return T2nSingletonWrapper<Client>::exec(f); \
232 BOOST_PP_REPEAT( BOOST_PP_ADD(T2N_SINGLETON_WRAPPER_MAX_ARGS,1), _GEN_EXEC, ~ )
235 #undef _GEN_PLACEHOLDER