staticlib_io  1.2.11
 All Classes Namespaces Files Functions Variables
array_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: array_source.hpp
19  * Author: alex
20  *
21  * Created on November 6, 2015, 3:59 PM
22  */
23 
24 #ifndef STATICLIB_IO_ARRAY_SOURCE_HPP
25 #define STATICLIB_IO_ARRAY_SOURCE_HPP
26 
27 #include <cstdint>
28 #include <cstring>
29 #include <ios>
30 
31 #include "staticlib/config.hpp"
32 
34 #include "staticlib/io/span.hpp"
35 
36 namespace staticlib {
37 namespace io {
38 
43 class array_source {
47  const char* src_buf;
51  size_t src_buf_len;
55  size_t idx = 0;
56 
57 public:
64  array_source(const char* src_buf, size_t src_buf_len) :
65  src_buf(src_buf),
66  src_buf_len(src_buf_len) { }
67 
74  src_buf(src_buf.data()),
75  src_buf_len(src_buf.size()) { }
76 
83  src_buf(src_buf.data()),
84  src_buf_len(src_buf.size()) { }
85 
91  array_source(const array_source& other) :
92  src_buf(other.src_buf),
93  src_buf_len(other.src_buf_len),
94  idx(other.idx) { }
95 
103  src_buf = other.src_buf;
104  src_buf_len = other.src_buf_len;
105  idx = other.idx;
106  return *this;
107  }
108 
114  array_source(array_source&& other) STATICLIB_NOEXCEPT :
115  src_buf(other.src_buf),
116  src_buf_len(other.src_buf_len),
117  idx(other.idx) {
118  other.src_buf = nullptr;
119  other.src_buf_len = 0;
120  other.idx = 0;
121  }
122 
129  array_source& operator=(array_source&& other) STATICLIB_NOEXCEPT {
130  src_buf = other.src_buf;
131  other.src_buf = nullptr;
132  src_buf_len = other.src_buf_len;
133  other.src_buf_len = 0;
134  idx = other.idx;
135  other.idx = 0;
136  return *this;
137  }
138 
145  std::streamsize read(span<char> span) {
146  size_t ulen = span.size();
147  size_t avail = src_buf_len - idx;
148  if (avail > 0) {
149  size_t len = ulen <= avail ? ulen : avail;
150  std::memcpy(span.data(), src_buf + idx, len);
151  this->idx += len;
152  return static_cast<std::streamsize>(len);
153  } else return std::char_traits<char>::eof();
154  }
155 
161  const char* get_array() {
162  return src_buf;
163  }
164 
165 };
166 
167 } // namespace
168 }
169 
170 #endif /* STATICLIB_IO_ARRAY_SOURCE_HPP */
171