Mangrove
The C++ Object Document Mapper for MongoDB
bson_streambuf.hpp
1 // Copyright 2016 MongoDB Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <boson/config/prelude.hpp>
18 
19 #include <istream>
20 #include <ostream>
21 #include <streambuf>
22 
23 #include <bsoncxx/builder/stream/document.hpp>
24 #include <bsoncxx/json.hpp>
25 
26 namespace boson {
27 BOSON_INLINE_NAMESPACE_BEGIN
28 
35 class BOSON_API bson_output_streambuf : public std::streambuf {
36  public:
37  using document_callback = std::function<void(bsoncxx::document::value)>;
38 
44  bson_output_streambuf(document_callback cb);
45 
52  int overflow(int ch) override;
53 
59  virtual int underflow() override;
60 
61  private:
72  BOSON_PRIVATE int insert(int ch);
73  // A callback that accepts a document::value and returns void.
74  document_callback _cb;
75  std::unique_ptr<uint8_t, void (*)(std::uint8_t *)> _data;
76  size_t _len = 0;
77  size_t _bytes_read = 0;
78 };
79 
85 class BOSON_API bson_ostream : public std::ostream {
86  public:
87  bson_ostream(bson_output_streambuf::document_callback cb) : std::ostream(0), bs_buf(cb) {
88  this->rdbuf(&bs_buf);
89  }
90 
91  private:
92  // TODO somehow set rdbuf(*sb) to private so people can't change the streambuffer?
93  bson_output_streambuf bs_buf;
94 };
95 
99 class BOSON_API char_array_streambuf : public std::streambuf {
100  public:
107  char_array_streambuf(const char *data, size_t len);
108 
109  private:
114  int underflow() final override;
115 
116  // These are necessary since the buffer is not writeable, and thus we cannot call setg()
117  int uflow() final override;
118  int pbackfail(int ch) final override;
119  std::streamsize showmanyc() final override;
120 
128  std::streampos seekpos(std::streampos sp,
129  std::ios_base::openmode which = std::ios_base::in |
130  std::ios_base::out) final override;
131 
141  std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way,
142  std::ios_base::openmode which = std::ios_base::in |
143  std::ios_base::out) final override;
144  // Pointers to the data buffer.
145  const char *const _begin;
146  const char *const _end;
147  const char *_current;
148 };
149 
153 class BOSON_API bson_input_streambuf : public char_array_streambuf {
154  public:
155  bson_input_streambuf(const bsoncxx::document::view &v);
156 };
157 
166 class BOSON_API bson_istream : public std::istream {
167  public:
168  bson_istream(const bsoncxx::document::view &v) : std::istream(0), bs_buf(v) {
169  this->rdbuf(&bs_buf);
170  }
171 
172  private:
173  // TODO somehow set rdbuf(*sb) to private so people can't change the streambuffer?
174  bson_input_streambuf bs_buf;
175 };
176 
177 BOSON_INLINE_NAMESPACE_END
178 } // namespace boson
179 
180 #include <boson/config/postlude.hpp>
A wrapper from char_array_streambuf, that uses the data from a BSON document view as a buffer...
Definition: bson_streambuf.hpp:153
An istream that uses a BSON document as a buffer.
Definition: bson_streambuf.hpp:166
A streambuffer that accepts one or more BSON documents as bytes of BSON data.
Definition: bson_streambuf.hpp:35
Definition: bson_archiver.hpp:70
An ostream that writes bytes of BSON documents into a collection.
Definition: bson_streambuf.hpp:85
An input streambuf that uses an existing byte array as a buffer.
Definition: bson_streambuf.hpp:99