MongoDB C++ Driver mongocxx-3.6.7
value.hpp
1// Copyright 2014 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 <cstdlib>
18#include <memory>
19
20#include <bsoncxx/document/view.hpp>
21
22#include <bsoncxx/config/prelude.hpp>
23
24namespace bsoncxx {
25BSONCXX_INLINE_NAMESPACE_BEGIN
26namespace document {
27
33class BSONCXX_API value {
34 public:
35 using deleter_type = void (*)(std::uint8_t*);
36 using unique_ptr_type = std::unique_ptr<uint8_t[], deleter_type>;
37
50 value(std::uint8_t* data, std::size_t length, deleter_type dtor);
51
61 value(unique_ptr_type ptr, std::size_t length);
62
72
73 value(const value&);
74 value& operator=(const value&);
75
76 value(value&&) noexcept = default;
77 value& operator=(value&&) noexcept = default;
78
82 BSONCXX_INLINE document::view view() const noexcept;
83
89 BSONCXX_INLINE operator document::view() const noexcept;
90
100 unique_ptr_type release();
101
106 void reset(document::view view);
107
108 private:
109 unique_ptr_type _data;
110 std::size_t _length{0};
111};
112
113BSONCXX_INLINE document::view value::view() const noexcept {
114 return document::view{static_cast<uint8_t*>(_data.get()), _length};
115}
116
117BSONCXX_INLINE value::operator document::view() const noexcept {
118 return view();
119}
120
128BSONCXX_INLINE bool operator==(const value& lhs, const value& rhs) {
129 return (lhs.view() == rhs.view());
130}
131
132BSONCXX_INLINE bool operator!=(const value& lhs, const value& rhs) {
133 return !(lhs == rhs);
134}
135
139
140} // namespace document
141BSONCXX_INLINE_NAMESPACE_END
142} // namespace bsoncxx
143
144#include <bsoncxx/config/postlude.hpp>
A read-only BSON document that owns its underlying buffer.
Definition: value.hpp:33
value(std::uint8_t *data, std::size_t length, deleter_type dtor)
Constructs a value from a buffer.
bool operator==(const value &lhs, const value &rhs)
Compares two document values for (in)-equality.
Definition: value.hpp:128
document::view view() const noexcept
Get a view over the document owned by this value.
Definition: value.hpp:113
value(unique_ptr_type ptr, std::size_t length)
Constructs a value from a std::unique_ptr to a buffer.
value(document::view view)
Constructs a value from a view of a document.
A read-only, non-owning view of a BSON document.
Definition: view.hpp:33
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24