TPIE

2362a60
factory_helpers.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 2013, 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_FACTORY_HELPERS_H__
21 #define __TPIE_PIPELINING_FACTORY_HELPERS_H__
22 
23 #include <tpie/pipelining/container.h>
24 #include <tpie/pipelining/factory_base.h>
25 #include <tpie/pipelining/node.h>
26 
27 namespace tpie {
28 namespace pipelining {
29 
34 template <template <typename dest_t> class R, typename... T>
35 class factory : public factory_base {
36 public:
37  factory(const factory &) = delete;
38  factory(factory &&) = default;
39  factory & operator=(const factory &) = delete;
40  factory & operator=(factory &&) = default;
41 
42  template <typename... Args>
43  factory(Args && ... v) : cont(std::forward<Args>(v)...) {}
44 
45  template<typename dest_t>
46  struct constructed {
47  typedef R<typename bits::remove<dest_t>::type> type;
48  };
49 
50  template <typename dest_t>
51  typename constructed<dest_t>::type construct(dest_t && dest) {
52  node_token tok = dest.get_token();
53  typename constructed<dest_t>::type r = container_construct<typename constructed<dest_t>::type>(cont, std::forward<dest_t>(dest));
54  this->init_node(r);
55  this->add_default_edge(r, tok);
56  this->add_node_set_edges(r);
57  return r;
58  }
59 
60  template <typename dest_t>
61  typename constructed<dest_t>::type construct_copy(dest_t && dest) {
62  node_token tok = dest.get_token();
63  typename constructed<dest_t>::type r = container_construct_copy<typename constructed<dest_t>::type>(cont, std::forward<dest_t>(dest));
64  this->init_node(r);
65  this->add_default_edge(r, tok);
66  this->add_node_set_edges(r);
67  return r;
68  }
69 
70 private:
71  container<T...> cont;
72 };
73 
78 template <typename Holder, typename... T>
79 class tempfactory : public factory_base {
80 public:
81  tempfactory(const tempfactory & o) = delete;
82  tempfactory(tempfactory && o) = default;
83  tempfactory & operator=(const tempfactory & o) = delete;
84  tempfactory & operator=(tempfactory && o) = default;
85 
86  template <typename... Args>
87  tempfactory(Args && ... v) : cont(std::forward<Args>(v)...) {}
88 
89  template<typename dest_t>
90  struct constructed {
91  typedef typename Holder::template type<typename bits::remove<dest_t>::type> type;
92  };
93 
94  template <typename dest_t>
95  typename constructed<dest_t>::type construct(dest_t && dest) {
96  node_token tok = dest.get_token();
97  typename constructed<dest_t>::type r = container_construct<typename constructed<dest_t>::type>(cont, std::forward<dest_t>(dest));
98  this->init_node(r);
99  this->add_default_edge(r, tok);
100  this->add_node_set_edges(r);
101  return r;
102  }
103 
104  template <typename dest_t>
105  typename constructed<dest_t>::type construct_copy(dest_t && dest) {
106  node_token tok = dest.get_token();
107  typename constructed<dest_t>::type r = container_construct_copy<typename constructed<dest_t>::type>(cont, std::forward<dest_t>(dest));
108  this->init_node(r);
109  this->add_default_edge(r, tok);
110  this->add_node_set_edges(r);
111  return r;
112  }
113 private:
114  container<T...> cont;
115 };
116 
121 template <typename R, typename... T>
122 class termfactory : public factory_base {
123 public:
124  typedef R constructed_type;
125 
126  termfactory(const termfactory & o) = delete;
127  termfactory(termfactory && o) = default;
128  termfactory & operator=(const termfactory & o) = delete;
129  termfactory & operator=(termfactory && o) = default;
130 
131  template<typename... Args>
132  termfactory(Args && ... v) : cont(std::forward<Args>(v)...) {}
133 
134  R construct() {
135  R r = container_construct<R>(cont);
136  this->init_node(r);
137  this->add_node_set_edges(r);
138  return r;
139  }
140 
141  R construct_copy() {
142  R r = container_construct_copy<R>(cont);
143  this->init_node(r);
144  this->add_node_set_edges(r);
145  return r;
146  }
147 private:
148  container<T...> cont;
149 };
150 
151 
152 template <typename ...>
153 class Args;
154 
159 template <template <typename dest_t, typename ... X> class R, typename Args, typename... T>
160 class tfactory {/*We should never use this*/};
161 
162 template <template <typename dest_t, typename ... X> class R,
163  typename ...TT, typename... T>
164 class tfactory<R, Args<TT...>, T...> : public factory_base {
165 public:
166  tfactory(const tfactory & o) = delete;
167  tfactory(tfactory && o) = default;
168  tfactory & operator=(const tfactory & o) = delete;
169  tfactory & operator=(tfactory && o) = default;
170 
171  template<typename... Args>
172  tfactory(Args && ... v) : cont(std::forward<Args>(v)...) {}
173 
174  template<typename dest_t>
175  struct constructed {
176  typedef R<typename bits::remove<dest_t>::type, TT...> type;
177  };
178 
179  template <typename dest_t>
180  typename constructed<dest_t>::type construct(dest_t && dest) {
181  node_token tok = dest.get_token();
182  typename constructed<dest_t>::type r = container_construct<typename constructed<dest_t>::type>(cont, std::forward<dest_t>(dest));
183  this->init_node(r);
184  this->add_default_edge(r, tok);
185  this->add_node_set_edges(r);
186  return r;
187  }
188 
189  template <typename dest_t>
190  typename constructed<dest_t>::type construct_copy(dest_t && dest) {
191  node_token tok = dest.get_token();
192  typename constructed<dest_t>::type r = container_construct_copy<typename constructed<dest_t>::type>(cont, std::forward<dest_t>(dest));
193  this->init_node(r);
194  this->add_default_edge(r, tok);
195  this->add_node_set_edges(r);
196  return r;
197  }
198 private:
199  container<T...> cont;
200 };
201 
202 
211 template <template <typename item_type> class I, typename OB, template<typename dest_t> class O>
212 class split_factory : public factory_base {
213 public:
214  template <typename dest_t>
215  struct constructed {
216  typedef typename push_type<dest_t>::type item_type;
217  typedef I<item_type> type;
218  };
219 
220  template <typename dest_t>
221  typename constructed<dest_t>::type construct(dest_t && dest) const {
222  node_token input_token;
223  typedef typename push_type<dest_t>::type item_type;
224  std::shared_ptr<OB> o = std::make_shared<O<dest_t> >(std::forward<dest_t>(dest), input_token);
225  return I<item_type>(input_token, std::move(o));
226  };
227 
228  template <typename dest_t>
229  typename constructed<dest_t>::type construct_copy(dest_t && dest) const {
230  return construct(std::forward<dest_t>(dest));
231  };
232 };
233 
234 } // namespace pipelining
235 } // namespace tpie
236 
237 #endif // __TPIE_PIPELINING_FACTORY_HELPERS_H__
void add_default_edge(node &r, const node &dest) const
Used by pipe_base classes to set a default actor edge for ordinary push/pull nodes.
Definition: factory_base.h:178
Base class of all pipelining factories.
Definition: factory_base.h:73
void init_node(node &r)
Initialize node constructed in a subclass.
Definition: factory_base.h:134
Class to deduce the item_type of a node of type T.
Definition: node_traits.h:152
Node factory for variadic argument templated generators.
Node factory for variadic argument terminators.
pipe_middle< tempfactory< bits::item_type_t< T > > > item_type()
Create item type defining identity pipe node.
Definition: helpers.h:654
Node factory for variadic argument generators.
Node factory for variadic argument terminators.
Node factory for split nodes, typically used for phase boundary nodes.