staticlib_io  1.2.11
 All Classes Namespaces Files Functions Variables
streambuf_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: streambuf_source.hpp
19  * Author: alex
20  *
21  * Created on October 9, 2015, 12:32 PM
22  */
23 
24 #ifndef STATICLIB_IO_STREAMBUF_SOURCE_HPP
25 #define STATICLIB_IO_STREAMBUF_SOURCE_HPP
26 
27 #include <ios>
28 #include <streambuf>
29 
30 #include "staticlib/config.hpp"
31 
32 #include "staticlib/io/span.hpp"
33 
34 namespace staticlib {
35 namespace io {
36 
44  std::streambuf* streambuf;
45 
46 public:
52  explicit streambuf_source(std::streambuf* streambuf) :
53  streambuf(streambuf) { }
54 
61  streambuf_source(const streambuf_source&) = delete;
62 
70 
76  streambuf_source(streambuf_source&& other) STATICLIB_NOEXCEPT :
77  streambuf(other.streambuf) {
78  other.streambuf = nullptr;
79  }
80 
87  streambuf_source& operator=(streambuf_source&& other) STATICLIB_NOEXCEPT {
88  streambuf = other.streambuf;
89  other.streambuf = nullptr;
90  return *this;
91  }
92 
99  std::streamsize read(span<char> span) {
100  std::streamsize res = streambuf->sgetn(span.data(), span.size_signed());
101  if (res > 0) {
102  return res;
103  } else if (0 == res && std::char_traits<char>::eof() == streambuf->sbumpc()) {
104  return std::char_traits<char>::eof();
105  } else {
106  streambuf->sungetc();
107  return res;
108  }
109  }
110 
111 };
112 
113 } // namespace
114 }
115 
116 #endif /* STATICLIB_IO_STREAMBUF_SOURCE_HPP */
117