cxtream  0.5.1
C++17 data pipeline with Python bindings.
for_each.hpp
1 /****************************************************************************
2  * cxtream library
3  * Copyright (c) 2017, Cognexa Solutions s.r.o.
4  * Author(s) Filip Matzner
5  *
6  * This file is distributed under the MIT License.
7  * See the accompanying file LICENSE.txt for the complete license agreement.
8  ****************************************************************************/
9 
10 #ifndef CXTREAM_CORE_STREAM_FOR_EACH_HPP
11 #define CXTREAM_CORE_STREAM_FOR_EACH_HPP
12 
13 #include <cxtream/core/stream/template_arguments.hpp>
14 #include <cxtream/core/stream/transform.hpp>
15 #include <cxtream/core/utility/tuple.hpp>
16 
17 namespace cxtream::stream {
18 
19 namespace detail {
20 
21  // Wrap the given function so that its return value is ignored
22  // and so that it can be forwarded to stream::transform.
23  template<typename Fun, typename... FromTypes>
24  struct wrap_void_fun_for_transform {
25  Fun fun;
26 
27  constexpr utility::maybe_tuple<FromTypes...> operator()(FromTypes&... args)
28  {
29  std::invoke(fun, args...);
30  // we can force std::move here because the old
31  // data are going to be ignored anyway
32  return {std::move(args)...};
33  }
34  };
35 
36 } // namespace detail
37 
58 template<typename... FromColumns, typename Fun, int Dim = 1>
59 constexpr auto for_each(from_t<FromColumns...> f, Fun fun, dim_t<Dim> d = dim_t<1>{})
60 {
61  // wrap the function to be compatible with stream::transform
62  detail::wrap_void_fun_for_transform<
63  Fun, utility::ndim_type_t<typename FromColumns::batch_type, Dim>...>
64  fun_wrapper{std::move(fun)};
65  // apply the dummy transformation
66  return stream::transform(f, to<FromColumns...>, std::move(fun_wrapper), d);
67 }
68 
69 } // namespace cxtream::stream
70 #endif
constexpr auto transform(from_t< FromColumns... > f, to_t< ToColumns... > t, Fun fun, dim_t< Dim > d=dim_t< 1 >{})
Transform a subset of cxtream columns to a different subset of cxtream columns.
Definition: transform.hpp:177
constexpr auto for_each(from_t< FromColumns... > f, Fun fun, dim_t< Dim > d=dim_t< 1 >{})
Apply a function to a subset of stream columns.
Definition: for_each.hpp:59