writebuf.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 <functional>
31 #include <iterator>
32 #include <ios>
33 
34 namespace atria {
35 namespace xform {
36 
37 namespace detail {
38 
39 struct writebuf_rf_gen
40 {
41  template <typename ReducingFnT, typename OutputStreamRefT>
42  struct apply
43  {
44  ReducingFnT step;
45  OutputStreamRefT stream_ref;
46 
47  template <typename StateT, typename InputT, typename ...InputTs>
48  auto operator() (StateT&& s, InputT&& buf, InputTs&& ...is)
49  -> decltype(step(std::forward<StateT>(s),
50  std::forward<InputT>(buf),
51  std::forward<InputTs>(is)...))
52  {
53  using std::begin;
54  using std::end;
55 
56  auto& stream = stream_ref.get();
57  auto first = begin(buf);
58  auto last = end(buf);
59  stream.write(&*first, static_cast<std::streamsize>(
60  std::distance(first, last)));
61  return step(std::forward<StateT>(s),
62  std::forward<InputT>(buf),
63  std::forward<InputTs>(is)...);
64  }
65  };
66 };
67 
68 } // namespace detail
69 
70 template <typename OutputStreamRefT>
71 using writebuf_t = transducer_impl<detail::writebuf_rf_gen, OutputStreamRefT>;
72 
78 template <typename OutputStreamT>
79 auto writebuf(OutputStreamT& stream)
81 {
82  return { std::ref(stream) };
83 }
84 
85 } // namespace xform
86 } // namespace atria
Utility to write simple transducers easily.
C++ amazing templates and reusable implementations awesomeness.
Definition: _doc.hpp:35
auto writebuf(OutputStreamT &stream) -> writebuf_t< std::reference_wrapper< OutputStreamT > >
Generator transducer that reads buffers of size N from stream, and passes them into the sequence...
Definition: writebuf.hpp:79
Fork me on GitHub