5eee0c5f9b1308f9b25da9931455eadbfa7bb7be
[senf.git] / Utils / mpl.hh
1 // $Id$
2 //
3 // Copyright (C) 2007 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <g0dil@berlios.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 /** \file
24     \brief mpl public header */
25
26 #ifndef HH_mpl_
27 #define HH_mpl_ 1
28
29 // Custom includes
30
31 //#include "mpl.mpp"
32 #include "mpl.ih"
33 ///////////////////////////////hh.p////////////////////////////////////////
34
35 namespace senf {
36 namespace mpl {
37
38     /** \defgroup senfmpl Low-level template meta programming helpers
39      */
40
41 #   ifndef SENF_MPL_RV_ALIGNMENT
42 #       define SENF_MPL_RV_ALIGNMENT 16
43 #   endif
44
45     /** \brief Return-value type used to implement overload selection
46
47         The senf::mpl::rv type is used together with \ref SENF_MPL_RV() to select template
48         specializations based on a set of overloads:
49
50         \code
51         template <unsigned _> struct select {};
52
53         // Case 0
54         template <>
55         struct select<0> {
56             static bool const has_int_value = true;
57             void frobble();
58         };
59         template <class T>
60         senf::mpl::rv<0> select_(int, senf::mpl::take_int<T::value> * = 0);
61
62         // Case 1
63         template <>
64         struct select<1> {
65             static bool const has_int_value = false;
66             void dazzle();
67         };
68         template <class T>
69         senf::mpl::rv<1> select_(...);
70
71         template <class T>
72         struct choice : public select<SENF_MPL_RV( select_<T>(0) )> {};
73
74         struct A { static const int value = 0; };
75         struct B {};
76
77         choice<A> a; a.frobble();
78         choice<B> b; b.dazzle();
79         \endcode
80
81         The selection is always based on two components: A selector class specialized for each of
82         the possible choices and an overloaded function (only signatures, no implementation needed)
83         to provide the conditions.
84
85         When instantiatinv <tt>choice<T></tt>, we forward \a T to the <tt>select_</tt> set of
86         overloads. Because of <a href="http://en.wikipedia.org/wiki/SFINAE">SFINAE</a>, the overload
87         set will only contain those instantiations, for which template expansion does not fail.
88
89         So, if \a T has an integer \c value member, both \c select_ overloads are ok and the call
90         <tt>select_<T>(0)</tt> will choose the first (case 0) variant, since the argument \c 0 is
91         better matched by \c int than by <tt>...</tt>.
92
93         However, if \a T does not have an integer \c value member, expansion for the first overload
94         fails and the overload set only contains the second case.
95
96         \ref SENF_MPL_RV() internally uses \c sizeof to find out, \e which overload was selected
97         and returns the senf::mpl::rv-argument of that overloads return type. For this to work, the
98         \c select_ functions need not be implemented since no code is generated and \c select_ is
99         never called.
100
101         This number is than forwarded as template argument to \c select which is specialized for
102         each case. Therefore, <tt>choice<A></tt> has a \c frobble() member whereas
103         <tt>choice<B></tt> has a \c dazzle() member.
104
105         \see \ref SENF_MPL_RV
106         \ingroup senfmpl
107      */
108     template <unsigned n>
109     struct rv { 
110         char _[SENF_MPL_RV_ALIGNMENT][n+1]; 
111     };
112
113     /** \brief Get return value of overload selector
114         
115         Used together with senf::mpl::rv to implement overload selection.
116         
117         \see \ref senf::mpl::rv
118         \ingroup senfmpl
119         \hideinitializer
120      */
121 #   define SENF_MPL_RV(expr) (sizeof(expr)/SENF_MPL_RV_ALIGNMENT-1)
122
123     /** \brief Take an arbitrary unsigned integer template argument
124         
125         Used together with <a href="http://en.wikipedia.org/wiki/SFINAE">SFINAE</a>: The expression
126         <tt>take_uint<</tt> \a expr <tt>></tt> is only valid if \a expr is valid and returns a value
127         convertible to an unsigned integer.
128
129         \ingroup senfmpl
130      */
131     template <unsigned long _> struct take_uint {};
132
133     /** \brief Take an arbitrary integer template argument
134         
135         Used together with <a href="http://en.wikipedia.org/wiki/SFINAE">SFINAE</a>: The expression
136         <tt>take_int<</tt> \a expr <tt>></tt> is only valid if \a expr is valid and returns a value
137         convertible to an integer.
138
139         \ingroup senfmpl
140      */
141     template <long _> struct take_int {};
142
143     /** \brief Take an arbitrary type template argument
144         
145         Used together with <a href="http://en.wikipedia.org/wiki/SFINAE">SFINAE</a>: The expression
146         <tt>take_class<</tt> \a expr <tt>></tt> is only valid if \a expr is valid and is a type.
147
148         \ingroup senfmpl
149      */
150     template <class _> struct take_class {};
151
152     /** \brief Define MPL slot
153
154         The slot macros \ref SENF_MPL_SLOT_DEF(), \ref SENF_MPL_SLOT_SET() and \ref
155         SENF_MPL_SLOT_GET() provide a facility to get the last unsigned integer value assigned to
156         the slot before the current point in the current class.
157         \code
158         struct Foo
159         {
160             // Define SLOT slot named 'accum' initialized to 0
161             SENF_MPL_SLOT_DEF(accum, 0);
162
163             // Add 2 to 'accum'
164             SENF_MPL_SLOT_SET(accum, SENF_MPL_SLOT_GET(accum) + 2);
165
166             // Multiply 'accum' by 3
167             SENF_MPL_SLOT_SET(accum, SENF_MPL_SLOT_GET(accum) * 3);
168
169             // Define the result as a constant expression. result is now 6
170             static unsigned result = SENF_MPL_SLOT_GET(accum);
171         };
172         \endcode
173         Of course, it does not make sense to use these macros for simple arithmetic as in the
174         example. The SENF_MPL_SLOT macros allow to define macros which pass information from one
175         macro invocation to the next.
176
177         \implementation The implementation is based on __LINE__: We check backwards for a value
178             defined on a previous line. The check is limited to 80 lines backwards.
179
180         \ingroup senfmpl
181         \hideinitializer
182      */
183 #   define SENF_MPL_SLOT_DEF(name,value)                                                          \
184         template <class _>                                                                        \
185         static senf::mpl::rv<0> _SENF_MPL_SLOT_ ## name (_);                                      \
186         SENF_MPL_SLOT_SET(name,value)
187
188     /** \brief Set MPL slot
189         \see \ref SENF_MPL_SLOT_DEF()
190         \ingroup senfmpl
191         \hideinitializer
192      */
193 #   define SENF_MPL_SLOT_SET(name,value)                                                          \
194         static senf::mpl::rv<unsigned(value)+1>                                                   \
195             _SENF_MPL_SLOT_ ## name (senf::mpl::take_int<__LINE__>)
196
197     /** \brief Get current MPL slot value
198         \see \ref SENF_MPL_SLOT_DEF()
199         \ingroup senfmpl
200         \hideinitializer
201      */
202 #   define SENF_MPL_SLOT_GET(name)                                                                \
203         SENF_MPL_SLOT_I_GET(name)
204
205 }}
206
207 ///////////////////////////////hh.e////////////////////////////////////////
208 //#include "mpl.cci"
209 //#include "mpl.ct"
210 //#include "mpl.cti"
211 #endif
212
213 \f
214 // Local Variables:
215 // mode: c++
216 // fill-column: 100
217 // comment-column: 40
218 // c-file-style: "senf"
219 // indent-tabs-mode: nil
220 // ispell-local-dictionary: "american"
221 // compile-command: "scons -u test"
222 // End: