TPIE

2362a60
node_traits.h
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2014, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
20 #ifndef TPIE_PIPELINING_NODE_TRAITS_H
21 #define TPIE_PIPELINING_NODE_TRAITS_H
22 
23 #include <type_traits>
24 
25 namespace tpie {
26 
27 namespace pipelining {
28 
29 namespace bits {
30 
31 template <typename T>
32 struct remove {
33  typedef T type;
34 };
35 
36 template <typename T>
37 struct remove<const T> {
38  typedef typename remove<T>::type type;
39 };
40 
41 template <typename T>
42 struct remove<T &> {
43  typedef typename remove<T>::type type;
44 };
45 
46 template <typename T>
47 struct remove<T &&> {
48  typedef typename remove<T>::type type;
49 };
50 
51 template <typename T>
52 struct push_traits {};
53 
54 template <typename ClassType, typename ArgType>
55 struct push_traits< void(ClassType::*)(ArgType) > {
56  typedef ArgType type;
57 };
58 
59 template <typename T>
60 struct pull_traits {};
61 
62 template <typename ClassType, typename RetType>
63 struct pull_traits< RetType(ClassType::*)() > {
64  typedef RetType type;
65 };
66 
67 template <typename T>
68 using void_if_valid = void;
69 
70 template <typename T>
71 struct has_itemtype {
72  typedef char yes[1];
73  typedef char no[2];
74 
75  template <typename C>
76  static yes& test(void_if_valid<typename C::item_type> *);
77 
78  template <typename>
79  static no& test(...);
80  //static_assert(sizeof(test<T>(nullptr)) == sizeof(yes), "WTF");
81  static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes);
82 };
83 
84 template <typename T>
86  typedef char yes[1];
87  typedef char no[2];
88 
89  template <typename C>
90  static yes& test(void_if_valid<typename push_traits<decltype(&C::push)>::type> *);
91 
92  template <typename>
93  static no& test(...);
94  //static_assert(sizeof(test<T>(nullptr)) == sizeof(yes), "WTF");
95  static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes);
96 };
97 
98 template <typename T>
100  typedef char yes[1];
101  typedef char no[2];
102 
103  template <typename C>
104  static yes& test(void_if_valid<typename pull_traits<decltype(&C::pull)>::type> *);
105 
106  template <typename>
107  static no& test(...);
108  //static_assert(sizeof(test<T>(nullptr)) == sizeof(yes), "WTF");
109  static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes);
110 };
111 
112 template <typename T, typename default_type, bool has_push_method, bool has_item_type>
114  typedef default_type type;
115 };
116 
117 template <typename T, typename default_type, bool has_push_method>
118 struct push_type_help<T, default_type, has_push_method, true> {
119  typedef typename T::item_type type;
120 };
121 
122 template <typename T, typename default_type>
123 struct push_type_help<T, default_type, true, false> {
124  typedef typename push_traits<decltype(&T::push)>::type type_;
125  typedef typename std::decay<type_>::type type;
126 };
127 
128 template <typename T, typename default_type, bool has_push_method, bool has_item_type>
130  typedef default_type type;
131 };
132 
133 template <typename T, typename default_type, bool has_pull_method>
134 struct pull_type_help<T, default_type, has_pull_method, true> {
135  typedef typename T::item_type type;
136 };
137 
138 template <typename T, typename default_type>
139 struct pull_type_help<T, default_type, true, false> {
140  typedef typename pull_traits<decltype(&T::pull)>::type type_;
141  typedef typename std::decay<type_>::type type;
142 };
143 
144 } // namespace bits
145 
151 template <typename T, typename default_type = void>
152 struct push_type {
153  typedef typename std::decay<T>::type node_type;
155  static_assert(!std::is_void<type>::value, "Could not deduce push type.");
156 };
157 
158 template <typename T, typename default_type = void>
159 struct pull_type {
160  typedef typename std::decay<T>::type node_type;
162  static_assert(!std::is_void<type>::value, "Could not deduce pull type.");
163 };
164 
165 
166 
167 } // namespace pipelining
168 
169 } // namespace tpie
170 
171 #endif // TPIE_PIPELINING_NODE_TRAITS_H
Class to deduce the item_type of a node of type T.
Definition: node_traits.h:152