MongoDB C++ Driver  mongocxx-3.6.2
All Classes Namespaces Functions Typedefs Enumerations Enumerator Friends Pages
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/stdx/optional.hpp>
20 
21 #include <bsoncxx/config/prelude.hpp>
22 
23 namespace bsoncxx {
24 BSONCXX_INLINE_NAMESPACE_BEGIN
25 
29 template <typename View, typename Value>
31  public:
32  using view_type = View;
33  using value_type = Value;
34 
38  static_assert(std::is_constructible<View, Value>::value,
39  "View type must be constructible from a Value");
40 
44  static_assert(std::is_default_constructible<View>::value,
45  "View type must be default constructible");
46 
51  BSONCXX_INLINE view_or_value() = default;
52 
60  BSONCXX_INLINE view_or_value(View view) : _view{view} {}
61 
68  BSONCXX_INLINE view_or_value(Value&& value) : _value(std::move(value)), _view(*_value) {}
69 
73  BSONCXX_INLINE view_or_value(const view_or_value& other)
74  : _value(other._value), _view(_value ? *_value : other._view) {}
75 
79  BSONCXX_INLINE view_or_value& operator=(const view_or_value& other) {
80  _value = other._value;
81  _view = _value ? *_value : other._view;
82  return *this;
83  }
84 
88 
90  BSONCXX_INLINE view_or_value(view_or_value &&other) noexcept
91  : _value{std::move(other._value)},
92  _view(_value ? *_value : std::move(other._view)) {
93  other._view = View();
94  other._value = stdx::nullopt;
95  }
96 
101  BSONCXX_INLINE view_or_value& operator=(view_or_value&& other) noexcept {
102  _value = std::move(other._value);
103  _view = _value ? *_value : std::move(other._view);
104  other._view = View();
105  other._value = stdx::nullopt;
106  return *this;
107  }
108 
114  BSONCXX_INLINE bool is_owning() const noexcept {
115  return static_cast<bool>(_value);
116  }
117 
123  BSONCXX_INLINE operator View() const {
124  return _view;
125  }
126 
132  BSONCXX_INLINE const View& view() const {
133  return _view;
134  }
135 
136  private:
137  stdx::optional<Value> _value;
138  View _view;
139 };
140 
148 template <typename View, typename Value>
149 BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs,
150  const view_or_value<View, Value>& rhs) {
151  return lhs.view() == rhs.view();
152 }
153 
154 template <typename View, typename Value>
155 BSONCXX_INLINE bool operator!=(const view_or_value<View, Value>& lhs,
156  const view_or_value<View, Value>& rhs) {
157  return !(lhs == rhs);
158 }
162 
170 template <typename View, typename Value>
171 BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs, View rhs) {
172  return lhs.view() == rhs;
173 }
174 
175 template <typename View, typename Value>
176 BSONCXX_INLINE bool operator==(View lhs, const view_or_value<View, Value>& rhs) {
177  return rhs == lhs;
178 }
179 
180 template <typename View, typename Value>
181 BSONCXX_INLINE bool operator!=(const view_or_value<View, Value>& lhs, View rhs) {
182  return !(lhs == rhs);
183 }
184 
185 template <typename View, typename Value>
186 BSONCXX_INLINE bool operator!=(View lhs, const view_or_value<View, Value>& rhs) {
187  return !(rhs == lhs);
188 }
189 
190 template <typename View, typename Value>
191 BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs, const Value& rhs) {
192  return lhs.view() == View(rhs);
193 }
194 
195 template <typename View, typename Value>
196 BSONCXX_INLINE bool operator==(const Value& lhs, const view_or_value<View, Value>& rhs) {
197  return rhs == lhs;
198 }
199 
200 template <typename View, typename Value>
201 BSONCXX_INLINE bool operator!=(const view_or_value<View, Value>& lhs, const Value& rhs) {
202  return !(lhs == rhs);
203 }
204 
205 template <typename View, typename Value>
206 BSONCXX_INLINE bool operator!=(const Value& lhs, const view_or_value<View, Value>& rhs) {
207  return !(rhs == lhs);
208 }
212 
213 BSONCXX_INLINE_NAMESPACE_END
214 } // namespace bsoncxx
215 
216 #include <bsoncxx/config/postlude.hpp>
bsoncxx::view_or_value::view_or_value
view_or_value()=default
Class View must be constructible from an instance of class Value.
bsoncxx
Top level namespace for MongoDB C++ BSON functionality.
Definition: element.hpp:24
bsoncxx::view_or_value
Class representing a view-or-value variant type.
Definition: view_or_value.hpp:30
bsoncxx::view_or_value::operator==
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:149
bsoncxx::view_or_value::view
const View & view() const
Get a View for the type.
Definition: view_or_value.hpp:132
bsoncxx::view_or_value::is_owning
bool is_owning() const noexcept
Return whether or not this view_or_value owns an underlying Value.
Definition: view_or_value.hpp:114
bsoncxx::view_or_value::view_or_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:73
bsoncxx::view_or_value::operator==
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:171
bsoncxx::view_or_value::view_or_value
view_or_value(View view)
Construct a view_or_value from a View.
Definition: view_or_value.hpp:60
bsoncxx::view_or_value::view_or_value
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:90
bsoncxx::view_or_value::view_or_value
view_or_value(Value &&value)
Constructs a view_or_value from a Value type.
Definition: view_or_value.hpp:68
bsoncxx::view_or_value::operator=
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:79
bsoncxx::view_or_value::operator=
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:101