TPIE

11a2c2d
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  using constructed_type = R<typename bits::remove<dest_t>::type>;
47 
48  template <typename dest_t>
49  constructed_type<dest_t> construct(dest_t && dest) {
50  node_token tok = dest.get_token();
51  constructed_type<dest_t> r = container_construct<constructed_type<dest_t>>(cont, std::forward<dest_t>(dest));
52  this->init_node(r);
53  this->add_default_edge(r, tok);
54  this->add_node_set_edges(r);
55  return r;
56  }
57 
58  template <typename dest_t>
59  constructed_type<dest_t> construct_copy(dest_t && dest) {
60  node_token tok = dest.get_token();
61  constructed_type<dest_t> r = container_construct_copy<constructed_type<dest_t>>(cont, std::forward<dest_t>(dest));
62  this->init_node(r);
63  this->add_default_edge(r, tok);
64  this->add_node_set_edges(r);
65  return r;
66  }
67 
68 private:
69  container<T...> cont;
70 };
71 
72 
77 template <typename R, typename... T>
78 class termfactory : public factory_base {
79 public:
80  typedef R constructed_type;
81 
82  termfactory(const termfactory & o) = delete;
83  termfactory(termfactory && o) = default;
84  termfactory & operator=(const termfactory & o) = delete;
85  termfactory & operator=(termfactory && o) = default;
86 
87  template<typename... Args>
88  termfactory(Args && ... v) : cont(std::forward<Args>(v)...) {}
89 
90  R construct() {
91  R r = container_construct<R>(cont);
92  this->init_node(r);
93  this->add_node_set_edges(r);
94  return r;
95  }
96 
97  R construct_copy() {
98  R r = container_construct_copy<R>(cont);
99  this->init_node(r);
100  this->add_node_set_edges(r);
101  return r;
102  }
103 private:
104  container<T...> cont;
105 };
106 
107 
108 template <typename ...>
109 class Args;
110 
115 template <template <typename dest_t, typename ... X> class R, typename Args, typename... T>
116 class tfactory {/*We should never use this*/};
117 
118 template <template <typename dest_t, typename ... X> class R,
119  typename ...TT, typename... T>
120 class tfactory<R, Args<TT...>, T...> : public factory_base {
121 public:
122  tfactory(const tfactory & o) = delete;
123  tfactory(tfactory && o) = default;
124  tfactory & operator=(const tfactory & o) = delete;
125  tfactory & operator=(tfactory && o) = default;
126 
127  template<typename... Args>
128  tfactory(Args && ... v) : cont(std::forward<Args>(v)...) {}
129 
130  template<typename dest_t>
131  using constructed_type = R<typename bits::remove<dest_t>::type, TT...>;
132 
133  template <typename dest_t>
134  constructed_type<dest_t> construct(dest_t && dest) {
135  node_token tok = dest.get_token();
136  constructed_type<dest_t> r = container_construct<constructed_type<dest_t>>(cont, std::forward<dest_t>(dest));
137  this->init_node(r);
138  this->add_default_edge(r, tok);
139  this->add_node_set_edges(r);
140  return r;
141  }
142 
143  template <typename dest_t>
144  constructed_type<dest_t> construct_copy(dest_t && dest) {
145  node_token tok = dest.get_token();
146  constructed_type<dest_t> r = container_construct_copy<constructed_type<dest_t>>(cont, std::forward<dest_t>(dest));
147  this->init_node(r);
148  this->add_default_edge(r, tok);
149  this->add_node_set_edges(r);
150  return r;
151  }
152 private:
153  container<T...> cont;
154 };
155 
156 
165 template <template <typename item_type> class I, typename OB, template<typename dest_t> class O>
166 class split_factory : public factory_base {
167 public:
168  template <typename dest_t>
169  using constructed_type = I<typename push_type<dest_t>::type>;
170 
171  template <typename dest_t>
172  constructed_type<dest_t> construct(dest_t && dest) const {
173  node_token input_token;
174  typedef typename push_type<dest_t>::type item_type;
175  std::shared_ptr<OB> o = std::make_shared<O<dest_t> >(std::forward<dest_t>(dest), input_token);
176  return I<item_type>(input_token, std::move(o));
177  };
178 
179  template <typename dest_t>
180  constructed_type<dest_t> construct_copy(dest_t && dest) const {
181  return construct(std::forward<dest_t>(dest));
182  };
183 };
184 
185 } // namespace pipelining
186 } // namespace tpie
187 
188 #endif // __TPIE_PIPELINING_FACTORY_HELPERS_H__
tpie::pipelining::factory
Definition: factory_helpers.h:35
tpie::pipelining::factory_base::init_node
void init_node(node &r)
\Brief Initialize node constructed in a subclass.
tpie::pipelining::split_factory
Node factory for split nodes, typically used for phase boundary nodes.
Definition: factory_helpers.h:166
tpie::pipelining::factory_base::add_default_edge
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.
tpie::pipelining::push_type
Class to deduce the item_type of a node of type T.
Definition: node_traits.h:158
tpie::pipelining::factory_base
Base class of all pipelining factories.
Definition: factory_base.h:73
tpie::pipelining::Args
Definition: factory_helpers.h:109
tpie::pipelining::container
Definition: container.h:61
tpie::pipelining::termfactory
Definition: factory_helpers.h:78
tpie::pipelining::tfactory
Definition: factory_helpers.h:116
tpie::pipelining::node_token
Definition: tokens.h:292
tpie::pipelining::item_type
pipe_middle< tfactory< bits::item_type_t, Args< T > > > item_type()
Create item type defining identity pipe node.
Definition: helpers.h:654
tpie
Definition: access_type.h:26