staticlib_io  1.2.11
 All Classes Namespaces Files Functions Variables
multi_source.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018, 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: multi_source.hpp
19  * Author: alex
20  *
21  * Created on June 18, 2018, 11:05 AM
22  */
23 
24 #ifndef STATICLIB_IO_MULTI_SOURCE_HPP
25 #define STATICLIB_IO_MULTI_SOURCE_HPP
26 
27 #include <ios>
28 #include <vector>
29 
30 #include "staticlib/config.hpp"
31 
33 #include "staticlib/io/span.hpp"
34 
35 namespace staticlib {
36 namespace io {
37 
41 template<typename Range>
42 class multi_source {
46  Range sources;
50  typename Range::iterator it;
51 
52 public:
58  multi_source(Range&& sources_range):
59  sources(std::move(sources_range)),
60  it(sources.begin()) { }
61 
67  multi_source(const multi_source&) = delete;
68 
75  multi_source& operator=(const multi_source&) = delete;
76 
82  multi_source(multi_source&& other) STATICLIB_NOEXCEPT :
83  sources(std::move(other.sources)),
84  it(std::move(other.it)) { }
85 
92  multi_source& operator=(multi_source&& other) STATICLIB_NOEXCEPT {
93  sources = std::move(other.sources);
94  it = std::move(other.it);
95  return *this;
96  }
97 
104  std::streamsize read(span<char> span) {
105  while (sources.end() != it) {
106  std::streamsize read = it->read(span);
107  if (std::char_traits<char>::eof() != read) {
108  return read;
109  }
110  ++it;
111  }
112  return std::char_traits<char>::eof();
113  }
114 
115 };
116 
124 template <typename Range,
125 class = typename std::enable_if<!std::is_lvalue_reference<Range>::value>::type>
127  return multi_source<Range>(std::move(range));
128 }
129 
137 template <typename Range>
138 multi_source<std::vector<reference_source<typename Range::value_type>>>
139 make_multi_source(Range& range) {
140  auto vec = std::vector<reference_source<typename Range::value_type>>();
141  for (auto& el : range) {
142  vec.emplace_back(make_reference_source(el));
143  }
145 }
146 
147 } // namespace
148 }
149 
150 #endif /* STATICLIB_IO_MULTI_SOURCE_HPP */
151