readbuf.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 
31 #include <atria/prelude/comp.hpp>
32 #include <functional>
33 
34 #include <ableton/build_system/Warnings.hpp>
35 ABL_DISABLE_WARNINGS
36 #include <boost/range/iterator_range.hpp>
37 ABL_RESTORE_WARNINGS
38 #include <array>
39 #include <vector>
40 
41 namespace atria {
42 namespace xform {
43 
44 namespace detail {
45 
46 struct readbuf_rf_gen
47 {
48  template <typename ReducingFnT, typename InputStreamRefT, typename BufferT>
49  struct apply
50  {
51  ReducingFnT step;
52  InputStreamRefT stream_ref;
53  BufferT buffer;
54 
55  template <typename StateT, typename ...InputTs>
56  auto operator() (StateT&& s, InputTs&& ...is)
57  -> decltype(reduced(step(state_unwrap(std::forward<StateT>(s)),
58  std::forward<InputTs>(is)...,
59  boost::make_iterator_range(std::declval<char*>(),
60  std::declval<char*>()))))
61  {
62  auto& stream = stream_ref.get();
63  auto data = &buffer[0];
64  stream.read(data, static_cast<std::streamsize>(buffer.size()));
65  return reduced_if(
66  step(state_unwrap(std::forward<StateT>(s)),
67  std::forward<InputTs>(is)...,
68  boost::make_iterator_range(
69  data,
70  data + stream.gcount())),
71  !stream);
72  }
73  };
74 };
75 
76 } // namespace detail
77 
78 template <typename InputStreamRefT, typename BufferT>
79 using readbuf_t = transducer_impl<detail::readbuf_rf_gen,
80  InputStreamRefT,
81  BufferT>;
82 
88 template <std::size_t N, typename InputStreamT>
89 auto readbuf(InputStreamT& stream)
90  -> readbuf_t<std::reference_wrapper<InputStreamT>, std::array<char, N> >
91 {
92  return { std::ref(stream), std::array<char, N>{} };
93 }
94 
98 template <typename InputStreamT>
99 auto readbuf(InputStreamT& stream, std::size_t n)
100  -> readbuf_t<std::reference_wrapper<InputStreamT>, std::vector<char> >
101 {
102  return { std::ref(stream), std::vector<char>(n) };
103 }
104 
105 } // namespace xform
106 } // namespace atria
auto reduced(T &&x) -> decltype(reduced_if(std::forward< T >(x), true))
Wraps x such that the reduction should finish.
auto state_unwrap(T &&s) -> decltype(state_traits_t< T >::unwrap(std::forward< T >(s)))
Convenience function for calling state_traits::unwrap
auto readbuf(InputStreamT &stream) -> readbuf_t< std::reference_wrapper< InputStreamT >, std::array< char, N > >
Generator transducer that reads buffers of size N from stream, and passes them into the sequence...
Definition: readbuf.hpp:89
Utility to write simple transducers easily.
auto reduced_if(T &&x, bool is_reduced) -> maybe_reduced< estd::decay_t< T > >
Wraps x in a maybe_reduced, where is_reduced contains whether the reduction should actually finish...
C++ amazing templates and reusable implementations awesomeness.
Definition: _doc.hpp:35
Fork me on GitHub