Support Class Library
A set of tools providing classes and utility
filter.h
Go to the documentation of this file.
1 #pragma once
4 #include <functional>
5 #include <memory>
8 #include <scl/stream/Stream.h>
9 
10 namespace scl{
11  namespace stream{
12  namespace operators{
13  namespace details{
18  template <class T>
20  public:
26 
31  using predicate_type = std::function<bool(const value_type&)>;
32 
39  }
40 
41  payload_type next() override{
42  while(this->parent()->hasNext()){
43  const auto& alt = this->parent()->next().value();
44  if(alt.hasValue()){
45  const auto& value = *alt;
46  if(this->pred(value))
47  return payload_type::withValue(value);
48  }
49  }
50 
51  return payload_type::withoutValue();
52  }
53  protected:
59  };
60 
65  template <class T>
72 
78  };
79  }
80 
89  template <
90  class F,
91  class Fn = decltype(META::as_fn(std::declval<F>())),
92  class T = META::decay_t<META::arg_t<Fn, 0>>
93  >
95  return {META::as_fn(predicate)};
96  }
97 
104  template <class T>
106  return {pred};
107  }
108 
116  template <class T>
118  using namespace scl::tools;
119  return Stream<T>{
120  make::ptr<details::FilterOperator<T>>(lhs.it(), rhs.pred)
121  };
122  }
123  }
124  }
125 }
Tag type for filter operations.
Definition: filter.h:66
details::filter_toolbox< T > filter(F &&predicate)
Filter a stream.
Definition: filter.h:94
details::filter_toolbox< T > filter_(typename details::filter_toolbox< T >::pred_t pred)
Filter a stream by explicitly giving the type arguments.
Definition: filter.h:105
Abstract base class for stream iterators defining the required behavior.
Definition: StlAdapter.h:11
parent_type parent() const
Getter for the parent iterator.
FilterOperator(parent_type p, predicate_type pred)
Construct a filter operation from a parent iterator and a predicate.
Definition: filter.h:38
Iterator powering an intermediate operation.
StreamIteratorPayload< T > 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
std::function< bool(const value_type &)> predicate_type
The function type used to determine whether to keep the value or not.
Definition: filter.h:31
it_t it() const
Get a reference to the underlying iterator.
Definition: Stream.h:48
std_fn_type< R(*)(Args...)> as_fn(R(*f)(Args...))
Definition: fn_introspect.h:89
typename iterator_type::parent_type parent_type
Definition: filter.h:25
std::shared_ptr< parent_iterator_type > parent_type
BaseStreamIterator< T > parent_iterator_type
Type alias for the "parent" iterator.
predicate_type pred
The predicate.
Definition: filter.h:58
typename FilterOperator< T >::predicate_type pred_t
The predicate type.
Definition: filter.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
typename iterator_type::parent_iterator_type parent_iterator_type
Definition: filter.h:24
payload_type next() override
Retrieve the next iterator payload.
Definition: filter.h:41
A class representing a stream iterator&#39;s payload.
Definition: payload.h:16
Class representing a stream of data.
Definition: Stream.h:15