Support Class Library
A set of tools providing classes and utility
map.h
Go to the documentation of this file.
1 #pragma once
4 #include <functional>
5 #include <memory>
8 
9 namespace scl{
10  namespace stream{
11  namespace operators{
12  namespace details{
18  template <class T, class U>
20  public:
24 
26  using parent_value_type = typename parent_iterator_type::value_type;
27  using parent_payload_type = typename parent_iterator_type::payload_type;
29 
34  using mapper_type = std::function<value_type(const parent_value_type&)>;
35 
42  }
43 
44  payload_type next() override{
45  auto alt = this->parent()->next().value();
46 
47  return alt.hasValue()
48  ? payload_type::withValue(this->mapper(*alt))
49  : payload_type::withoutValue();
50  }
51 
52  protected:
58  };
59 
65  template <class T, class U>
66  struct map_toolbox{
72 
78  };
79  }
80 
81 
91  template <
92  class F,
93  class Fn = decltype(META::as_fn(std::declval<F>())),
94  class T = META::decay_t<META::arg_t<Fn, 0>>,
95  class U = META::decay_t<META::return_t<Fn>>
96  >
98  return {META::as_fn(std::forward<F>(mapper))};
99  }
100 
108  template<class T, class U = T>
110  return {mapper};
111  }
112 
119  template <class T, class U>
121  using namespace scl::tools;
122  return Stream<U>{
123  make::ptr<details::MapOperator<T, U>>(lhs.it(), rhs.mapper)
124  };
125  }
126  }
127  }
128 }
typename iterator_type::parent_iterator_type parent_iterator_type
Definition: map.h:25
parent_type parent() const
Getter for the parent iterator.
Iterator powering an intermediate operation.
typename iterator_type::payload_type payload_type
Definition: map.h:23
typename iterator_type::parent_type parent_type
Definition: map.h:28
StreamIteratorPayload< U > payload_type
Type alias for the payload used.
typename payload_type::value_type value_type
Type alias for the data type manipulated.
Global namespace of the SCL.
Definition: alias.hpp:3
mapper_t mapper
the mapper function
Definition: map.h:77
it_t it() const
Get a reference to the underlying iterator.
Definition: Stream.h:48
details::map_toolbox< T, U > map_(typename details::map_toolbox< T, U >::mapper_t mapper)
Map a stream by explicitly providing the type arguments.
Definition: map.h:109
std_fn_type< R(*)(Args...)> as_fn(R(*f)(Args...))
Definition: fn_introspect.h:89
typename parent_iterator_type::payload_type parent_payload_type
Definition: map.h:27
mapper_type mapper
the function used to map values from the parent iterator
Definition: map.h:57
payload_type next() override
Retrieve the next iterator payload.
Definition: map.h:44
std::shared_ptr< parent_iterator_type > parent_type
BaseStreamIterator< T > parent_iterator_type
Type alias for the "parent" iterator.
Tag type that allows operation piping for map operations.
Definition: map.h:66
typename MapOperator< T, U >::mapper_type mapper_t
The mapper funtion type.
Definition: map.h:71
General purpose tooling.
Definition: iostream.hpp:4
Stream< T > operator|(const Stream< T > &lhs, const details::filter_toolbox< T > &rhs)
Pipe operator overload for filter operations.
Definition: filter.h:117
std::function< value_type(const parent_value_type &)> mapper_type
Function type that maps the parent value type to the current value type.
Definition: map.h:34
typename parent_iterator_type::value_type parent_value_type
Definition: map.h:26
details::map_toolbox< T, U > map(F &&mapper)
Map a stream.
Definition: map.h:97
MapOperator(parent_type p, mapper_type mapper)
Construct the operator from the parent iterator and a mapper function.
Definition: map.h:41
typename iterator_type::value_type value_type
Definition: map.h:22
Class representing a stream of data.
Definition: Stream.h:15