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 ///////////////////////////////ih.p////////////////////////////////////////
52 // Hide this code from doxygen
56 # define SENF_SOCKET_POLICIES_N BOOST_PP_SEQ_SIZE( SENF_SOCKET_POLICIES )
58 # define SP_DeclareAlias(x1,x2,SomePolicy) \
59 typedef BOOST_PP_CAT(SomePolicy,Base) BOOST_PP_CAT(Unspecified,SomePolicy);
61 BOOST_PP_SEQ_FOR_EACH( SP_DeclareAlias, , SENF_SOCKET_POLICIES )
63 # undef SP_DeclareAlias
65 struct SocketPolicyBase
67 virtual ~SocketPolicyBase() {}
69 # define SP_Declare(x1,x2,SomePolicy) \
70 virtual BOOST_PP_CAT(SomePolicy,Base) const & BOOST_PP_CAT(the,SomePolicy) () const = 0;
72 BOOST_PP_SEQ_FOR_EACH( SP_Declare, , SENF_SOCKET_POLICIES )
77 # define SP_TemplateArgs(x1,x2,n,SomePolicy) \
78 BOOST_PP_COMMA_IF( n ) \
79 class BOOST_PP_CAT(SomePolicy,_) = BOOST_PP_CAT(SomePolicy,Base)
81 template < BOOST_PP_SEQ_FOR_EACH_I( SP_TemplateArgs, , SENF_SOCKET_POLICIES ) >
83 : public SocketPolicyBase
85 # define SP_DeclarePolicyMember(x1,x2,SomePolicy) \
86 typedef BOOST_PP_CAT(SomePolicy,_) SomePolicy; \
87 SomePolicy BOOST_PP_CAT(BOOST_PP_CAT(the,SomePolicy),_); \
88 BOOST_PP_CAT(SomePolicy,Base) const & BOOST_PP_CAT(the,SomePolicy) () const \
89 { return BOOST_PP_CAT(BOOST_PP_CAT(the,SomePolicy),_); }
91 BOOST_PP_SEQ_FOR_EACH( SP_DeclarePolicyMember, , SENF_SOCKET_POLICIES )
92 # undef SP_DeclarePolicyMember
94 static void checkBaseOf(SocketPolicyBase const & other);
97 # undef SP_TemplateArgs
104 struct SocketPolicy_rv
107 template <class Base, class Policy, int N>
108 struct MakeSocketPolicy_merge
111 # define SP_DeclareMakeSocketPolicy_merge_member(r,n,m,SomePolicy) \
112 BOOST_PP_COMMA_IF( m ) \
113 BOOST_PP_IIF( BOOST_PP_EQUAL(n,m), Policy, typename Base::SomePolicy )
115 # define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_DEC( SENF_SOCKET_POLICIES_N ) )
116 # define BOOST_PP_LOCAL_MACRO(n) \
117 SocketPolicy_rv<n> MakeSocketPolicy_merge_(BOOST_PP_CAT( BOOST_PP_SEQ_ELEM( n, SENF_SOCKET_POLICIES ),Base)*); \
119 template <class Base, class Policy> \
120 struct MakeSocketPolicy_merge<Base,Policy,sizeof(SocketPolicy_rv<n>)> \
122 typedef SocketPolicy< \
123 BOOST_PP_SEQ_FOR_EACH_I( SP_DeclareMakeSocketPolicy_merge_member, n, SENF_SOCKET_POLICIES ) \
127 # include BOOST_PP_LOCAL_ITERATE()
129 # undef SP_DeclareMakeSocketPolicy_merge_member
131 struct MakeSocketPolicy_fold
133 template <class Base, class Policy>
135 : MakeSocketPolicy_merge<Base,
137 sizeof(MakeSocketPolicy_merge_(static_cast<Policy*>(0)))>
140 template <class Base>
141 struct apply<Base,nil>
147 template <class Base, class Vector>
148 struct MakeSocketPolicy_impl
150 typedef typename boost::mpl::fold< Vector, Base, MakeSocketPolicy_fold >::type policy;
153 # define SP_DeclareArguments(x1,x2,n,SomePolicy) \
154 BOOST_PP_COMMA_IF( n ) \
155 typename Base::SomePolicy *
157 template <class Base>
158 SocketPolicy_rv<1> SocketPolicy_checkcompat_(
159 BOOST_PP_SEQ_FOR_EACH_I( SP_DeclareArguments, , SENF_SOCKET_POLICIES ) );
161 # undef SP_DeclareArguments
163 template <class Base>
164 SocketPolicy_rv<2> SocketPolicy_checkcompat_( ... );
167 struct SocketPolicy_checkcompat
168 : public boost::false_type
172 struct SocketPolicy_checkcompat<sizeof(SocketPolicy_rv<1>)>
173 : public boost::true_type
177 # define SP_DeclareArguments(x1,x2,n,SomePolicy) \
178 BOOST_PP_COMMA_IF( n ) \
179 static_cast<typename Derived::SomePolicy *>(0)
181 template <class Base, class Derived>
182 struct SocketPolicy_compatibility
183 : public SocketPolicy_checkcompat< sizeof(
184 SocketPolicy_checkcompat_<Base>(
185 BOOST_PP_SEQ_FOR_EACH_I( SP_DeclareArguments, , SENF_SOCKET_POLICIES ) )) >
190 template < BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT( SENF_SOCKET_POLICIES_N, class T, senf::impl::nil ) >
191 class MakeSocketPolicy
192 : public boost::mpl::if_< boost::is_convertible< T0*, SocketPolicyBase* >,
193 impl::MakeSocketPolicy_impl< T0, boost::mpl::vector< BOOST_PP_ENUM_SHIFTED_PARAMS( SENF_SOCKET_POLICIES_N, T ) > >,
194 impl::MakeSocketPolicy_impl< SocketPolicy<>,
195 boost::mpl::vector< BOOST_PP_ENUM_PARAMS( SENF_SOCKET_POLICIES_N, T ) > > >::type
198 template <class BasePolicy, class DerivedPolicy>
199 struct SocketPolicyIsBaseOf
200 : public boost::mpl::if_< boost::mpl::and_< boost::is_convertible< BasePolicy*, SocketPolicyBase* >,
201 boost::is_convertible< DerivedPolicy*, SocketPolicyBase* > >,
202 impl::SocketPolicy_compatibility<BasePolicy,DerivedPolicy>,
203 boost::false_type >::type
206 # define SP_DefineConditions(x1,x2,SomePolicy) \
207 template <class Policy, class Trait> \
208 struct BOOST_PP_CAT(SomePolicy,Is) \
209 : public boost::is_convertible< typename Policy::SomePolicy*, Trait* > \
212 template <class Policy, class Trait> \
213 struct BOOST_PP_CAT(BOOST_PP_CAT(If,SomePolicy),Is) \
214 : public boost::enable_if< BOOST_PP_CAT(SomePolicy,Is)<Policy,Trait> > \
217 template <class Policy, class Trait> \
218 struct BOOST_PP_CAT(BOOST_PP_CAT(If,SomePolicy),IsNot) \
219 : public boost::enable_if_c< ! BOOST_PP_CAT(SomePolicy,Is)<Policy,Trait>::value > \
222 BOOST_PP_SEQ_FOR_EACH( SP_DefineConditions, , SENF_SOCKET_POLICIES )
224 # undef SP_DefineConditions
230 ///////////////////////////////ih.e////////////////////////////////////////
236 // c-file-style: "senf"