BRE12
_template_helpers.h
1 /*
2  Copyright 2005-2016 Intel Corporation. All Rights Reserved.
3 
4  This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5  you can redistribute it and/or modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation. Threading Building Blocks is
7  distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9  See the GNU General Public License for more details. You should have received a copy of
10  the GNU General Public License along with Threading Building Blocks; if not, write to the
11  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12 
13  As a special exception, you may use this file as part of a free software library without
14  restriction. Specifically, if other files instantiate templates or use macros or inline
15  functions from this file, or you compile this file and link it with other files to produce
16  an executable, this file does not by itself cause the resulting executable to be covered
17  by the GNU General Public License. This exception does not however invalidate any other
18  reasons why the executable file might be covered by the GNU General Public License.
19 */
20 
21 #ifndef __TBB_template_helpers_H
22 #define __TBB_template_helpers_H
23 
24 #include <utility>
25 
26 namespace tbb { namespace internal {
27 
29 template<bool Condition, typename T = void> struct enable_if {};
30 template<typename T> struct enable_if<true, T> { typedef T type; };
31 
33 template<typename T> struct strip { typedef T type; };
34 template<typename T> struct strip<const T> { typedef T type; };
35 template<typename T> struct strip<volatile T> { typedef T type; };
36 template<typename T> struct strip<const volatile T> { typedef T type; };
37 template<typename T> struct strip<T&> { typedef T type; };
38 template<typename T> struct strip<const T&> { typedef T type; };
39 template<typename T> struct strip<volatile T&> { typedef T type; };
40 template<typename T> struct strip<const volatile T&> { typedef T type; };
42 template<typename T> struct strip<T(&)()> { typedef T(*type)(); };
43 #if __TBB_CPP11_RVALUE_REF_PRESENT
44 template<typename T> struct strip<T&&> { typedef T type; };
45 template<typename T> struct strip<const T&&> { typedef T type; };
46 template<typename T> struct strip<volatile T&&> { typedef T type; };
47 template<typename T> struct strip<const volatile T&&> { typedef T type; };
48 #endif
49 template<typename T, size_t N> struct strip<T(&)[N]> { typedef T* type; };
51 template<typename T, size_t N> struct strip<const T(&)[N]> { typedef const T* type; };
52 template<typename T, size_t N> struct strip<volatile T(&)[N]> { typedef volatile T* type; };
53 template<typename T, size_t N> struct strip<const volatile T(&)[N]> { typedef const volatile T* type; };
54 
56 template<class U, class V> struct is_same_type { static const bool value = false; };
57 template<class W> struct is_same_type<W,W> { static const bool value = true; };
58 
59 template<typename T> struct is_ref { static const bool value = false; };
60 template<typename U> struct is_ref<U&> { static const bool value = true; };
61 
62 #if __TBB_CPP11_RVALUE_REF_PRESENT && __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
63 
65 template< typename... Types >
66 struct stored_pack;
67 
68 template<>
69 struct stored_pack<>
70 {
71  typedef stored_pack<> pack_type;
72  stored_pack() {}
73 
74  // Friend front-end functions
75  template< typename F, typename Pack > friend void call( F&& f, Pack&& p );
76  template< typename Ret, typename F, typename Pack > friend Ret call_and_return( F&& f, Pack&& p );
77 
78 protected:
79  // Ideally, ref-qualified non-static methods would be used,
80  // but that would greatly reduce the set of compilers where it works.
81  template< typename Ret, typename F, typename... Preceding >
82  static Ret call( F&& f, const pack_type& /*pack*/, Preceding&&... params ) {
83  return std::forward<F>(f)( std::forward<Preceding>(params)... );
84  }
85  template< typename Ret, typename F, typename... Preceding >
86  static Ret call( F&& f, pack_type&& /*pack*/, Preceding&&... params ) {
87  return std::forward<F>(f)( std::forward<Preceding>(params)... );
88  }
89 };
90 
91 template< typename T, typename... Types >
92 struct stored_pack<T, Types...> : stored_pack<Types...>
93 {
94  typedef stored_pack<T, Types...> pack_type;
95  typedef stored_pack<Types...> pack_remainder;
96  // Since lifetime of original values is out of control, copies should be made.
97  // Thus references should be stripped away from the deduced type.
98  typename strip<T>::type leftmost_value;
99 
100  // Here rvalue references act in the same way as forwarding references,
101  // as long as class template parameters were deduced via forwarding references.
102  stored_pack( T&& t, Types&&... types )
103  : pack_remainder(std::forward<Types>(types)...), leftmost_value(std::forward<T>(t)) {}
104 
105  // Friend front-end functions
106  template< typename F, typename Pack > friend void call( F&& f, Pack&& p );
107  template< typename Ret, typename F, typename Pack > friend Ret call_and_return( F&& f, Pack&& p );
108 
109 protected:
110  template< typename Ret, typename F, typename... Preceding >
111  static Ret call( F&& f, const pack_type& pack, Preceding&&... params ) {
112  return pack_remainder::template call<Ret>(
113  std::forward<F>(f), static_cast<const pack_remainder&>(pack),
114  std::forward<Preceding>(params)... , pack.leftmost_value
115  );
116  }
117  template< typename Ret, typename F, typename... Preceding >
118  static Ret call( F&& f, pack_type&& pack, Preceding&&... params ) {
119  return pack_remainder::template call<Ret>(
120  std::forward<F>(f), static_cast<pack_remainder&&>(pack),
121  std::forward<Preceding>(params)... , std::move(pack.leftmost_value)
122  );
123  }
124 };
125 
127 template< typename F, typename Pack >
128 void call( F&& f, Pack&& p ) {
129  strip<Pack>::type::template call<void>( std::forward<F>(f), std::forward<Pack>(p) );
130 }
131 
132 template< typename Ret, typename F, typename Pack >
133 Ret call_and_return( F&& f, Pack&& p ) {
134  return strip<Pack>::type::template call<Ret>( std::forward<F>(f), std::forward<Pack>(p) );
135 }
136 
137 template< typename... Types >
138 stored_pack<Types...> save_pack( Types&&... types ) {
139  return stored_pack<Types...>( std::forward<Types>(types)... );
140 }
141 
142 #endif /* __TBB_CPP11_RVALUE_REF_PRESENT && __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT */
143 } } // namespace internal, namespace tbb
144 
145 #endif /* __TBB_template_helpers_H */
Enables one or the other code branches.
Definition: _template_helpers.h:29
Detects whether two given types are the same.
Definition: _template_helpers.h:56
Definition: _template_helpers.h:59
Definition: _flow_graph_async_msg_impl.h:32
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44
Strips its template type argument from cv- and ref-qualifiers.
Definition: _template_helpers.h:33