MongoDB C++ Driver current
value.hpp
1// Copyright 2020 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 <iostream>
18#include <memory>
19#include <vector>
20
21#include <bsoncxx/array/view_or_value.hpp>
22#include <bsoncxx/document/view_or_value.hpp>
23#include <bsoncxx/stdx/make_unique.hpp>
24#include <bsoncxx/types/bson_value/view.hpp>
25
26#include <bsoncxx/config/prelude.hpp>
27
28namespace bsoncxx {
29BSONCXX_INLINE_NAMESPACE_BEGIN
30
31namespace types {
32namespace bson_value {
33
44class BSONCXX_API value {
45 public:
55#define BSONCXX_ENUM(name, val) value(b_##name v);
56#include <bsoncxx/enums/type.hpp>
57#undef BSONCXX_ENUM
58
62 value(const char* v);
63
67 value(std::string v);
68
72 value(stdx::string_view v);
73
77 value(int32_t v);
78
82 value(int64_t v);
83
87 value(double v);
88
92 value(bool v);
93
98
103
107 value(std::chrono::milliseconds v);
108
112 value(std::nullptr_t);
113
118
123
132 value(std::vector<unsigned char> v, const binary_sub_type sub_type = {});
133
144 value(const uint8_t* data, size_t size, const binary_sub_type sub_type = {});
145
157 value(stdx::string_view collection, oid value);
158
167 value(stdx::string_view code, bsoncxx::document::view_or_value scope);
168
177 value(stdx::string_view regex, stdx::string_view options);
178
198 value(const type id, stdx::string_view v);
199
211 value(const type id);
212
233 value(const type id, uint64_t a, uint64_t b);
234
235 ~value();
236
237 value(const value&);
238 value& operator=(const value&);
239
240 value(value&&) noexcept;
241 value& operator=(value&&) noexcept;
242
246 explicit value(const view&);
247
251 bson_value::view view() const noexcept;
252
256 operator bson_value::view() const noexcept;
257
258 private:
259 friend class bsoncxx::document::element;
260
261 value(const std::uint8_t* raw,
262 std::uint32_t length,
263 std::uint32_t offset,
264 std::uint32_t keylen);
265
266 // Makes a copy of 'internal_value' and owns the copy.
267 value(void* internal_value);
268
269 friend value make_owning_bson(void* internal_value);
270
271 class BSONCXX_PRIVATE impl;
272 std::unique_ptr<impl> _impl;
273};
274
282BSONCXX_INLINE bool operator==(const value& lhs, const value& rhs) {
283 return (lhs.view() == rhs.view());
284}
285
286BSONCXX_INLINE bool operator!=(const value& lhs, const value& rhs) {
287 return !(lhs == rhs);
288}
289
293
301BSONCXX_INLINE bool operator==(const value& lhs, const view& rhs) {
302 return (lhs.view() == rhs);
303}
304
305BSONCXX_INLINE bool operator==(const view& lhs, const value& rhs) {
306 return (rhs == lhs);
307}
308
309BSONCXX_INLINE bool operator!=(const value& lhs, const view& rhs) {
310 return !(lhs == rhs);
311}
312
313BSONCXX_INLINE bool operator!=(const view& lhs, const value& rhs) {
314 return !(lhs == rhs);
315}
316
320
321} // namespace bson_value
322} // namespace types
323
324BSONCXX_INLINE_NAMESPACE_END
325} // namespace bsoncxx
326
327#include <bsoncxx/config/postlude.hpp>
A read-only, non-owning view of a BSON document.
Definition: view.hpp:40
Represents an IEEE 754-2008 BSON Decimal128 value in a platform-independent way.
Definition: decimal128.hpp:30
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33
Represents a MongoDB ObjectId.
Definition: oid.hpp:38
A variant owning type that represents any BSON type.
Definition: value.hpp:44
value(const char *v)
Constructs a BSON UTF-8 string value.
value(decimal128 v)
Constructs a BSON Decimal128 value.
value(const type id)
Constructs one of the following BSON values (each specified by the parenthesized type):
value(const type id, uint64_t a, uint64_t b)
Constructs one of the following BSON values (each specified by the parenthesized type):
value(stdx::string_view code, bsoncxx::document::view_or_value scope)
Constructs a BSON JavaScript code with scope value.
value(std::nullptr_t)
Constructs a BSON null value.
value(int64_t v)
Constructs a BSON 64-bit signed integer value.
value(bsoncxx::array::view v)
Constructs a BSON array value.
value(const uint8_t *data, size_t size, const binary_sub_type sub_type={})
Constructs a BSON binary data value.
value(stdx::string_view collection, oid value)
Constructs a BSON DBPointer value.
value(std::vector< unsigned char > v, const binary_sub_type sub_type={})
Constructs a BSON binary data value.
value(bool v)
Constructs a BSON boolean value.
value(int32_t v)
Constructs a BSON 32-bit signed integer value.
value(double v)
Constructs a BSON double value.
value(bsoncxx::document::view v)
Constructs a BSON document value.
value(const type id, stdx::string_view v)
Constructs one of the following BSON values (each specified by the parenthesized type):
bool operator==(const value &lhs, const view &rhs)
Compares a value with a view for (in)-equality.
Definition: value.hpp:301
bson_value::view view() const noexcept
Get a view over the bson_value owned by this object.
value(stdx::string_view regex, stdx::string_view options)
Constructs a BSON regex value with options.
value(stdx::string_view v)
Constructs a BSON UTF-8 string value.
value(std::string v)
Constructs a BSON UTF-8 string value.
value(std::chrono::milliseconds v)
Constructs a BSON date value.
value(oid v)
Constructs a BSON ObjectId value.
A view-only variant that can contain any BSON type.
Definition: view.hpp:44
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
type
An enumeration of each BSON type.
Definition: types.hpp:46
binary_sub_type
An enumeration of each BSON binary sub type.
Definition: types.hpp:66