TPIE

11a2c2d
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::pipelining {
26 namespace bits {
27 
28 template <typename T>
29 struct remove {
30  typedef T type;
31 };
32 
33 template <typename T>
34 struct remove<const T> {
35  typedef typename remove<T>::type type;
36 };
37 
38 template <typename T>
39 struct remove<T &> {
40  typedef typename remove<T>::type type;
41 };
42 
43 template <typename T>
44 struct remove<T &&> {
45  typedef typename remove<T>::type type;
46 };
47 
48 template <typename T>
49 struct push_traits {};
50 
51 template <typename ClassType, typename ArgType>
52 struct push_traits< void(ClassType::*)(ArgType) > {
53  typedef ArgType type;
54 };
55 
56 template <typename T>
57 struct pull_traits {};
58 
59 template <typename ClassType, typename RetType>
60 struct pull_traits< RetType(ClassType::*)() > {
61  typedef RetType type;
62 };
63 
64 template <typename T>
65 using void_if_valid = void;
66 
67 template <typename T>
68 struct has_itemtype {
69  typedef char yes[1];
70  typedef char no[2];
71 
72  template <typename C>
73  static yes& test(void_if_valid<typename C::item_type> *);
74 
75  template <typename>
76  static no& test(...);
77  //static_assert(sizeof(test<T>(nullptr)) == sizeof(yes), "WTF");
78  static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes);
79 };
80 
81 template <typename T>
83  typedef char yes[1];
84  typedef char no[2];
85 
86  template <typename C>
87  static yes& test(void_if_valid<typename push_traits<decltype(&C::push)>::type> *);
88 
89  template <typename>
90  static no& test(...);
91  //static_assert(sizeof(test<T>(nullptr)) == sizeof(yes), "WTF");
92  static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes);
93 };
94 
95 template <typename T>
97  typedef char yes[1];
98  typedef char no[2];
99 
100  template <typename C>
101  static yes& test(void_if_valid<typename pull_traits<decltype(&C::pull)>::type> *);
102 
103  template <typename>
104  static no& test(...);
105  //static_assert(sizeof(test<T>(nullptr)) == sizeof(yes), "WTF");
106  static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes);
107 };
108 
109 template <typename T, typename default_type, bool has_push_method, bool has_item_type>
111  typedef default_type type;
112 };
113 
114 template <typename T, typename default_type, bool has_push_method>
115 struct push_type_help<T, default_type, has_push_method, true> {
116  typedef typename T::item_type type;
117 };
118 
119 template <typename T, typename default_type>
120 struct push_type_help<T, default_type, true, false> {
121  typedef typename push_traits<decltype(&T::push)>::type type_;
122  typedef typename std::decay<type_>::type type;
123 };
124 
125 template <typename T, typename default_type, bool has_push_method, bool has_item_type>
127  typedef default_type type;
128 };
129 
130 template <typename T, typename default_type, bool has_pull_method>
131 struct pull_type_help<T, default_type, has_pull_method, true> {
132  typedef typename T::item_type type;
133 };
134 
135 template <typename T, typename default_type>
136 struct pull_type_help<T, default_type, true, false> {
137  typedef typename pull_traits<decltype(&T::pull)>::type type_;
138  typedef typename std::decay<type_>::type type;
139 };
140 
141 struct pull_type_base_tag;
142 
143 template <typename T, template <typename> typename map>
144 struct pull_type_base_help {using item_type = map<T>;};
145 
146 template <template <typename> typename map>
147 struct pull_type_base_help<pull_type_base_tag, map> {};
148 
149 
150 } // namespace bits
151 
157 template <typename T, typename default_type = void>
158 struct push_type {
159  typedef typename std::decay<T>::type node_type;
161  static_assert(!std::is_void<type>::value, "Could not deduce push type.");
162 };
163 
164 template <typename T, typename default_type = void>
165 struct pull_type {
166  typedef typename std::decay<T>::type node_type;
168  static_assert(!std::is_void<type>::value, "Could not deduce pull type.");
169 };
170 
171 
172 template <typename T>
173 using push_type_map_identity = T;
174 
175 template <typename T, template <typename> typename map = push_type_map_identity>
177 
178 } // namespace tpie::pipelining
179 
180 #endif // TPIE_PIPELINING_NODE_TRAITS_H
tpie::pipelining
pipelining/factory_base.h Base class of pipelining factories
Definition: ami_glue.h:23
tpie::pipelining::bits::pull_type_base_help
Definition: node_traits.h:144
tpie::pipelining::bits::pull_type_help
Definition: node_traits.h:126
tpie::pipelining::push_type
Class to deduce the item_type of a node of type T.
Definition: node_traits.h:158
tpie::pipelining::bits::has_itemtype
Definition: node_traits.h:68
tpie::pipelining::pull_type
Definition: node_traits.h:165
tpie::pipelining::bits::push_type_help
Definition: node_traits.h:110
tpie::pipelining::bits::has_pull_method
Definition: node_traits.h:96
tpie::pipelining::map
pipe_middle< tfactory< bits::map_t, Args< F >, F > > map(const F &functor)
Pipelining nodes that applies to given functor to items in the stream.
Definition: map.h:154
tpie::pipelining::bits::remove
Definition: node_traits.h:29
tpie::pipelining::bits::push_traits
Definition: node_traits.h:49
tpie::pipelining::bits::has_push_method
Definition: node_traits.h:82
tpie::pipelining::bits::pull_traits
Definition: node_traits.h:57