MongoDB C++ Driver  mongocxx-3.9.0
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 inline namespace v_noabi {
28 template <typename View, typename Value>
30  public:
31  using view_type = View;
32  using value_type = Value;
33 
37  static_assert(std::is_constructible<View, Value>::value,
38  "View type must be constructible from a Value");
39 
43  static_assert(std::is_default_constructible<View>::value,
44  "View type must be default constructible");
45 
50  BSONCXX_INLINE view_or_value() = default;
51 
59  BSONCXX_INLINE view_or_value(View view) : _view{view} {}
60 
67  BSONCXX_INLINE view_or_value(Value&& value) : _value(std::move(value)), _view(*_value) {}
68 
72  BSONCXX_INLINE view_or_value(const view_or_value& other)
73  : _value(other._value), _view(_value ? *_value : other._view) {}
74 
78  BSONCXX_INLINE view_or_value& operator=(const view_or_value& other) {
79  _value = other._value;
80  _view = _value ? *_value : other._view;
81  return *this;
82  }
83 
87 
89  BSONCXX_INLINE view_or_value(view_or_value&& other) noexcept
90  : _value{std::move(other._value)}, _view(_value ? *_value : std::move(other._view)) {
91  other._view = View();
92  other._value = stdx::nullopt;
93  }
94 
99  BSONCXX_INLINE view_or_value& operator=(view_or_value&& other) noexcept {
100  _value = std::move(other._value);
101  _view = _value ? *_value : std::move(other._view);
102  other._view = View();
103  other._value = stdx::nullopt;
104  return *this;
105  }
106 
112  BSONCXX_INLINE bool is_owning() const noexcept {
113  return static_cast<bool>(_value);
114  }
115 
121  BSONCXX_INLINE operator View() const {
122  return _view;
123  }
124 
130  BSONCXX_INLINE const View& view() const {
131  return _view;
132  }
133 
134  private:
135  stdx::optional<Value> _value;
136  View _view;
137 };
138 
146 template <typename View, typename Value>
147 BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs,
148  const view_or_value<View, Value>& rhs) {
149  return lhs.view() == rhs.view();
150 }
151 
152 template <typename View, typename Value>
153 BSONCXX_INLINE bool operator!=(const view_or_value<View, Value>& lhs,
154  const view_or_value<View, Value>& rhs) {
155  return !(lhs == rhs);
156 }
160 
168 template <typename View, typename Value>
169 BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs, View rhs) {
170  return lhs.view() == rhs;
171 }
172 
173 template <typename View, typename Value>
174 BSONCXX_INLINE bool operator==(View lhs, const view_or_value<View, Value>& rhs) {
175  return rhs == lhs;
176 }
177 
178 template <typename View, typename Value>
179 BSONCXX_INLINE bool operator!=(const view_or_value<View, Value>& lhs, View rhs) {
180  return !(lhs == rhs);
181 }
182 
183 template <typename View, typename Value>
184 BSONCXX_INLINE bool operator!=(View lhs, const view_or_value<View, Value>& rhs) {
185  return !(rhs == lhs);
186 }
187 
188 template <typename View, typename Value>
189 BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs, const Value& rhs) {
190  return lhs.view() == View(rhs);
191 }
192 
193 template <typename View, typename Value>
194 BSONCXX_INLINE bool operator==(const Value& lhs, const view_or_value<View, Value>& rhs) {
195  return rhs == lhs;
196 }
197 
198 template <typename View, typename Value>
199 BSONCXX_INLINE bool operator!=(const view_or_value<View, Value>& lhs, const Value& rhs) {
200  return !(lhs == rhs);
201 }
202 
203 template <typename View, typename Value>
204 BSONCXX_INLINE bool operator!=(const Value& lhs, const view_or_value<View, Value>& rhs) {
205  return !(rhs == lhs);
206 }
210 
211 } // namespace v_noabi
212 } // namespace bsoncxx
213 
214 #include <bsoncxx/config/postlude.hpp>
Class representing a view-or-value variant type.
Definition: view_or_value.hpp:29
view_or_value(View view)
Construct a view_or_value from a View.
Definition: view_or_value.hpp:59
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:72
const View & view() const
Get a View for the type.
Definition: view_or_value.hpp:130
bool is_owning() const noexcept
Return whether or not this view_or_value owns an underlying Value.
Definition: view_or_value.hpp:112
view_or_value(Value &&value)
Constructs a view_or_value from a Value type.
Definition: view_or_value.hpp:67
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:169
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:99
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:78
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:89
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:147
The top-level namespace for bsoncxx library entities.
Definition: element.hpp:24