| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- #pragma once
- namespace stream { namespace detail {
- namespace map {
- template <typename T, typename R>
- class iterator {
- public:
- iterator(std::function<R(T const&)> f, ::stream::iterator<T>&& impl)
- : fun_(f), impl_(std::forward<::stream::iterator<T>>(impl)) {}
- ~iterator() {}
- R operator*() { return fun_(*impl_); }
- DELEGATE_ITERATOR_IMPL(impl_)
- private:
- std::function<R(T const&)> fun_;
- ::stream::iterator<T> impl_;
- };
- }
-
- template <typename T, typename R>
- class map_stream : public stream_impl<R> {
- public:
- template <typename F>
- map_stream(F&& func, stream_base<T> const& sb) : fun_(func), source_(sb) {}
- ~map_stream() override {}
-
- iterator<R> begin() override {
- return {map::iterator<T, R>{fun_, source_.begin()}};
- }
-
- iterator<R> end() override {
- return {map::iterator<T, R>{fun_, source_.end()}};
- }
- private:
- std::function<R(T const&)> fun_;
- stream_base<T> source_;
- };
-
- template <typename T>
- template <typename F>
- auto stream_base<T>::map(F&& func) const -> stream_base<map_f<F>> {
- using impl_t = map_stream<T, map_f<F>>;
- return {std::make_shared<impl_t>(func, *this)};
- }
- } }
|