staticlib_io  1.2.11
 All Classes Namespaces Files Functions Variables
copying_source.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015, alex at staticlibs.net
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * File: copying_source.hpp
19  * Author: alex
20  *
21  * Created on October 9, 2015, 12:24 PM
22  */
23 
24 #ifndef STATICLIB_IO_COPYING_SOURCE_HPP
25 #define STATICLIB_IO_COPYING_SOURCE_HPP
26 
27 #include <ios>
28 #include <utility>
29 
30 #include "staticlib/config.hpp"
31 
35 #include "staticlib/io/span.hpp"
36 
37 namespace staticlib {
38 namespace io {
39 
44 template<typename Source, typename Sink>
49  Source src;
53  Sink sink;
54 
55 public:
63  copying_source(Source&& src, Sink&& sink) :
64  src(std::move(src)),
65  sink(std::move(sink)) { }
66 
72  copying_source(const copying_source&) = delete;
73 
80  copying_source& operator=(const copying_source&) = delete;
81 
87  copying_source(copying_source&& other) STATICLIB_NOEXCEPT :
88  src(std::move(other.src)),
89  sink(std::move(other.sink)) { }
90 
97  copying_source& operator=(copying_source&& other) STATICLIB_NOEXCEPT {
98  src = std::move(other.src);
99  sink = std::move(other.sink);
100  return *this;
101  }
102 
110  std::streamsize read(span<char> span) {
111  std::streamsize res = src.read(span);
112  if (std::char_traits<char>::eof() != res) {
113  write_all(sink, {span.data(), res});
114  }
115  return res;
116  }
117 
123  std::streamsize flush() {
124  return sink.flush();
125  }
126 
132  Source& get_source() {
133  return src;
134  }
135 
141  Sink& get_sink() {
142  return sink;
143  }
144 
145 };
146 
155 template<typename Source, typename Sink,
156  class = typename std::enable_if<!std::is_lvalue_reference<Source>::value>::type,
157  class = typename std::enable_if<!std::is_lvalue_reference<Sink>::value>::type>
158 copying_source<Source, Sink> make_copying_source(Source&& source, Sink&& sink) {
159  return copying_source<Source, Sink>(std::move(source), std::move(sink));
160 }
161 
170 template<typename Source, typename Sink>
171 copying_source<reference_source<Source>, reference_sink<Sink>>
172 make_copying_source(Source& source, Sink& sink) {
175 }
176 
177 } // namespace
178 }
179 
180 #endif /* STATICLIB_IO_COPYING_SOURCE_HPP */
181