read.hpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2014, 2015 Ableton AG, Berlin. All rights reserved.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21 //
22 
27 #pragma once
28 
30 #include <atria/xform/skip.hpp>
32 #include <atria/prelude/comp.hpp>
33 #include <functional>
34 
35 namespace atria {
36 namespace xform {
37 
38 namespace detail {
39 
40 template <typename ValueT>
41 struct read_rf_gen
42 {
43  template <typename ReducingFnT, typename InputStreamRefT>
44  struct apply
45  {
46  ReducingFnT step;
47  InputStreamRefT stream_ref;
48 
49  template <typename StateT, typename ...InputTs>
50  auto operator() (StateT&& s, InputTs&& ...is)
51  -> decltype(reduced(skip(step,
52  state_unwrap(std::forward<StateT>(s)),
53  std::forward<InputTs>(is)...,
54  std::declval<ValueT>())))
55  {
56  ValueT value{};
57  auto& stream = stream_ref.get();
58  if (stream) {
59  stream >> value;
60  if (!stream.fail())
61  return not_reduced(
62  call(step,
63  state_unwrap(std::forward<StateT>(s)),
64  std::forward<InputTs>(is)...,
65  std::move(value)));
66  }
67  return reduced(
68  skip(step,
69  state_unwrap(std::forward<StateT>(s)),
70  std::forward<InputTs>(is)...,
71  std::move(value)));
72  }
73  };
74 };
75 
76 } // namespace detail
77 
78 template <typename T, typename InputStreamRefT>
79 using read_t = transducer_impl<detail::read_rf_gen<T>, InputStreamRefT>;
80 
87 template <typename T, typename InputStreamT>
88 auto read(InputStreamT& stream)
90 {
91  return { std::ref(stream) };
92 }
93 
94 template <typename T1, typename T2, typename ...Ts, typename InputStreamT>
95 auto read(InputStreamT& stream)
97  comp(read<T1>(stream),
98  read<T2>(stream),
99  read<Ts>(stream)...))
100 
101 } // namespace xform
102 } // namespace atria
auto reduced(T &&x) -> decltype(reduced_if(std::forward< T >(x), true))
Wraps x such that the reduction should finish.
auto call(ReducingFnT &&step, StateT &&state, InputTs &&...ins) -> estd::enable_if_t< is_skip_state< estd::decay_t< StateT > >
Call the next reducing function in a transducer that could otherwise skip calling the next reducing f...
Definition: skip.hpp:276
#define ABL_DECLTYPE_RETURN(body_expr)
Utility for defining generic functions with a deduced return type, that are composed of a single expr...
Definition: utils.hpp:109
auto state_unwrap(T &&s) -> decltype(state_traits_t< T >::unwrap(std::forward< T >(s)))
Convenience function for calling state_traits::unwrap
Utility to write simple transducers easily.
auto not_reduced(T &&x) -> decltype(reduced_if(std::forward< T >(x), false))
Wraps x such that the reduction should continue.
auto comp(F &&f) -> F &&
Right-to left function composition.
Definition: comp.hpp:80
C++ amazing templates and reusable implementations awesomeness.
Definition: _doc.hpp:35
auto read(InputStreamT &stream) -> read_t< T, std::reference_wrapper< InputStreamT > >
Generator transducer that produces a sequence of values of type T read from the given stream using th...
Definition: read.hpp:88
auto skip(ReducingFnT &&, StateT &&state, InputTs &&...) -> skip_result_t< ReducingFnT, StateT, InputTs... >
Skip calling the next reducing function in a transducer.
Definition: skip.hpp:228
Fork me on GitHub