blob: 0236e090e4208dee6d85fd9850e00a2ec5744e16 [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001// - lambda_traits.hpp --- Boost Lambda Library ----------------------------
2//
3// Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
4//
5// Distributed under the Boost Software License, Version 1.0. (See
6// accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9// For more information, see www.boost.org
10// -------------------------------------------------------------------------
11
Jeff Thompson3d613fd2013-10-15 15:39:04 -070012#ifndef NDNBOOST_LAMBDA_LAMBDA_TRAITS_HPP
13#define NDNBOOST_LAMBDA_LAMBDA_TRAITS_HPP
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070014
15#include "ndnboost/type_traits/transform_traits.hpp"
16#include "ndnboost/type_traits/cv_traits.hpp"
17#include "ndnboost/type_traits/function_traits.hpp"
18#include "ndnboost/type_traits/object_traits.hpp"
19#include "ndnboost/tuple/tuple.hpp"
20
21namespace ndnboost {
22namespace lambda {
23
24// -- if construct ------------------------------------------------
25// Proposed by Krzysztof Czarnecki and Ulrich Eisenecker
26
27namespace detail {
28
29template <bool If, class Then, class Else> struct IF { typedef Then RET; };
30
31template <class Then, class Else> struct IF<false, Then, Else> {
32 typedef Else RET;
33};
34
35
36// An if construct that doesn't instantiate the non-matching template:
37
38// Called as:
39// IF_type<condition, A, B>::type
40// The matching template must define the typeded 'type'
41// I.e. A::type if condition is true, B::type if condition is false
42// Idea from Vesa Karvonen (from C&E as well I guess)
43template<class T>
44struct IF_type_
45{
46 typedef typename T::type type;
47};
48
49
50template<bool C, class T, class E>
51struct IF_type
52{
53 typedef typename
54 IF_type_<typename IF<C, T, E>::RET >::type type;
55};
56
57// helper that can be used to give typedef T to some type
58template <class T> struct identity_mapping { typedef T type; };
59
60// An if construct for finding an integral constant 'value'
61// Does not instantiate the non-matching branch
62// Called as IF_value<condition, A, B>::value
63// If condition is true A::value must be defined, otherwise B::value
64
65template<class T>
66struct IF_value_
67{
Jeff Thompson3d613fd2013-10-15 15:39:04 -070068 NDNBOOST_STATIC_CONSTANT(int, value = T::value);
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070069};
70
71
72template<bool C, class T, class E>
73struct IF_value
74{
Jeff Thompson3d613fd2013-10-15 15:39:04 -070075 NDNBOOST_STATIC_CONSTANT(int, value = (IF_value_<typename IF<C, T, E>::RET>::value));
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070076};
77
78
79// --------------------------------------------------------------
80
81// removes reference from other than function types:
82template<class T> class remove_reference_if_valid
83{
84
85 typedef typename ndnboost::remove_reference<T>::type plainT;
86public:
87 typedef typename IF<
88 ndnboost::is_function<plainT>::value,
89 T,
90 plainT
91 >::RET type;
92
93};
94
95
96template<class T> struct remove_reference_and_cv {
97 typedef typename ndnboost::remove_cv<
98 typename ndnboost::remove_reference<T>::type
99 >::type type;
100};
101
102
103
104// returns a reference to the element of tuple T
105template<int N, class T> struct tuple_element_as_reference {
106 typedef typename
107 ndnboost::tuples::access_traits<
108 typename ndnboost::tuples::element<N, T>::type
109 >::non_const_type type;
110};
111
112// returns the cv and reverence stripped type of a tuple element
113template<int N, class T> struct tuple_element_stripped {
114 typedef typename
115 remove_reference_and_cv<
116 typename ndnboost::tuples::element<N, T>::type
117 >::type type;
118};
119
120// is_lambda_functor -------------------------------------------------
121
122template <class T> struct is_lambda_functor_ {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700123 NDNBOOST_STATIC_CONSTANT(bool, value = false);
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700124};
125
126template <class Arg> struct is_lambda_functor_<lambda_functor<Arg> > {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700127 NDNBOOST_STATIC_CONSTANT(bool, value = true);
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700128};
129
130} // end detail
131
132
133template <class T> struct is_lambda_functor {
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700134 NDNBOOST_STATIC_CONSTANT(bool,
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700135 value =
136 detail::is_lambda_functor_<
137 typename detail::remove_reference_and_cv<T>::type
138 >::value);
139};
140
141
142namespace detail {
143
144// -- parameter_traits_ ---------------------------------------------
145
146// An internal parameter type traits class that respects
147// the reference_wrapper class.
148
149// The conversions performed are:
150// references -> compile_time_error
151// T1 -> T2,
152// reference_wrapper<T> -> T&
153// const array -> ref to const array
154// array -> ref to array
155// function -> ref to function
156
157// ------------------------------------------------------------------------
158
159template<class T1, class T2>
160struct parameter_traits_ {
161 typedef T2 type;
162};
163
164// Do not instantiate with reference types
165template<class T, class Any> struct parameter_traits_<T&, Any> {
166 typedef typename
167 generate_error<T&>::
168 parameter_traits_class_instantiated_with_reference_type type;
169};
170
171// Arrays can't be stored as plain types; convert them to references
172template<class T, int n, class Any> struct parameter_traits_<T[n], Any> {
173 typedef T (&type)[n];
174};
175
176template<class T, int n, class Any>
177struct parameter_traits_<const T[n], Any> {
178 typedef const T (&type)[n];
179};
180
181template<class T, int n, class Any>
182struct parameter_traits_<volatile T[n], Any> {
183 typedef volatile T (&type)[n];
184};
185template<class T, int n, class Any>
186struct parameter_traits_<const volatile T[n], Any> {
187 typedef const volatile T (&type)[n];
188};
189
190
191template<class T, class Any>
192struct parameter_traits_<ndnboost::reference_wrapper<T>, Any >{
193 typedef T& type;
194};
195
196template<class T, class Any>
197struct parameter_traits_<const ndnboost::reference_wrapper<T>, Any >{
198 typedef T& type;
199};
200
201template<class T, class Any>
202struct parameter_traits_<volatile ndnboost::reference_wrapper<T>, Any >{
203 typedef T& type;
204};
205
206template<class T, class Any>
207struct parameter_traits_<const volatile ndnboost::reference_wrapper<T>, Any >{
208 typedef T& type;
209};
210
211template<class Any>
212struct parameter_traits_<void, Any> {
213 typedef void type;
214};
215
216template<class Arg, class Any>
217struct parameter_traits_<lambda_functor<Arg>, Any > {
218 typedef lambda_functor<Arg> type;
219};
220
221template<class Arg, class Any>
222struct parameter_traits_<const lambda_functor<Arg>, Any > {
223 typedef lambda_functor<Arg> type;
224};
225
226// Are the volatile versions needed?
227template<class Arg, class Any>
228struct parameter_traits_<volatile lambda_functor<Arg>, Any > {
229 typedef lambda_functor<Arg> type;
230};
231
232template<class Arg, class Any>
233struct parameter_traits_<const volatile lambda_functor<Arg>, Any > {
234 typedef lambda_functor<Arg> type;
235};
236
237} // end namespace detail
238
239
240// ------------------------------------------------------------------------
241// traits classes for lambda expressions (bind functions, operators ...)
242
243// must be instantiated with non-reference types
244
245// The default is const plain type -------------------------
246// const T -> const T,
247// T -> const T,
248// references -> compile_time_error
249// reference_wrapper<T> -> T&
250// array -> const ref array
251template<class T>
252struct const_copy_argument {
253 typedef typename
254 detail::parameter_traits_<
255 T,
256 typename detail::IF<ndnboost::is_function<T>::value, T&, const T>::RET
257 >::type type;
258};
259
260// T may be a function type. Without the IF test, const would be added
261// to a function type, which is illegal.
262
263// all arrays are converted to const.
264// This traits template is used for 'const T&' parameter passing
265// and thus the knowledge of the potential
266// non-constness of an actual argument is lost.
267template<class T, int n> struct const_copy_argument <T[n]> {
268 typedef const T (&type)[n];
269};
270template<class T, int n> struct const_copy_argument <volatile T[n]> {
271 typedef const volatile T (&type)[n];
272};
273
274template<class T>
275struct const_copy_argument<T&> {};
276// do not instantiate with references
277 // typedef typename detail::generate_error<T&>::references_not_allowed type;
278
279
280template<>
281struct const_copy_argument<void> {
282 typedef void type;
283};
284
285
286// Does the same as const_copy_argument, but passes references through as such
287template<class T>
288struct bound_argument_conversion {
289 typedef typename const_copy_argument<T>::type type;
290};
291
292template<class T>
293struct bound_argument_conversion<T&> {
294 typedef T& type;
295};
296
297// The default is non-const reference -------------------------
298// const T -> const T&,
299// T -> T&,
300// references -> compile_time_error
301// reference_wrapper<T> -> T&
302template<class T>
303struct reference_argument {
304 typedef typename detail::parameter_traits_<T, T&>::type type;
305};
306
307template<class T>
308struct reference_argument<T&> {
309 typedef typename detail::generate_error<T&>::references_not_allowed type;
310};
311
312template<class Arg>
313struct reference_argument<lambda_functor<Arg> > {
314 typedef lambda_functor<Arg> type;
315};
316
317template<class Arg>
318struct reference_argument<const lambda_functor<Arg> > {
319 typedef lambda_functor<Arg> type;
320};
321
322// Are the volatile versions needed?
323template<class Arg>
324struct reference_argument<volatile lambda_functor<Arg> > {
325 typedef lambda_functor<Arg> type;
326};
327
328template<class Arg>
329struct reference_argument<const volatile lambda_functor<Arg> > {
330 typedef lambda_functor<Arg> type;
331};
332
333template<>
334struct reference_argument<void> {
335 typedef void type;
336};
337
338namespace detail {
339
340// Array to pointer conversion
341template <class T>
342struct array_to_pointer {
343 typedef T type;
344};
345
346template <class T, int N>
347struct array_to_pointer <const T[N]> {
348 typedef const T* type;
349};
350template <class T, int N>
351struct array_to_pointer <T[N]> {
352 typedef T* type;
353};
354
355template <class T, int N>
356struct array_to_pointer <const T (&) [N]> {
357 typedef const T* type;
358};
359template <class T, int N>
360struct array_to_pointer <T (&) [N]> {
361 typedef T* type;
362};
363
364
365// ---------------------------------------------------------------------------
366// The call_traits for bind
367// Respects the reference_wrapper class.
368
369// These templates are used outside of bind functions as well.
370// the bind_tuple_mapper provides a shorter notation for default
371// bound argument storing semantics, if all arguments are treated
372// uniformly.
373
374// from template<class T> foo(const T& t) : bind_traits<const T>::type
375// from template<class T> foo(T& t) : bind_traits<T>::type
376
377// Conversions:
378// T -> const T,
379// cv T -> cv T,
380// T& -> T&
381// reference_wrapper<T> -> T&
382// const reference_wrapper<T> -> T&
383// array -> const ref array
384
385// make bound arguments const, this is a deliberate design choice, the
386// purpose is to prevent side effects to bound arguments that are stored
387// as copies
388template<class T>
389struct bind_traits {
390 typedef const T type;
391};
392
393template<class T>
394struct bind_traits<T&> {
395 typedef T& type;
396};
397
398// null_types are an exception, we always want to store them as non const
399// so that other templates can assume that null_type is always without const
400template<>
401struct bind_traits<null_type> {
402 typedef null_type type;
403};
404
405// the bind_tuple_mapper, bind_type_generators may
406// introduce const to null_type
407template<>
408struct bind_traits<const null_type> {
409 typedef null_type type;
410};
411
412// Arrays can't be stored as plain types; convert them to references.
413// All arrays are converted to const. This is because bind takes its
414// parameters as const T& and thus the knowledge of the potential
415// non-constness of actual argument is lost.
416template<class T, int n> struct bind_traits <T[n]> {
417 typedef const T (&type)[n];
418};
419
420template<class T, int n>
421struct bind_traits<const T[n]> {
422 typedef const T (&type)[n];
423};
424
425template<class T, int n> struct bind_traits<volatile T[n]> {
426 typedef const volatile T (&type)[n];
427};
428
429template<class T, int n>
430struct bind_traits<const volatile T[n]> {
431 typedef const volatile T (&type)[n];
432};
433
434template<class R>
435struct bind_traits<R()> {
436 typedef R(&type)();
437};
438
439template<class R, class Arg1>
440struct bind_traits<R(Arg1)> {
441 typedef R(&type)(Arg1);
442};
443
444template<class R, class Arg1, class Arg2>
445struct bind_traits<R(Arg1, Arg2)> {
446 typedef R(&type)(Arg1, Arg2);
447};
448
449template<class R, class Arg1, class Arg2, class Arg3>
450struct bind_traits<R(Arg1, Arg2, Arg3)> {
451 typedef R(&type)(Arg1, Arg2, Arg3);
452};
453
454template<class R, class Arg1, class Arg2, class Arg3, class Arg4>
455struct bind_traits<R(Arg1, Arg2, Arg3, Arg4)> {
456 typedef R(&type)(Arg1, Arg2, Arg3, Arg4);
457};
458
459template<class R, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
460struct bind_traits<R(Arg1, Arg2, Arg3, Arg4, Arg5)> {
461 typedef R(&type)(Arg1, Arg2, Arg3, Arg4, Arg5);
462};
463
464template<class R, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
465struct bind_traits<R(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6)> {
466 typedef R(&type)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6);
467};
468
469template<class R, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
470struct bind_traits<R(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7)> {
471 typedef R(&type)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7);
472};
473
474template<class R, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7, class Arg8>
475struct bind_traits<R(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8)> {
476 typedef R(&type)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8);
477};
478
479template<class R, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7, class Arg8, class Arg9>
480struct bind_traits<R(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9)> {
481 typedef R(&type)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7, Arg8, Arg9);
482};
483
484template<class T>
485struct bind_traits<reference_wrapper<T> >{
486 typedef T& type;
487};
488
489template<class T>
490struct bind_traits<const reference_wrapper<T> >{
491 typedef T& type;
492};
493
494template<>
495struct bind_traits<void> {
496 typedef void type;
497};
498
499
500
501template <
502 class T0 = null_type, class T1 = null_type, class T2 = null_type,
503 class T3 = null_type, class T4 = null_type, class T5 = null_type,
504 class T6 = null_type, class T7 = null_type, class T8 = null_type,
505 class T9 = null_type
506>
507struct bind_tuple_mapper {
508 typedef
509 tuple<typename bind_traits<T0>::type,
510 typename bind_traits<T1>::type,
511 typename bind_traits<T2>::type,
512 typename bind_traits<T3>::type,
513 typename bind_traits<T4>::type,
514 typename bind_traits<T5>::type,
515 typename bind_traits<T6>::type,
516 typename bind_traits<T7>::type,
517 typename bind_traits<T8>::type,
518 typename bind_traits<T9>::type> type;
519};
520
521// bind_traits, except map const T& -> const T
522 // this is needed e.g. in currying. Const reference arguments can
523 // refer to temporaries, so it is not safe to store them as references.
524 template <class T> struct remove_const_reference {
525 typedef typename bind_traits<T>::type type;
526 };
527
528 template <class T> struct remove_const_reference<const T&> {
529 typedef const T type;
530 };
531
532
533// maps the bind argument types to the resulting lambda functor type
534template <
535 class T0 = null_type, class T1 = null_type, class T2 = null_type,
536 class T3 = null_type, class T4 = null_type, class T5 = null_type,
537 class T6 = null_type, class T7 = null_type, class T8 = null_type,
538 class T9 = null_type
539>
540class bind_type_generator {
541
542 typedef typename
543 detail::bind_tuple_mapper<
544 T0, T1, T2, T3, T4, T5, T6, T7, T8, T9
545 >::type args_t;
546
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700547 NDNBOOST_STATIC_CONSTANT(int, nof_elems = ndnboost::tuples::length<args_t>::value);
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700548
549 typedef
550 action<
551 nof_elems,
552 function_action<nof_elems>
553 > action_type;
554
555public:
556 typedef
557 lambda_functor<
558 lambda_functor_base<
559 action_type,
560 args_t
561 >
562 > type;
563
564};
565
566
567
568} // detail
569
570template <class T> inline const T& make_const(const T& t) { return t; }
571
572
573} // end of namespace lambda
574} // end of namespace ndnboost
575
576
577
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700578#endif // NDNBOOST_LAMBDA_TRAITS_HPP