1 /*************************************************************************
3 * Copyright 2016 Realm Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 **************************************************************************/
19 #ifndef REALM_UTIL_MEMORY_STREAM_HPP
20 #define REALM_UTIL_MEMORY_STREAM_HPP
30 class MemoryInputStreambuf : public std::streambuf {
32 MemoryInputStreambuf();
33 ~MemoryInputStreambuf() noexcept;
35 /// Behavior is undefined if the size of the specified buffer exceeds
37 void set_buffer(const char* begin, const char* end) noexcept;
44 int_type underflow() override;
45 int_type uflow() override;
46 int_type pbackfail(int_type) override;
47 std::streamsize showmanyc() override;
48 pos_type seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode) override;
49 pos_type seekpos(pos_type, std::ios_base::openmode) override;
51 pos_type do_seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode);
55 class MemoryOutputStreambuf : public std::streambuf {
57 MemoryOutputStreambuf();
58 ~MemoryOutputStreambuf() noexcept;
60 /// Behavior is undefined if the size of the specified buffer exceeds
62 void set_buffer(char* begin, char* end) noexcept;
64 /// Returns the amount of data written to the buffer.
65 size_t size() const noexcept;
69 class MemoryInputStream : public std::istream {
72 ~MemoryInputStream() noexcept;
74 /// \{ Behavior is undefined if the size of the specified buffer exceeds
76 void set_buffer(const char* begin, const char* end) noexcept;
77 template <size_t N> void set_buffer(const char (&buffer)[N]) noexcept;
78 void set_string(const std::string&) noexcept;
79 void set_c_string(const char* c_str) noexcept;
83 MemoryInputStreambuf m_streambuf;
87 class MemoryOutputStream : public std::ostream {
90 ~MemoryOutputStream() noexcept;
92 /// \{ Behavior is undefined if the size of the specified buffer exceeds
94 void set_buffer(char* begin, char* end) noexcept;
95 template <size_t N> void set_buffer(char (&buffer)[N]) noexcept;
98 /// Returns the amount of data written to the underlying buffer.
99 size_t size() const noexcept;
102 MemoryOutputStreambuf m_streambuf;
108 inline MemoryInputStreambuf::MemoryInputStreambuf()
115 inline MemoryInputStreambuf::~MemoryInputStreambuf() noexcept
119 inline void MemoryInputStreambuf::set_buffer(const char* begin, const char* end) noexcept
127 inline MemoryOutputStreambuf::MemoryOutputStreambuf()
131 inline MemoryOutputStreambuf::~MemoryOutputStreambuf() noexcept
135 inline void MemoryOutputStreambuf::set_buffer(char* begin, char* end) noexcept
140 inline size_t MemoryOutputStreambuf::size() const noexcept
142 return pptr() - pbase();
146 inline MemoryInputStream::MemoryInputStream()
147 : std::istream(&m_streambuf)
151 inline MemoryInputStream::~MemoryInputStream() noexcept
155 inline void MemoryInputStream::set_buffer(const char* begin, const char* end) noexcept
157 m_streambuf.set_buffer(begin, end);
161 template <size_t N> inline void MemoryInputStream::set_buffer(const char (&buffer)[N]) noexcept
163 const char* begin = buffer;
164 const char* end = begin + N;
165 set_buffer(begin, end);
168 inline void MemoryInputStream::set_string(const std::string& str) noexcept
170 const char* begin = str.data();
171 const char* end = begin + str.size();
172 set_buffer(begin, end);
175 inline void MemoryInputStream::set_c_string(const char* c_str) noexcept
177 const char* begin = c_str;
178 const char* end = begin + traits_type::length(c_str);
179 set_buffer(begin, end);
183 inline MemoryOutputStream::MemoryOutputStream()
184 : std::ostream(&m_streambuf)
188 inline MemoryOutputStream::~MemoryOutputStream() noexcept
192 inline void MemoryOutputStream::set_buffer(char* begin, char* end) noexcept
194 m_streambuf.set_buffer(begin, end);
199 inline void MemoryOutputStream::set_buffer(char (&buffer)[N]) noexcept
201 set_buffer(buffer, buffer + N);
204 inline size_t MemoryOutputStream::size() const noexcept
206 return m_streambuf.size();
212 #endif // REALM_UTIL_MEMORY_STREAM_HPP