Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / Utils / type_traits.hh
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
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 type_traits public header */
25
26 #ifndef HH_SENF_Utils_type_traits_
27 #define HH_SENF_Utils_type_traits_ 1
28
29 // Custom includes
30 #include <boost/type_traits/function_traits.hpp>
31 #include <boost/type_traits/remove_pointer.hpp>
32 #include <boost/type_traits/is_function.hpp>
33 #include <boost/type_traits/remove_cv.hpp>
34 #include <boost/type_traits/remove_reference.hpp>
35 #include <boost/bind.hpp>
36 #include <senf/config.hh>
37
38 #include "type_traits.mpp"
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40
41 namespace senf
42 {
43
44     /** \defgroup typetraits Type traits
45      */
46
47     ///\addtogroup typetraits
48     //\{
49
50     /** \brief Strip first parameter from function traits
51
52         This meta-function will remove the first argument from \a Traits. The return value is a new
53         function trait with one less argument.
54
55         If the function described in \a Traits does not take any arguments, it is returned
56         unchanged.
57
58         \code
59         typedef boost::function_traits<void (int, double)> traits
60         BOOST_STATIC_ASSERT(( boost::is_same<
61             senf::function_traits_remove_arg< traits >::type,
62             boost::function_traits<void (double)>
63         >::value ));
64         \endcode
65
66         \tparam Traits \c boost::function_traits instantiation
67      */
68     template < class Traits, unsigned arity = Traits::arity >
69     struct function_traits_remove_arg {};
70
71     /** \brief Get argument type from function traits
72
73         function_traits_arg<Traits, index> will return the type of the \a index-th argument from \a
74         Traits. If the function has no argument at that index, \c void is returned
75
76         \code
77         typedef boost::function_traits<void (int, double)> traits;
78         BOOST_STATIC_ASSERT(( boost:is_same<
79             senf::function_traits_arg_type< traits, 0 >::type,
80             int
81         >::value ));
82         BOOST_STATIC_ASSERT(( boost::is_same<
83             senf::function_traits_arg_type< traits, 2 >::type,
84             void
85         >::value ));
86         \endcode
87
88         \tparam Traits \c boost::function_traits instantiation
89         \tparam index 0 based argument index
90      */
91     template < class Traits, int index, bool flag = (index < Traits::arity) >
92     struct function_traits_arg_type {};
93
94 #ifndef DOXYGEN
95
96     template < class Traits, int index >
97     struct function_traits_arg_type <Traits, index, false >
98     {
99         typedef void type;
100     };
101
102 #endif
103
104     /** \brief Remove member pointer from type
105
106         This meta function will remove any type of member pointer from it's argument. Other types
107         will be returned unchanged.
108
109         \code
110         BOOST_STATIC_ASSERT(( boost::is_same<
111             senf::remove_member_pointer< int (Class::*) >::type,
112             int
113         >::value ));
114         BOOST_STATIC_ASSERT(( boost::is_same<
115             senf::remove_member_pointer< void (Class::*)(int) >::type,
116             void (int)
117         >::value ));
118         \endcode
119
120         \tparam MemberPointer type to remove member pointer from
121      */
122     template < class MemberPointer > struct remove_member_pointer
123     {
124         typedef MemberPointer type;
125     };
126
127 #ifndef DOXYGEN
128
129     template < class C, class T > struct remove_member_pointer <T (C::*) >
130     {
131         typedef T type;
132     };
133
134     template < class C, class T > struct remove_member_pointer <T (C::* const) >
135     {
136         typedef T type;
137     };
138
139 #endif
140
141     /** \brief Get class of a member pointer
142
143         Returns the class, an arbitrary member pointer belongs to. If the argument is not a member
144         pointer, void is returned.
145
146         \code
147         BOOST_STATIC_ASSERT(( boost::is_same<
148             senf::member_class< int (Class::*) >::type,
149             Class
150         >::value ));
151         BOOST_STATIC_ASSERT(( boost::is_Same<
152             senf::member_class< int * >::type,
153             void
154         >::value ));
155         \endcode
156
157         \tparam MemberPointer Type to get the member pointer class from
158      */
159     template < class MemberPointer > struct member_class
160     {
161         typedef void type;
162     };
163
164 #ifndef DOXYGEN
165
166     template < class C, class T > struct member_class <T (C::*) >
167     {
168         typedef C type;
169     };
170
171     template < class C, class T > struct member_class <T (C::* const) >
172     {
173         typedef C type;
174     };
175
176 #endif
177
178     /** \brief Remove any type of pointer from type
179
180         This meta function will remove a plain or member pointer from the given type. If \a T is
181         neither a member pointer nor an ordinary pointer, \a T will be returned unchanged.
182
183         \code
184         BOOST_STATIC_ASSERT(( boost::is_same<
185             senf::remove_any_pointer< int (Class::*) >::type,
186             int
187         >::value ));
188         BOOST_STATIC_ASSERT(( boost::is_same<
189             senf::remove_any_pointer< void (Class::*)(int) >::type,
190             void (int) > );
191         BOOST_STATIC_ASSERT(( boost::is_same<
192             senf::remove_any_pointer < int (*)() >::type,
193             int (
194         >::value ));
195         BOOST_STATIC_ASSERT(( boost::is_same<
196             senf::remove_any_pointer < int >::type,
197             int
198         >::value ));
199         \endcode
200
201         \tparam T type to remove member pointer from
202      */
203     template < class T >
204     struct remove_any_pointer
205         : public remove_member_pointer < typename boost::remove_pointer < T >::type >
206     {};
207
208     /** \brief Test object if it is a function or member-function (pointer)
209
210         is_any_function will inherit from \c boost::true_type, when \a T is a function type,
211         function pointer type or a member function pointer type. Otherwise, it will inherit from \c
212         boost::false_type.
213
214         \code
215         BOOST_STATIC_ASSERT((   senf::is_any_function< void () >::value ));
216         BOOST_STATIC_ASSERT((   senf::is_any_function< void (*)(int) >::value ));
217         BOOST_STATIC_ASSERT((   senf::is_any_function< void (Class::*)() >::value ));
218         BOOST_STATIC_ASSERT(( ! senf::is_any_function< int * >::value ));
219         \endcode
220
221         \tparam T type to test
222      */
223     template < class T >
224     struct is_any_function
225         : public boost::is_function < typename senf::remove_any_pointer < T >::type >
226     {};
227
228     /** \brief Remove reference and CV qualification from type
229
230         remove_cvref will remove all the 'ornaments' from a type as typically used to pass
231         arguments: references and any CV spec. It will thus convert a typical argument type into
232         it's basic type.
233
234         \code
235         BOOST_STATIC_ASSERT(( boost::is_same<
236             senf::remove_cvref<int const &>::type,
237             int
238         >::value ));
239         \endcode
240      */
241     template < class T >
242     struct remove_cvref
243         : public boost::remove_cv< typename boost::remove_reference<T>::type >
244     {};
245
246     /** \brief Get arity of function T
247
248         \a T may be any function like type: function, pointer to function or (pointer to)
249         member-function.
250
251         \code
252         BOOST_STATIC_ASSERT(( senf::function_arity<void (Class::*)(int)>::value == 1 ));
253         \endcode
254      */
255     template < class T >
256     struct function_arity
257         : public boost::integral_constant<
258               unsigned,
259               boost::function_traits<
260                   typename senf::remove_any_pointer<T>::type>::arity>
261     {};
262
263     /** Test object if it is any \c std::pair type
264
265         if \a Pair is any \c std::pair type, this trait will inherit from \c boost::true_type,
266         otherwise it will inherit from \c boost::false_type.
267
268         \code
269         BOOST_STATIC_ASSERT((   senf::is_pair< std::pair<int,void*> >::value ));
270         BOOST_STATIC_ASSERT(( ! senf::is_pair< void () >::value ));
271         \endcode
272      */
273     template <class Pair>
274     struct is_pair
275         : public boost::false_type
276     {};
277
278 #ifndef DOXYGEN
279     template <class First, class Second>
280     struct is_pair< std::pair<First,Second> >
281         : public boost::true_type
282     {};
283 #endif
284
285     //\}
286
287 #ifndef DOXYGEN
288
289 #   define BOOST_PP_ITERATION_PARAMS_1 (4, (0, 10,                                                \
290                                             SENF_ABSOLUTE_INCLUDE_PATH(Utils/type_traits.mpp),    \
291                                             1))
292 #   include BOOST_PP_ITERATE()
293
294 #endif
295
296 }
297
298 //-/////////////////////////////////////////////////////////////////////////////////////////////////
299 //#include "type_traits.cci"
300 //#include "type_traits.ct"
301 //#include "type_traits.cti"
302 #endif
303
304 \f
305 // Local Variables:
306 // mode: c++
307 // fill-column: 100
308 // comment-column: 40
309 // c-file-style: "senf"
310 // indent-tabs-mode: nil
311 // ispell-local-dictionary: "american"
312 // compile-command: "scons -u test"
313 // End: