Realm C++ SDK Version v2.2.0

bson.hpp

1
2//
3// Copyright 2024 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//
18
19#ifndef REALMCXX_BSON_HPP
20#define REALMCXX_BSON_HPP
21
22#include <cpprealm/types.hpp>
23#include <cpprealm/internal/bridge/utils.hpp>
24#include <any>
25
26namespace realm {
27 namespace bson {
28 class Bson;
29 class BsonDocument;
30 struct RegularExpression;
31 }
32
33
60 struct bsoncxx {
61
62 using array = std::vector<bsoncxx>;
63
64 enum class type {
65 b_null,
66 b_int32,
67 b_int64,
68 b_bool,
69 b_double,
70 b_string,
71 b_binary,
72 b_timestamp,
73 b_datetime,
74 b_objectId,
75 b_decimal128,
76 b_regular_expression,
77 b_max_key,
78 b_min_key,
79 b_document,
80 b_array,
81 b_uuid
82 };
83
84 struct document {
85 using CoreDocument = realm::bson::BsonDocument;
86
87 struct value {
88 value& operator=(const bsoncxx& v);
89 operator bsoncxx() const;
90 const std::string key;
91 private:
92 friend struct bsoncxx;
93 explicit value(CoreDocument* d, const std::string& k)
94 : key(k), m_document(d) {}
95 CoreDocument* m_document;
96 };
97
98 struct iterator {
99 std::pair<std::string, bsoncxx> operator*();
100 iterator& operator++();
101 iterator& operator--();
102 iterator& operator++(int);
103 iterator& operator--(int);
104 bool operator!=(const iterator& rhs) const noexcept;
105 bool operator==(const iterator& rhs) const noexcept;
106 private:
107 friend struct document;
108 iterator(std::any&& i) : m_iterator(i) { }
109 std::any m_iterator;
110 };
111 iterator begin();
112 iterator end();
113
114 document() noexcept;
115 document(const std::initializer_list<std::pair<std::string, bsoncxx>>&) noexcept;
116 document(const document&);
118 ~document();
119 document& operator=(const document&);
120 document& operator=(document&&);
121 void insert(const std::string& key, const bsoncxx& value);
122 bool empty();
123 size_t size() const;
124 value operator[](const std::string&);
125 operator CoreDocument() const;
126 document(CoreDocument&) noexcept;
127 private:
128 friend struct bsoncxx;
129#ifdef CPPREALM_HAVE_GENERATED_BRIDGE_TYPES
130 internal::bridge::storage::BsonIndexedMap m_document[1];
131#else
132 std::shared_ptr<CoreDocument> m_document;
133#endif
134 };
135
137 struct min_key {};
139 struct max_key {};
140
144 enum class option : uint8_t {
145 none,
146 ignore_case = 1,
147 multiline = 2,
148 dotall = 4,
149 extended = 8
150 };
151 regular_expression(const std::string& pattern, const std::string& options);
152 regular_expression(const std::string& pattern, option options);
154 option get_options() const;
155 std::string get_pattern() const;
156 operator realm::bson::RegularExpression() const;
157 private:
158 friend struct bsoncxx;
159 regular_expression(const realm::bson::RegularExpression&);
160 option m_options;
161 std::string m_pattern;
162 };
163
165 struct timestamp {
166 explicit timestamp(uint32_t seconds, uint32_t increment) : m_seconds(seconds), m_increment(increment) {}
167 uint32_t get_seconds() const noexcept { return m_seconds; }
168 uint32_t get_increment() const noexcept { return m_increment; }
169 private:
170 friend struct bsoncxx;
171 const uint32_t m_seconds;
172 const uint32_t m_increment;
173 };
174
176 struct date {
177 explicit date(const std::chrono::time_point<std::chrono::system_clock>& d) : m_date(d) {}
178 std::chrono::time_point<std::chrono::system_clock> get_date() const noexcept { return m_date; }
179 private:
180 friend struct bsoncxx;
181 const std::chrono::time_point<std::chrono::system_clock> m_date;
182 };
183
184 bsoncxx() noexcept;
185 ~bsoncxx() noexcept;
186 bsoncxx(const bsoncxx&) noexcept;
187 bsoncxx(bsoncxx&&) noexcept;
188 bsoncxx& operator=(const bsoncxx&) noexcept;
189 bsoncxx& operator=(bsoncxx&&) noexcept;
190
194 bsoncxx(int32_t) noexcept;
195 bsoncxx(int64_t) noexcept;
196 bsoncxx(bool) noexcept;
197 bsoncxx(double) noexcept;
198 bsoncxx(min_key) noexcept;
199 bsoncxx(max_key) noexcept;
200 bsoncxx(const timestamp&) noexcept;
201 bsoncxx(const date&) noexcept;
202 bsoncxx(const decimal128&) noexcept;
203 bsoncxx(const object_id&) noexcept;
204 bsoncxx(const uuid&) noexcept;
205 bsoncxx(const regular_expression&) noexcept;
206 bsoncxx(const std::vector<uint8_t>&) noexcept;
207 bsoncxx(const std::string&) noexcept;
208 bsoncxx(const char*) noexcept;
209 bsoncxx(const document&) noexcept;
210 bsoncxx(const array&) noexcept;
211 type get_type() const;
212 operator realm::bson::Bson() const;
213
214 operator std::nullopt_t() const;
215 operator int32_t() const;
216 operator int64_t() const;
217 operator bool() const;
218 operator double() const;
219 operator min_key() const;
220 operator max_key() const;
221 operator timestamp() const;
222 operator date() const;
223 operator decimal128() const;
224 operator object_id() const;
225 operator uuid() const;
226 operator regular_expression() const;
227 operator std::vector<uint8_t>() const;
228 operator std::string() const;
229 operator document() const;
230 operator array() const;
231
232 std::string to_string() const;
233 std::string to_json() const;
234 bsoncxx(realm::bson::Bson&) noexcept;
235 private:
236#ifdef CPPREALM_HAVE_GENERATED_BRIDGE_TYPES
237 internal::bridge::storage::Bson m_bson[1];
238#else
239 std::shared_ptr<realm::bson::Bson> m_bson;
240#endif
241 };
242
243 bool operator==(const bsoncxx& lhs, const bsoncxx& rhs);
244 bool operator!=(const bsoncxx& lhs, const bsoncxx& rhs);
245 bool operator==(const bsoncxx::document& lhs, const bsoncxx::document& rhs);
246 bool operator!=(const bsoncxx::document& lhs, const bsoncxx::document& rhs);
247 bool operator==(const bsoncxx::regular_expression& lhs, const bsoncxx::regular_expression& rhs) noexcept;
248 bool operator!=(const bsoncxx::regular_expression& lhs, const bsoncxx::regular_expression& rhs) noexcept;
249 bsoncxx::regular_expression::option operator|(const bsoncxx::regular_expression::option& lhs,
250 const bsoncxx::regular_expression::option& rhs) noexcept;
251 bsoncxx::regular_expression::option operator&(const bsoncxx::regular_expression::option& lhs,
252 const bsoncxx::regular_expression::option& rhs) noexcept;
253} // namespace realm
254
255#endif//REALMCXX_BSON_HPP
A type representing a BSON datetime.
Definition: bson.hpp:176
Definition: bson.hpp:98
Definition: bson.hpp:87
Definition: bson.hpp:84
MaxKey will always be the greatest value when comparing to other BSON types.
Definition: bson.hpp:139
MinKey will always be the smallest value when comparing to other BSON types.
Definition: bson.hpp:137
Definition: bson.hpp:143
A type representing a BSON timestamp.
Definition: bson.hpp:165
Definition: bson.hpp:60
Definition: types.hpp:75
Definition: types.hpp:56
Definition: types.hpp:35