MongoDB C++ Driver mongocxx-3.10.1
Loading...
Searching...
No Matches
view_or_value.hpp
1// Copyright 2015 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 <type_traits>
18
19#include <bsoncxx/view_or_value-fwd.hpp>
20
21#include <bsoncxx/stdx/optional.hpp>
22
23#include <bsoncxx/config/prelude.hpp>
24
25namespace bsoncxx {
26namespace v_noabi {
27
31template <typename View, typename Value>
33 public:
34 using view_type = View;
35 using value_type = Value;
36
40 static_assert(std::is_constructible<View, Value>::value,
41 "View type must be constructible from a Value");
42
46 static_assert(std::is_default_constructible<View>::value,
47 "View type must be default constructible");
48
53 BSONCXX_INLINE view_or_value() = default;
54
62 BSONCXX_INLINE view_or_value(View view) : _view{view} {}
63
70 BSONCXX_INLINE view_or_value(Value&& value) : _value(std::move(value)), _view(*_value) {}
71
75 BSONCXX_INLINE view_or_value(const view_or_value& other)
76 : _value(other._value), _view(_value ? *_value : other._view) {}
77
81 BSONCXX_INLINE view_or_value& operator=(const view_or_value& other) {
82 _value = other._value;
83 _view = _value ? *_value : other._view;
84 return *this;
85 }
86
90
92 BSONCXX_INLINE view_or_value(view_or_value&& other) noexcept
93 : _value{std::move(other._value)}, _view(_value ? *_value : std::move(other._view)) {
94 other._view = View();
95 other._value = stdx::nullopt;
96 }
97
102 BSONCXX_INLINE view_or_value& operator=(view_or_value&& other) noexcept {
103 _value = std::move(other._value);
104 _view = _value ? *_value : std::move(other._view);
105 other._view = View();
106 other._value = stdx::nullopt;
107 return *this;
108 }
109
115 BSONCXX_INLINE bool is_owning() const noexcept {
116 return static_cast<bool>(_value);
117 }
118
124 BSONCXX_INLINE operator View() const {
125 return _view;
126 }
127
133 BSONCXX_INLINE const View& view() const {
134 return _view;
135 }
136
137 private:
138 stdx::optional<Value> _value;
139 View _view;
140};
141
149template <typename View, typename Value>
150BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs,
151 const view_or_value<View, Value>& rhs) {
152 return lhs.view() == rhs.view();
153}
154
155template <typename View, typename Value>
156BSONCXX_INLINE bool operator!=(const view_or_value<View, Value>& lhs,
157 const view_or_value<View, Value>& rhs) {
158 return !(lhs == rhs);
159}
163
171template <typename View, typename Value>
172BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs, View rhs) {
173 return lhs.view() == rhs;
174}
175
176template <typename View, typename Value>
177BSONCXX_INLINE bool operator==(View lhs, const view_or_value<View, Value>& rhs) {
178 return rhs == lhs;
179}
180
181template <typename View, typename Value>
182BSONCXX_INLINE bool operator!=(const view_or_value<View, Value>& lhs, View rhs) {
183 return !(lhs == rhs);
184}
185
186template <typename View, typename Value>
187BSONCXX_INLINE bool operator!=(View lhs, const view_or_value<View, Value>& rhs) {
188 return !(rhs == lhs);
189}
190
191template <typename View, typename Value>
192BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs, const Value& rhs) {
193 return lhs.view() == View(rhs);
194}
195
196template <typename View, typename Value>
197BSONCXX_INLINE bool operator==(const Value& lhs, const view_or_value<View, Value>& rhs) {
198 return rhs == lhs;
199}
200
201template <typename View, typename Value>
202BSONCXX_INLINE bool operator!=(const view_or_value<View, Value>& lhs, const Value& rhs) {
203 return !(lhs == rhs);
204}
205
206template <typename View, typename Value>
207BSONCXX_INLINE bool operator!=(const Value& lhs, const view_or_value<View, Value>& rhs) {
208 return !(rhs == lhs);
209}
213
214} // namespace v_noabi
215} // namespace bsoncxx
216
217namespace bsoncxx {
218
219using ::bsoncxx::v_noabi::operator==;
220using ::bsoncxx::v_noabi::operator!=;
221
222} // namespace bsoncxx
223
224#include <bsoncxx/config/postlude.hpp>
Class representing a view-or-value variant type.
Definition view_or_value.hpp:32
view_or_value(View view)
Construct a view_or_value from a View.
Definition view_or_value.hpp:62
view_or_value()=default
Class View must be constructible from an instance of class Value.
view_or_value(const view_or_value &other)
Construct a view_or_value from a copied view_or_value.
Definition view_or_value.hpp:75
bool is_owning() const noexcept
Return whether or not this view_or_value owns an underlying Value.
Definition view_or_value.hpp:115
view_or_value(Value &&value)
Constructs a view_or_value from a Value type.
Definition view_or_value.hpp:70
view_or_value & operator=(view_or_value &&other) noexcept
Assign to this view_or_value from a moved-in view_or_value.
Definition view_or_value.hpp:102
bool operator==(const view_or_value< View, Value > &lhs, View rhs)
Mixed (in)-equality operators for view_or_value against View or Value types.
Definition view_or_value.hpp:172
view_or_value(view_or_value &&other) noexcept
Construct a view_or_value from a moved-in view_or_value.
Definition view_or_value.hpp:92
const View & view() const
Get a View for the type.
Definition view_or_value.hpp:133
view_or_value & operator=(const view_or_value &other)
Assign to this view_or_value from a copied view_or_value.
Definition view_or_value.hpp:81
bool operator==(const view_or_value< View, Value > &lhs, const view_or_value< View, Value > &rhs)
Compare view_or_value objects for (in)-equality.
Definition view_or_value.hpp:150
The top-level namespace for bsoncxx library entities.
Definition element-fwd.hpp:19