MongoDB C++ Driver  mongocxx-3.10.2
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 
25 namespace bsoncxx {
26 namespace v_noabi {
27 
31 template <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 
149 template <typename View, typename Value>
150 BSONCXX_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 
155 template <typename View, typename Value>
156 BSONCXX_INLINE bool operator!=(const view_or_value<View, Value>& lhs,
157  const view_or_value<View, Value>& rhs) {
158  return !(lhs == rhs);
159 }
163 
171 template <typename View, typename Value>
172 BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs, View rhs) {
173  return lhs.view() == rhs;
174 }
175 
176 template <typename View, typename Value>
177 BSONCXX_INLINE bool operator==(View lhs, const view_or_value<View, Value>& rhs) {
178  return rhs == lhs;
179 }
180 
181 template <typename View, typename Value>
182 BSONCXX_INLINE bool operator!=(const view_or_value<View, Value>& lhs, View rhs) {
183  return !(lhs == rhs);
184 }
185 
186 template <typename View, typename Value>
187 BSONCXX_INLINE bool operator!=(View lhs, const view_or_value<View, Value>& rhs) {
188  return !(rhs == lhs);
189 }
190 
191 template <typename View, typename Value>
192 BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs, const Value& rhs) {
193  return lhs.view() == View(rhs);
194 }
195 
196 template <typename View, typename Value>
197 BSONCXX_INLINE bool operator==(const Value& lhs, const view_or_value<View, Value>& rhs) {
198  return rhs == lhs;
199 }
200 
201 template <typename View, typename Value>
202 BSONCXX_INLINE bool operator!=(const view_or_value<View, Value>& lhs, const Value& rhs) {
203  return !(lhs == rhs);
204 }
205 
206 template <typename View, typename Value>
207 BSONCXX_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 
217 namespace bsoncxx {
218 
219 using ::bsoncxx::v_noabi::operator==;
220 using ::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
const View & view() const
Get a View for the type.
Definition: view_or_value.hpp:133
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
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 & 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
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
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
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