added iOS source code
[wl-app.git] / iOS / Pods / Realm / include / core / realm / util / memory_stream.hpp
1 /*************************************************************************
2  *
3  * Copyright 2016 Realm Inc.
4  *
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  **************************************************************************/
18
19 #ifndef REALM_UTIL_MEMORY_STREAM_HPP
20 #define REALM_UTIL_MEMORY_STREAM_HPP
21
22 #include <cstddef>
23 #include <string>
24 #include <istream>
25 #include <ostream>
26
27 namespace realm {
28 namespace util {
29
30 class MemoryInputStreambuf : public std::streambuf {
31 public:
32     MemoryInputStreambuf();
33     ~MemoryInputStreambuf() noexcept;
34
35     /// Behavior is undefined if the size of the specified buffer exceeds
36     /// PTRDIFF_MAX.
37     void set_buffer(const char* begin, const char* end) noexcept;
38
39 private:
40     const char* m_begin;
41     const char* m_end;
42     const char* m_curr;
43
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;
50
51     pos_type do_seekoff(off_type, std::ios_base::seekdir, std::ios_base::openmode);
52 };
53
54
55 class MemoryOutputStreambuf : public std::streambuf {
56 public:
57     MemoryOutputStreambuf();
58     ~MemoryOutputStreambuf() noexcept;
59
60     /// Behavior is undefined if the size of the specified buffer exceeds
61     /// PTRDIFF_MAX.
62     void set_buffer(char* begin, char* end) noexcept;
63
64     /// Returns the amount of data written to the buffer.
65     size_t size() const noexcept;
66 };
67
68
69 class MemoryInputStream : public std::istream {
70 public:
71     MemoryInputStream();
72     ~MemoryInputStream() noexcept;
73
74     /// \{ Behavior is undefined if the size of the specified buffer exceeds
75     /// PTRDIFF_MAX.
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;
80     /// \}
81
82 private:
83     MemoryInputStreambuf m_streambuf;
84 };
85
86
87 class MemoryOutputStream : public std::ostream {
88 public:
89     MemoryOutputStream();
90     ~MemoryOutputStream() noexcept;
91
92     /// \{ Behavior is undefined if the size of the specified buffer exceeds
93     /// PTRDIFF_MAX.
94     void set_buffer(char* begin, char* end) noexcept;
95     template <size_t N> void set_buffer(char (&buffer)[N]) noexcept;
96     /// \}
97
98     /// Returns the amount of data written to the underlying buffer.
99     size_t size() const noexcept;
100
101 private:
102     MemoryOutputStreambuf m_streambuf;
103 };
104
105
106 // Implementation
107
108 inline MemoryInputStreambuf::MemoryInputStreambuf()
109     : m_begin(nullptr)
110     , m_end(nullptr)
111     , m_curr(nullptr)
112 {
113 }
114
115 inline MemoryInputStreambuf::~MemoryInputStreambuf() noexcept
116 {
117 }
118
119 inline void MemoryInputStreambuf::set_buffer(const char* begin, const char* end) noexcept
120 {
121     m_begin = begin;
122     m_end = end;
123     m_curr = begin;
124 }
125
126
127 inline MemoryOutputStreambuf::MemoryOutputStreambuf()
128 {
129 }
130
131 inline MemoryOutputStreambuf::~MemoryOutputStreambuf() noexcept
132 {
133 }
134
135 inline void MemoryOutputStreambuf::set_buffer(char* begin, char* end) noexcept
136 {
137     setp(begin, end);
138 }
139
140 inline size_t MemoryOutputStreambuf::size() const noexcept
141 {
142     return pptr() - pbase();
143 }
144
145
146 inline MemoryInputStream::MemoryInputStream()
147     : std::istream(&m_streambuf)
148 {
149 }
150
151 inline MemoryInputStream::~MemoryInputStream() noexcept
152 {
153 }
154
155 inline void MemoryInputStream::set_buffer(const char* begin, const char* end) noexcept
156 {
157     m_streambuf.set_buffer(begin, end);
158     clear();
159 }
160
161 template <size_t N> inline void MemoryInputStream::set_buffer(const char (&buffer)[N]) noexcept
162 {
163     const char* begin = buffer;
164     const char* end = begin + N;
165     set_buffer(begin, end);
166 }
167
168 inline void MemoryInputStream::set_string(const std::string& str) noexcept
169 {
170     const char* begin = str.data();
171     const char* end = begin + str.size();
172     set_buffer(begin, end);
173 }
174
175 inline void MemoryInputStream::set_c_string(const char* c_str) noexcept
176 {
177     const char* begin = c_str;
178     const char* end = begin + traits_type::length(c_str);
179     set_buffer(begin, end);
180 }
181
182
183 inline MemoryOutputStream::MemoryOutputStream()
184     : std::ostream(&m_streambuf)
185 {
186 }
187
188 inline MemoryOutputStream::~MemoryOutputStream() noexcept
189 {
190 }
191
192 inline void MemoryOutputStream::set_buffer(char* begin, char* end) noexcept
193 {
194     m_streambuf.set_buffer(begin, end);
195     clear();
196 }
197
198 template <size_t N>
199 inline void MemoryOutputStream::set_buffer(char (&buffer)[N]) noexcept
200 {
201     set_buffer(buffer, buffer + N);
202 }
203
204 inline size_t MemoryOutputStream::size() const noexcept
205 {
206     return m_streambuf.size();
207 }
208
209 } // namespace util
210 } // namespace realm
211
212 #endif // REALM_UTIL_MEMORY_STREAM_HPP