Further documentation (mainly SocketPolicy)
[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 ///////////////////////////////ih.p////////////////////////////////////////
50
51 /// \cond disabled
52 // Hide this code from doxygen
53
54 namespace senf {
55
56 #   define SENF_SOCKET_POLICIES_N BOOST_PP_SEQ_SIZE( SENF_SOCKET_POLICIES )
57     
58 #   define SP_DeclareAlias(x1,x2,SomePolicy)                                             \
59         typedef BOOST_PP_CAT(SomePolicy,Base) BOOST_PP_CAT(Unspecified,SomePolicy);
60
61     BOOST_PP_SEQ_FOR_EACH( SP_DeclareAlias, , SENF_SOCKET_POLICIES )
62
63 #   undef SP_DeclareAlias
64         
65     struct SocketPolicyBase
66     {
67         virtual ~SocketPolicyBase() {}
68
69 #       define SP_Declare(x1,x2,SomePolicy)                                                     \
70             virtual BOOST_PP_CAT(SomePolicy,Base) const & BOOST_PP_CAT(the,SomePolicy) () const = 0;
71
72         BOOST_PP_SEQ_FOR_EACH( SP_Declare, , SENF_SOCKET_POLICIES )
73
74 #       undef SP_Declare
75     };
76
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)
80
81     template < BOOST_PP_SEQ_FOR_EACH_I( SP_TemplateArgs, , SENF_SOCKET_POLICIES ) >
82     struct SocketPolicy
83         : public SocketPolicyBase
84     {
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),_); }
90
91         BOOST_PP_SEQ_FOR_EACH( SP_DeclarePolicyMember, , SENF_SOCKET_POLICIES )
92 #   undef SP_DeclarePolicyMember
93
94         static void checkBaseOf(SocketPolicyBase const & other);
95     };
96
97 #   undef SP_TemplateArgs
98
99 namespace impl {
100
101     struct nil {};
102
103     template <int N>
104     struct SocketPolicy_rv
105     { int v[N+1]; };
106     
107     template <class Base, class Policy, int N>
108     struct MakeSocketPolicy_merge
109     {};
110
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 )
114
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)*);  \
118                                                                                                                         \
119         template <class Base, class Policy>                                                                             \
120         struct MakeSocketPolicy_merge<Base,Policy,sizeof(SocketPolicy_rv<n>)>                                           \
121         {                                                                                                               \
122             typedef SocketPolicy<                                                                                       \
123                BOOST_PP_SEQ_FOR_EACH_I( SP_DeclareMakeSocketPolicy_merge_member, n, SENF_SOCKET_POLICIES )              \
124                > type;                                                                                                  \
125         };
126
127 #   include BOOST_PP_LOCAL_ITERATE()
128
129 #   undef SP_DeclareMakeSocketPolicy_merge_member
130
131     struct MakeSocketPolicy_fold
132     {
133         template <class Base, class Policy>
134         struct apply
135             : MakeSocketPolicy_merge<Base,
136                                      Policy,
137                                      sizeof(MakeSocketPolicy_merge_(static_cast<Policy*>(0)))>
138         {};
139
140         template <class Base>
141         struct apply<Base,nil>
142         {
143             typedef Base type;
144         };
145     };
146
147     template <class Base, class Vector>
148     struct MakeSocketPolicy_impl
149     {
150         typedef typename boost::mpl::fold< Vector, Base, MakeSocketPolicy_fold >::type policy;
151     };
152
153 #   define SP_DeclareArguments(x1,x2,n,SomePolicy)      \
154         BOOST_PP_COMMA_IF( n )                          \
155         typename Base::SomePolicy *
156
157     template <class Base>
158     SocketPolicy_rv<1> SocketPolicy_checkcompat_(
159         BOOST_PP_SEQ_FOR_EACH_I( SP_DeclareArguments, , SENF_SOCKET_POLICIES ) );
160
161 #   undef SP_DeclareArguments
162
163     template <class Base>
164     SocketPolicy_rv<2> SocketPolicy_checkcompat_( ... );
165
166     template <int Size>
167     struct SocketPolicy_checkcompat
168         : public boost::false_type
169     {};
170
171     template<>
172     struct SocketPolicy_checkcompat<sizeof(SocketPolicy_rv<1>)>
173         : public boost::true_type
174     {};
175
176
177 #   define SP_DeclareArguments(x1,x2,n,SomePolicy)      \
178         BOOST_PP_COMMA_IF( n )                          \
179         static_cast<typename Derived::SomePolicy *>(0)
180
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 ) )) >
186     {};
187
188 } // namespace impl
189
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
196     {};
197
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
204     {};
205
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* >              \
210         {};                                                                                     \
211                                                                                                 \
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> >              \
215         {};                                                                                     \
216                                                                                                 \
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 >   \
220         {};
221
222     BOOST_PP_SEQ_FOR_EACH( SP_DefineConditions, , SENF_SOCKET_POLICIES )
223
224 #   undef SP_DefineConditions
225
226 }
227
228 /// \endcond
229
230 ///////////////////////////////ih.e////////////////////////////////////////
231 #endif
232
233 \f
234 // Local Variables:
235 // mode: c++
236 // c-file-style: "senf"
237 // End: