TPIE

2362a60
map.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 2011, 2012, 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_MAP_H__
21 #define __TPIE_PIPELINING_MAP_H__
22 
23 #include <tpie/pipelining/node.h>
24 #include <tpie/pipelining/pipe_base.h>
25 #include <tpie/pipelining/factory_helpers.h>
26 #include <tpie/pipelining/node_name.h>
27 #include <type_traits>
28 
29 namespace tpie {
30 namespace pipelining {
31 namespace bits {
32 
33 template <typename T>
35 
36 template <typename C, typename R, typename A>
37 struct unary_traits_imp<R(C::*)(A)> {
38  typedef A argument_type;
39  typedef R return_type;
40 };
41 
42 template <typename C, typename R, typename A>
43 struct unary_traits_imp<R(C::*)(A) const > {
44  typedef A argument_type;
45  typedef R return_type;
46 };
47 
48 template <typename R, typename A>
49 struct unary_traits_imp<R(*)(A)> {
50  typedef A argument_type;
51  typedef R return_type;
52 };
53 
54 template <typename T>
55 struct unary_traits: public unary_traits_imp<decltype(&T::operator()) > {};
56 
57 template <typename F>
58 class map_t {
59 public:
60  template <typename dest_t>
61  class type: public node {
62  private:
63  F functor;
64  dest_t dest;
65  public:
66  typedef typename std::decay<typename unary_traits<F>::argument_type>::type item_type;
67 
68  type(dest_t dest, const F & functor):
69  functor(functor), dest(std::move(dest)) {
70  set_name(bits::extract_pipe_name(typeid(F).name()), PRIORITY_NO_NAME);
71  }
72 
73  void push(const item_type & item) {
74  dest.push(functor(item));
75  }
76  };
77 };
78 
79 template <typename F>
80 class map_temp_t {
81 public:
82  template <typename dest_t>
83  class type: public node {
84  private:
85  F functor;
86  dest_t dest;
87  public:
88  type(dest_t dest, const F & functor):
89  functor(functor), dest(std::move(dest)) {
90  set_name(bits::extract_pipe_name(typeid(F).name()), PRIORITY_NO_NAME);
91  }
92 
93  template <typename T>
94  void push(const T & item) {
95  dest.push(functor(item));
96  }
97  };
98 };
99 
100 
101 template <typename F>
102 class map_sink_t: public node {
103 private:
104  F functor;
105 public:
106  typedef typename std::decay<typename unary_traits<F>::argument_type>::type item_type;
107 
108  map_sink_t(const F & functor):
109  functor(functor) {
110  set_name(bits::extract_pipe_name(typeid(F).name()), PRIORITY_NO_NAME);
111  }
112 
113  void push(const item_type & item) {
114  functor(item);
115  }
116 };
117 
118 template <typename T>
120  typedef char yes[1];
121  typedef char no[2];
122 
123  // This does not seem to work as well as it should
124  // template <typename C>
125  // static yes& test(typename unary_traits_imp<decltype(&C::operator())>::argument_type *);
126 
127  template <typename C>
128  static yes& test(decltype(&C::operator()) *);
129 
130  template <typename>
131  static no& test(...);
132  static const bool value = sizeof(test<T>(nullptr)) == sizeof(yes);
133 };
134 
135 } //namespace bits
136 
142 template <typename F, typename = typename std::enable_if<bits::has_argument_type<F>::value>::type>
143 pipe_middle<tempfactory<bits::map_t<F>, F> > map(const F & functor) {
144  return tempfactory<bits::map_t<F>, F >(functor);
145 }
146 
147 template <typename F, typename = typename std::enable_if<!bits::has_argument_type<F>::value>::type>
148 pipe_middle<tempfactory<bits::map_temp_t<F>, F> > map(const F & functor) {
149  return tempfactory<bits::map_temp_t<F>, F >(functor);
150 }
151 
152 template <typename F>
153 pipe_end<termfactory<bits::map_sink_t<F>, F> > map_sink(const F & functor) {
154  return termfactory<bits::map_sink_t<F>, F >(functor);
155 }
156 
157 } //namespace pipelining
158 } //namespace terrastream
159 
160 #endif //__TPIE_PIPELINING_MAP_H__
Base class of all nodes.
Definition: node.h:78
pipe_middle< tempfactory< bits::map_t< F >, F > > map(const F &functor)
Pipelining nodes that applies to given functor to items in the stream.
Definition: map.h:143
Node factory for variadic argument templated generators.
void set_name(const std::string &name, priority_type priority=PRIORITY_USER)
Set this node's name.
A pipe_middle class pushes input down the pipeline.
Definition: pipe_base.h:241