a6d9ebbc024f93e219e4a67e5b6d2fe87621c4bb
[senf.git] / Socket / SocketPolicy.ih
1 // $Id$
2 //
3 // Copyright (C) 2006 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
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.
12 //
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.
17 //
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.
22
23 #ifndef IH_SocketPolicy_
24 #define IH_SocketPolicy_ 1
25
26 // Custom includes
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>
41
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
48
49 #include "GenericSockAddr.hh"
50
51 ///////////////////////////////ih.p////////////////////////////////////////
52
53 namespace senf {
54
55
56 #   define SATLIB_SOCKET_POLICIES_N BOOST_PP_SEQ_SIZE( SATLIB_SOCKET_POLICIES )
57     
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 ...
62
63     struct AddressingPolicyBase
64     {
65         virtual ~ AddressingPolicyBase() {}
66         
67         typedef GenericSockAddr Address;
68     };
69
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);
74
75     BOOST_PP_SEQ_FOR_EACH( SP_DeclareBase, , BOOST_PP_SEQ_POP_FRONT( SATLIB_SOCKET_POLICIES ) )
76
77 #   undef SP_DeclareBase
78         
79     struct SocketPolicyBase
80     {
81         virtual ~SocketPolicyBase() {}
82
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),_); }
88
89         BOOST_PP_SEQ_FOR_EACH( SP_DeclareTypedef, , SATLIB_SOCKET_POLICIES )
90
91 #       undef SP_DeclareTypedef
92     };
93
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)
97
98     template < BOOST_PP_SEQ_FOR_EACH_I( SP_TemplateArgs, , SATLIB_SOCKET_POLICIES ) >
99     struct SocketPolicy
100         : public SocketPolicyBase
101     {
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),_); }
107
108         BOOST_PP_SEQ_FOR_EACH( SP_DeclarePolicyMember, , SATLIB_SOCKET_POLICIES )
109 #   undef SP_DeclarePolicyMember
110
111         static void checkBaseOf(SocketPolicyBase const & other);
112     };
113
114 #   undef SP_TemplateArgs
115
116 namespace impl {
117
118     struct nil {};
119
120     template <int N>
121     struct SocketPolicy_rv
122     { int v[N+1]; };
123     
124     template <class Base, class Policy, int N>
125     struct MakeSocketPolicy_merge
126     {};
127
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 )
131
132 #    define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_DEC( SATLIB_SOCKET_POLICIES_N ) )
133 #    define BOOST_PP_LOCAL_MACRO(n)                                                                                             \
134         SocketPolicy_rv<n> MakeSocketPolicy_merge_(BOOST_PP_CAT( BOOST_PP_SEQ_ELEM( n, SATLIB_SOCKET_POLICIES ),Base)*);        \
135                                                                                                                                 \
136         template <class Base, class Policy>                                                                                     \
137         struct MakeSocketPolicy_merge<Base,Policy,sizeof(SocketPolicy_rv<n>)>                                                   \
138         {                                                                                                                       \
139             typedef SocketPolicy<                                                                                               \
140                BOOST_PP_SEQ_FOR_EACH_I( SP_DeclareMakeSocketPolicy_merge_member, n, SATLIB_SOCKET_POLICIES )                    \
141                > type;                                                                                                          \
142         };
143
144 #   include BOOST_PP_LOCAL_ITERATE()
145
146 #   undef SP_DeclareMakeSocketPolicy_merge_member
147
148     struct MakeSocketPolicy_fold
149     {
150         template <class Base, class Policy>
151         struct apply
152             : MakeSocketPolicy_merge<Base,
153                                      Policy,
154                                      sizeof(MakeSocketPolicy_merge_(static_cast<Policy*>(0)))>
155         {};
156
157         template <class Base>
158         struct apply<Base,nil>
159         {
160             typedef Base type;
161         };
162     };
163
164     template <class Base, class Vector>
165     struct MakeSocketPolicy_impl
166     {
167         typedef typename boost::mpl::fold< Vector, Base, MakeSocketPolicy_fold >::type policy;
168     };
169
170 #   define SP_DeclareArguments(x1,x2,n,SomePolicy)      \
171         BOOST_PP_COMMA_IF( n )                          \
172         typename Base::SomePolicy *
173
174     template <class Base>
175     SocketPolicy_rv<1> SocketPolicy_checkcompat_(
176         BOOST_PP_SEQ_FOR_EACH_I( SP_DeclareArguments, , SATLIB_SOCKET_POLICIES ) );
177
178 #   undef SP_DeclareArguments
179
180     template <class Base>
181     SocketPolicy_rv<2> SocketPolicy_checkcompat_( 
182         BOOST_PP_ENUM_PARAMS( SATLIB_SOCKET_POLICIES_N, void * BOOST_PP_INTERCEPT ) );
183
184     template <int Size>
185     struct SocketPolicy_checkcompat
186         : public boost::false_type
187     {};
188
189     template<>
190     struct SocketPolicy_checkcompat<sizeof(SocketPolicy_rv<1>)>
191         : public boost::true_type
192     {};
193
194
195 #   define SP_DeclareArguments(x1,x2,n,SomePolicy)      \
196         BOOST_PP_COMMA_IF( n )                          \
197         static_cast<typename Derived::SomePolicy *>(0)
198
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, , SATLIB_SOCKET_POLICIES ) )) >
204     {};
205
206 } // namespace impl
207
208     template < BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT( SATLIB_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( SATLIB_SOCKET_POLICIES_N, T ) > >,
212                                   impl::MakeSocketPolicy_impl< SocketPolicy<>,
213                                                                boost::mpl::vector< BOOST_PP_ENUM_PARAMS( SATLIB_SOCKET_POLICIES_N, T ) > > >::type
214     {};
215
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
222     {};
223
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* >              \
228         {};                                                                                     \
229                                                                                                 \
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> >              \
233         {};                                                                                     \
234                                                                                                 \
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 >   \
238         {};
239
240     BOOST_PP_SEQ_FOR_EACH( SP_DefineConditions, , SATLIB_SOCKET_POLICIES )
241
242 #   undef SP_DefineConditions
243
244 }
245
246 ///////////////////////////////ih.e////////////////////////////////////////
247 #endif
248
249 \f
250 // Local Variables:
251 // mode: c++
252 // c-file-style: "senf"
253 // End: