4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #ifndef IH_SocketPolicy_
24 #define IH_SocketPolicy_ 1
27 #include <boost/preprocessor/seq/for_each.hpp>
28 #include <boost/preprocessor/seq/for_each_i.hpp>
29 #include <boost/preprocessor/seq/size.hpp>
30 #include <boost/preprocessor/seq/pop_front.hpp>
31 #include <boost/preprocessor/punctuation/comma_if.hpp>
32 #include <boost/preprocessor/repetition/enum_params.hpp>
33 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
34 #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
35 #include <boost/preprocessor/cat.hpp>
36 #include <boost/preprocessor/seq/elem.hpp>
37 #include <boost/preprocessor/arithmetic/dec.hpp>
38 #include <boost/preprocessor/iteration/local.hpp>
39 #include <boost/preprocessor/control/if.hpp>
40 #include <boost/preprocessor/comparison/equal.hpp>
42 #include <boost/type_traits.hpp>
43 #include <boost/mpl/vector.hpp>
44 #include <boost/mpl/fold.hpp>
45 #include <boost/mpl/if.hpp>
46 #include <boost/mpl/and.hpp>
47 #include <boost/utility.hpp> // for enable_if
49 #include "GenericSockAddr.hh"
51 ///////////////////////////////ih.p////////////////////////////////////////
56 # define SENF_SOCKET_POLICIES_N BOOST_PP_SEQ_SIZE( SENF_SOCKET_POLICIES )
58 // This REALLY is bad ... but we just need an Address member in
59 // AddressingPolicyBase as long as ClientSocketHandle /
60 // ServerSocketHandle don't make use of enable_if for each and
61 // every member they define ...
63 struct AddressingPolicyBase
65 virtual ~ AddressingPolicyBase() {}
67 typedef GenericSockAddr Address;
70 # define SP_DeclareBase(x1,x2,SomePolicy) \
71 struct BOOST_PP_CAT(SomePolicy,Base) \
72 { virtual ~ BOOST_PP_CAT(SomePolicy,Base) () {} }; \
73 typedef BOOST_PP_CAT(SomePolicy,Base) BOOST_PP_CAT(Unspecified,SomePolicy);
75 BOOST_PP_SEQ_FOR_EACH( SP_DeclareBase, , BOOST_PP_SEQ_POP_FRONT( SENF_SOCKET_POLICIES ) )
77 # undef SP_DeclareBase
79 struct SocketPolicyBase
81 virtual ~SocketPolicyBase() {}
83 # define SP_DeclareTypedef(x1,x2,SomePolicy) \
84 typedef BOOST_PP_CAT(SomePolicy,Base) SomePolicy; \
85 BOOST_PP_CAT(SomePolicy,Base) BOOST_PP_CAT(BOOST_PP_CAT(the,SomePolicy),_); \
86 virtual BOOST_PP_CAT(SomePolicy,Base) const & BOOST_PP_CAT(the,SomePolicy) () const \
87 { return BOOST_PP_CAT(BOOST_PP_CAT(the,SomePolicy),_); }
89 BOOST_PP_SEQ_FOR_EACH( SP_DeclareTypedef, , SENF_SOCKET_POLICIES )
91 # undef SP_DeclareTypedef
94 # define SP_TemplateArgs(x1,x2,n,SomePolicy) \
95 BOOST_PP_COMMA_IF( n ) \
96 class BOOST_PP_CAT(SomePolicy,_) = BOOST_PP_CAT(SomePolicy,Base)
98 template < BOOST_PP_SEQ_FOR_EACH_I( SP_TemplateArgs, , SENF_SOCKET_POLICIES ) >
100 : public SocketPolicyBase
102 # define SP_DeclarePolicyMember(x1,x2,SomePolicy) \
103 typedef BOOST_PP_CAT(SomePolicy,_) SomePolicy; \
104 SomePolicy BOOST_PP_CAT(BOOST_PP_CAT(the,SomePolicy),_); \
105 BOOST_PP_CAT(SomePolicy,Base) const & BOOST_PP_CAT(the,SomePolicy) () const \
106 { return BOOST_PP_CAT(BOOST_PP_CAT(the,SomePolicy),_); }
108 BOOST_PP_SEQ_FOR_EACH( SP_DeclarePolicyMember, , SENF_SOCKET_POLICIES )
109 # undef SP_DeclarePolicyMember
111 static void checkBaseOf(SocketPolicyBase const & other);
114 # undef SP_TemplateArgs
121 struct SocketPolicy_rv
124 template <class Base, class Policy, int N>
125 struct MakeSocketPolicy_merge
128 # define SP_DeclareMakeSocketPolicy_merge_member(r,n,m,SomePolicy) \
129 BOOST_PP_COMMA_IF( m ) \
130 BOOST_PP_IIF( BOOST_PP_EQUAL(n,m), Policy, typename Base::SomePolicy )
132 # define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_DEC( SENF_SOCKET_POLICIES_N ) )
133 # define BOOST_PP_LOCAL_MACRO(n) \
134 SocketPolicy_rv<n> MakeSocketPolicy_merge_(BOOST_PP_CAT( BOOST_PP_SEQ_ELEM( n, SENF_SOCKET_POLICIES ),Base)*); \
136 template <class Base, class Policy> \
137 struct MakeSocketPolicy_merge<Base,Policy,sizeof(SocketPolicy_rv<n>)> \
139 typedef SocketPolicy< \
140 BOOST_PP_SEQ_FOR_EACH_I( SP_DeclareMakeSocketPolicy_merge_member, n, SENF_SOCKET_POLICIES ) \
144 # include BOOST_PP_LOCAL_ITERATE()
146 # undef SP_DeclareMakeSocketPolicy_merge_member
148 struct MakeSocketPolicy_fold
150 template <class Base, class Policy>
152 : MakeSocketPolicy_merge<Base,
154 sizeof(MakeSocketPolicy_merge_(static_cast<Policy*>(0)))>
157 template <class Base>
158 struct apply<Base,nil>
164 template <class Base, class Vector>
165 struct MakeSocketPolicy_impl
167 typedef typename boost::mpl::fold< Vector, Base, MakeSocketPolicy_fold >::type policy;
170 # define SP_DeclareArguments(x1,x2,n,SomePolicy) \
171 BOOST_PP_COMMA_IF( n ) \
172 typename Base::SomePolicy *
174 template <class Base>
175 SocketPolicy_rv<1> SocketPolicy_checkcompat_(
176 BOOST_PP_SEQ_FOR_EACH_I( SP_DeclareArguments, , SENF_SOCKET_POLICIES ) );
178 # undef SP_DeclareArguments
180 template <class Base>
181 SocketPolicy_rv<2> SocketPolicy_checkcompat_(
182 BOOST_PP_ENUM_PARAMS( SENF_SOCKET_POLICIES_N, void * BOOST_PP_INTERCEPT ) );
185 struct SocketPolicy_checkcompat
186 : public boost::false_type
190 struct SocketPolicy_checkcompat<sizeof(SocketPolicy_rv<1>)>
191 : public boost::true_type
195 # define SP_DeclareArguments(x1,x2,n,SomePolicy) \
196 BOOST_PP_COMMA_IF( n ) \
197 static_cast<typename Derived::SomePolicy *>(0)
199 template <class Base, class Derived>
200 struct SocketPolicy_compatibility
201 : public SocketPolicy_checkcompat< sizeof(
202 SocketPolicy_checkcompat_<Base>(
203 BOOST_PP_SEQ_FOR_EACH_I( SP_DeclareArguments, , SENF_SOCKET_POLICIES ) )) >
208 template < BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT( SENF_SOCKET_POLICIES_N, class T, senf::impl::nil ) >
209 class MakeSocketPolicy
210 : public boost::mpl::if_< boost::is_convertible< T0*, SocketPolicyBase* >,
211 impl::MakeSocketPolicy_impl< T0, boost::mpl::vector< BOOST_PP_ENUM_SHIFTED_PARAMS( SENF_SOCKET_POLICIES_N, T ) > >,
212 impl::MakeSocketPolicy_impl< SocketPolicy<>,
213 boost::mpl::vector< BOOST_PP_ENUM_PARAMS( SENF_SOCKET_POLICIES_N, T ) > > >::type
216 template <class BasePolicy, class DerivedPolicy>
217 struct SocketPolicyIsBaseOf
218 : public boost::mpl::if_< boost::mpl::and_< boost::is_convertible< BasePolicy*, SocketPolicyBase* >,
219 boost::is_convertible< DerivedPolicy*, SocketPolicyBase* > >,
220 impl::SocketPolicy_compatibility<BasePolicy,DerivedPolicy>,
221 boost::false_type >::type
224 # define SP_DefineConditions(x1,x2,SomePolicy) \
225 template <class Policy, class Trait> \
226 struct BOOST_PP_CAT(SomePolicy,Is) \
227 : public boost::is_convertible< typename Policy::SomePolicy*, Trait* > \
230 template <class Policy, class Trait> \
231 struct BOOST_PP_CAT(BOOST_PP_CAT(If,SomePolicy),Is) \
232 : public boost::enable_if< BOOST_PP_CAT(SomePolicy,Is)<Policy,Trait> > \
235 template <class Policy, class Trait> \
236 struct BOOST_PP_CAT(BOOST_PP_CAT(If,SomePolicy),IsNot) \
237 : public boost::enable_if_c< ! BOOST_PP_CAT(SomePolicy,Is)<Policy,Trait>::value > \
240 BOOST_PP_SEQ_FOR_EACH( SP_DefineConditions, , SENF_SOCKET_POLICIES )
242 # undef SP_DefineConditions
246 ///////////////////////////////ih.e////////////////////////////////////////
252 // c-file-style: "senf"