Mangrove
The C++ Object Document Mapper for MongoDB
mapping_functions.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 <iostream>
20 
21 #include <bsoncxx/builder/basic/document.hpp>
22 #include <bsoncxx/stdx/optional.hpp>
23 
24 #include <boson/bson_archiver.hpp>
25 #include <boson/bson_streambuf.hpp>
26 
27 namespace boson {
28 BOSON_INLINE_NAMESPACE_BEGIN
29 
38 template <class T>
39 bsoncxx::document::value to_document(const T& obj) {
40  bsoncxx::stdx::optional<bsoncxx::document::value> doc;
41  bson_ostream bos([&doc](bsoncxx::document::value v) { doc = std::move(v); });
42  BSONOutputArchive archive(bos);
43  archive(obj);
44  return doc.value();
45 }
46 
55 template <class T>
56 bsoncxx::document::value to_dotted_notation_document(const T& obj) {
57  bsoncxx::stdx::optional<bsoncxx::document::value> doc;
58  bson_ostream bos([&doc](bsoncxx::document::value v) { doc = std::move(v); });
59  BSONOutputArchive archive(bos, true);
60  archive(obj);
61  return doc.value();
62 }
63 
74 template <class T>
75 T to_obj(bsoncxx::document::view v) {
76  static_assert(std::is_default_constructible<T>::value,
77  "Template type must be default constructible");
78  boson::bson_istream bis(v);
79  boson::BSONInputArchive archive(bis);
80  T obj;
81  archive(obj);
82  return obj;
83 }
84 
95 template <class T>
96 void to_obj(bsoncxx::document::view v, T& obj) {
97  boson::bson_istream bis(v);
98  boson::BSONInputArchive archive(bis);
99  archive(obj);
100 }
101 
102 /*
103 * This function converts an stdx::optional containing a BSON document value into an
104 * stdx::optional
105 * containing a deserialized object of the templated type.
106 * @tparam T A type that is serializable to BSON using a BSONArchiver.
107 * @param opt An optional BSON document view
108 * @return An optional object of type T
109 */
110 template <class T>
111 bsoncxx::stdx::optional<T> to_optional_obj(
112  const bsoncxx::stdx::optional<bsoncxx::document::value>& opt) {
113  if (opt) {
114  T obj = to_obj<T>(opt.value().view());
115  return {obj};
116  } else {
117  return {};
118  }
119 }
120 
128 template <class Iter>
130  : public std::iterator<std::input_iterator_tag, bsoncxx::document::value> {
131  public:
132  serializing_iterator(Iter ci) : _ci(ci) {
133  }
134 
135  serializing_iterator(const serializing_iterator& si) : _ci(si._ci) {
136  }
137 
138  serializing_iterator& operator++() {
139  ++_ci;
140  return *this;
141  }
142 
143  void operator++(int) {
144  operator++();
145  }
146 
147  bool operator==(const serializing_iterator& rhs) {
148  return _ci == rhs._ci;
149  }
150 
151  bool operator!=(const serializing_iterator& rhs) {
152  return _ci != rhs._ci;
153  }
154 
155  bsoncxx::document::value operator*() {
156  return to_document(*_ci);
157  }
158 
159  private:
160  Iter _ci;
161 };
162 
163 BOSON_INLINE_NAMESPACE_END
164 } // namespace boson
165 
166 #include <boson/config/postlude.hpp>
An iterator that wraps another iterator of serializable objects, and yields BSON document views corre...
Definition: mapping_functions.hpp:129
Definition: bson_archiver.hpp:469
An istream that uses a BSON document as a buffer.
Definition: bson_streambuf.hpp:166
Definition: bson_archiver.hpp:70