staticlib_io  1.2.11
 All Classes Namespaces Files Functions Variables
limited_source.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017, 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: limited_source.hpp
19  * Author: alex
20  *
21  * Created on January 29, 2017, 1:33 PM
22  */
23 
24 #ifndef STATICLIB_IO_LIMITED_SOURCE_HPP
25 #define STATICLIB_IO_LIMITED_SOURCE_HPP
26 
27 #include <ios>
28 
29 #include "staticlib/config.hpp"
30 
34 #include "staticlib/io/span.hpp"
35 
36 namespace staticlib {
37 namespace io {
38 
43 template<typename Source>
49 
53  size_t limit_bytes;
54 
55 public:
56 
64  limited_source(Source&& src, size_t limit_bytes) :
65  src(staticlib::io::make_counting_source(std::move(src))),
66  limit_bytes(limit_bytes) { }
67 
73  limited_source(const limited_source&) = delete;
74 
81  limited_source& operator=(const limited_source&) = delete;
82 
88  limited_source(limited_source&& other) STATICLIB_NOEXCEPT :
89  src(std::move(other.src)),
90  limit_bytes(other.limit_bytes) {
91  other.limit_bytes = 0;
92  }
93 
100  limited_source& operator=(limited_source&& other) STATICLIB_NOEXCEPT {
101  src = std::move(other.src);
102  limit_bytes = other.limit_bytes;
103  other.limit_bytes = 0;
104  return *this;
105  }
106 
113  std::streamsize read(span<char> origin_span) {
114  if (src.get_count() < limit_bytes) {
115  size_t ulen = origin_span.size();
116  auto remaining_bytes = limit_bytes - src.get_count();
117  auto read_length = (ulen < remaining_bytes) ? ulen : remaining_bytes;
118  return src.read(span<char> {origin_span.data(), static_cast<uint32_t>(read_length)});
119  } else {
120  return std::char_traits<char>::eof();
121  }
122  }
123 
129  size_t get_count() {
130  return src.get_count();
131  }
132 
138  Source& get_source() {
139  return src.get_source();
140  }
141 
142 };
143 
152 template <typename Source,
153 class = typename std::enable_if<!std::is_lvalue_reference<Source>::value>::type>
154 limited_source<Source> make_limited_source(Source&& source, size_t limit_bytes) {
155  return limited_source<Source>(std::move(source), limit_bytes);
156 }
157 
166 template <typename Source>
167 limited_source<reference_source<Source>> make_limited_source(Source& source, size_t limit_bytes) {
168  return limited_source<reference_source<Source>> (make_reference_source(source), limit_bytes);
169 }
170 
171 } // namespace
172 }
173 
174 #endif /* STATICLIB_IO_LIMITED_SOURCE_HPP */
175