4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <g0dil@berlios.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.
24 \brief mpl public header */
30 #include "../config.hh"
34 ///////////////////////////////hh.p////////////////////////////////////////
39 /** \defgroup senfmpl Low-level template meta programming helpers
42 /** \brief Return-value type used to implement overload selection
44 The senf::mpl::rv type is used together with \ref SENF_MPL_RV() to select template
45 specializations based on a set of overloads:
48 template <unsigned _> struct select {};
53 static bool const has_int_value = true;
57 senf::mpl::rv<0> select_(int, senf::mpl::take_int<T::value> * = 0);
62 static bool const has_int_value = false;
66 senf::mpl::rv<1> select_(...);
69 struct choice : public select<SENF_MPL_RV( select_<T>(0) )> {};
71 struct A { static const int value = 0; };
74 choice<A> a; a.frobble();
75 choice<B> b; b.dazzle();
78 The selection is always based on two components: A selector class specialized for each of
79 the possible choices and an overloaded function (only signatures, no implementation needed)
80 to provide the conditions.
82 When instantiatinv <tt>choice<T></tt>, we forward \a T to the <tt>select_</tt> set of
83 overloads. Because of <a href="http://en.wikipedia.org/wiki/SFINAE">SFINAE</a>, the overload
84 set will only contain those instantiations, for which template expansion does not fail.
86 So, if \a T has an integer \c value member, both \c select_ overloads are ok and the call
87 <tt>select_<T>(0)</tt> will choose the first (case 0) variant, since the argument \c 0 is
88 better matched by \c int than by <tt>...</tt>.
90 However, if \a T does not have an integer \c value member, expansion for the first overload
91 fails and the overload set only contains the second case.
93 \ref SENF_MPL_RV() internally uses \c sizeof to find out, \e which overload was selected
94 and returns the senf::mpl::rv-argument of that overloads return type. For this to work, the
95 \c select_ functions need not be implemented since no code is generated and \c select_ is
98 This number is than forwarded as template argument to \c select which is specialized for
99 each case. Therefore, <tt>choice<A></tt> has a \c frobble() member whereas
100 <tt>choice<B></tt> has a \c dazzle() member.
102 \see \ref SENF_MPL_RV
105 template <unsigned n>
107 char _[SENF_MPL_RV_ALIGNMENT][n+1];
110 /** \brief Get return value of overload selector
112 Used together with senf::mpl::rv to implement overload selection.
114 \see \ref senf::mpl::rv
118 # define SENF_MPL_RV(expr) (sizeof(expr)/SENF_MPL_RV_ALIGNMENT-1)
120 /** \brief Take an arbitrary unsigned integer template argument
122 Used together with <a href="http://en.wikipedia.org/wiki/SFINAE">SFINAE</a>: The expression
123 <tt>take_uint<</tt> \a expr <tt>></tt> is only valid if \a expr is valid and returns a value
124 convertible to an unsigned integer.
128 template <unsigned long _> struct take_uint {};
130 /** \brief Take an arbitrary integer template argument
132 Used together with <a href="http://en.wikipedia.org/wiki/SFINAE">SFINAE</a>: The expression
133 <tt>take_int<</tt> \a expr <tt>></tt> is only valid if \a expr is valid and returns a value
134 convertible to an integer.
138 template <long _> struct take_int {};
140 /** \brief Take an arbitrary type template argument
142 Used together with <a href="http://en.wikipedia.org/wiki/SFINAE">SFINAE</a>: The expression
143 <tt>take_class<</tt> \a expr <tt>></tt> is only valid if \a expr is valid and is a type.
147 template <class _> struct take_class {};
149 /** \brief Define MPL slot
151 The slot macros \ref SENF_MPL_SLOT_DEF(), \ref SENF_MPL_SLOT_SET() and \ref
152 SENF_MPL_SLOT_GET() provide a facility to get the last unsigned integer value assigned to
153 the slot before the current point in the current class.
157 // Define SLOT slot named 'accum' initialized to 0
158 SENF_MPL_SLOT_DEF(accum, 0);
161 SENF_MPL_SLOT_SET(accum, SENF_MPL_SLOT_GET(accum) + 2);
163 // Multiply 'accum' by 3
164 SENF_MPL_SLOT_SET(accum, SENF_MPL_SLOT_GET(accum) * 3);
166 // Define the result as a constant expression. result is now 6
167 static unsigned result = SENF_MPL_SLOT_GET(accum);
170 Of course, it does not make sense to use these macros for simple arithmetic as in the
171 example. The SENF_MPL_SLOT macros allow to define macros which pass information from one
172 macro invocation to the next.
174 \implementation The implementation is based on __LINE__: We check backwards for a value
175 defined on a previous line. The check is limited to 80 lines backwards.
180 # define SENF_MPL_SLOT_DEF(name,value) \
182 static senf::mpl::rv<0> _SENF_MPL_SLOT_ ## name (_); \
183 SENF_MPL_SLOT_SET(name,value)
185 /** \brief Define MPL slot initialized to 0
187 This is like \ref SENF_MPL_SLOT_DEF() but initializes the slot to the fixed value 0. The
188 advantage over \ref SENF_MPL_SLOT_DEF() is, that this macro may be called from an include
189 file whereas all the other \\c SENF_MPL_SLOT_ macros must always be called from the relevant
192 # define SENF_MPL_SLOT_DEF_ZERO(name) \
194 static senf::mpl::rv<0> _SENF_MPL_SLOT_ ## name (_);
196 /** \brief Set MPL slot
197 \see \ref SENF_MPL_SLOT_DEF()
201 # define SENF_MPL_SLOT_SET(name,value) \
202 static senf::mpl::rv<unsigned(value)+1> \
203 _SENF_MPL_SLOT_ ## name (senf::mpl::take_int<__LINE__>)
205 /** \brief Get current MPL slot value
206 \see \ref SENF_MPL_SLOT_DEF()
210 # define SENF_MPL_SLOT_GET(name) \
211 SENF_MPL_SLOT_I_GET(name)
215 ///////////////////////////////hh.e////////////////////////////////////////
225 // comment-column: 40
226 // c-file-style: "senf"
227 // indent-tabs-mode: nil
228 // ispell-local-dictionary: "american"
229 // compile-command: "scons -u test"