MongoDB C++ Driver  mongocxx-3.7.0
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)}, _view(_value ? *_value : std::move(other._view)) {
92  other._view = View();
93  other._value = stdx::nullopt;
94  }
95 
100  BSONCXX_INLINE view_or_value& operator=(view_or_value&& other) noexcept {
101  _value = std::move(other._value);
102  _view = _value ? *_value : std::move(other._view);
103  other._view = View();
104  other._value = stdx::nullopt;
105  return *this;
106  }
107 
113  BSONCXX_INLINE bool is_owning() const noexcept {
114  return static_cast<bool>(_value);
115  }
116 
122  BSONCXX_INLINE operator View() const {
123  return _view;
124  }
125 
131  BSONCXX_INLINE const View& view() const {
132  return _view;
133  }
134 
135  private:
136  stdx::optional<Value> _value;
137  View _view;
138 };
139 
147 template <typename View, typename Value>
148 BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs,
149  const view_or_value<View, Value>& rhs) {
150  return lhs.view() == rhs.view();
151 }
152 
153 template <typename View, typename Value>
154 BSONCXX_INLINE bool operator!=(const view_or_value<View, Value>& lhs,
155  const view_or_value<View, Value>& rhs) {
156  return !(lhs == rhs);
157 }
161 
169 template <typename View, typename Value>
170 BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs, View rhs) {
171  return lhs.view() == rhs;
172 }
173 
174 template <typename View, typename Value>
175 BSONCXX_INLINE bool operator==(View lhs, const view_or_value<View, Value>& rhs) {
176  return rhs == lhs;
177 }
178 
179 template <typename View, typename Value>
180 BSONCXX_INLINE bool operator!=(const view_or_value<View, Value>& lhs, View rhs) {
181  return !(lhs == rhs);
182 }
183 
184 template <typename View, typename Value>
185 BSONCXX_INLINE bool operator!=(View lhs, const view_or_value<View, Value>& rhs) {
186  return !(rhs == lhs);
187 }
188 
189 template <typename View, typename Value>
190 BSONCXX_INLINE bool operator==(const view_or_value<View, Value>& lhs, const Value& rhs) {
191  return lhs.view() == View(rhs);
192 }
193 
194 template <typename View, typename Value>
195 BSONCXX_INLINE bool operator==(const Value& lhs, const view_or_value<View, Value>& rhs) {
196  return rhs == lhs;
197 }
198 
199 template <typename View, typename Value>
200 BSONCXX_INLINE bool operator!=(const view_or_value<View, Value>& lhs, const Value& rhs) {
201  return !(lhs == rhs);
202 }
203 
204 template <typename View, typename Value>
205 BSONCXX_INLINE bool operator!=(const Value& lhs, const view_or_value<View, Value>& rhs) {
206  return !(rhs == lhs);
207 }
211 
212 BSONCXX_INLINE_NAMESPACE_END
213 } // namespace bsoncxx
214 
215 #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:148
bsoncxx::view_or_value::view
const View & view() const
Get a View for the type.
Definition: view_or_value.hpp:131
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:113
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:170
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:100