6#include <bond/core/config.h>
8#include "detail/alloc.h"
11#include <boost/none.hpp>
12#include <boost/optional/optional.hpp>
13#include <boost/utility/enable_if.hpp>
23[[noreturn]]
void NothingException();
42 template <
typename... Args>
45 _value.emplace(
value, std::forward<Args>(args)...);
48 template <
typename... Args>
51 _value.emplace(std::move(
value), std::forward<Args>(args)...);
54 maybe_common(maybe_common&& that) BOND_NOEXCEPT_IF(std::is_nothrow_move_constructible<boost::optional<T>>
::value)
55 : _value(
std::move(that._value))
61 that._value = boost::none;
68 return !
static_cast<bool>(_value);
74 explicit operator bool() const BOND_NOEXCEPT
89 template<
typename... Args>
92 _value.emplace(std::forward<Args>(args)...);
126 T&
value(
const std::nothrow_t&) BOND_NOEXCEPT
138 const T&
value(
const std::nothrow_t&)
const BOND_NOEXCEPT
157 return lhs._value == rhs;
168 return !(lhs == rhs);
179 return lhs == rhs._value;
190 return !(lhs == rhs);
194 boost::optional<T> _value;
198template <
typename T,
typename Enable =
void>
struct
200 : std::false_type {};
202template <
typename T>
struct
203has_allocator<T, typename boost::enable_if<std::is_class<typename T::allocator_type> >::type>
209template <
typename T,
typename Enabled =
void>
228class maybe<T, typename boost::disable_if<detail::has_allocator<T> >::type>
246 : detail::maybe_common<T>(value)
254 : detail::maybe_common<T>(
std::move(value))
257 maybe& operator=(
const maybe&) =
default;
258 maybe& operator=(maybe&&) =
default;
263 this->emplace(value);
271 this->emplace(std::move(value));
283 return lhs._value == rhs._value;
293 return lhs._value != rhs._value;
304 if (this->is_nothing())
309 return *this->_value;
316 swap(this->_value, that._value);
336class maybe<T, typename boost::enable_if<detail::has_allocator<T> >::type>
363 : detail::maybe_common<T>(),
366 if (!that.is_nothing())
368 this->emplace(*that._value, alloc);
378 : detail::maybe_common<T>(),
381 if (!that.is_nothing())
383 this->emplace(std::move(*that._value), alloc);
386 that._value = boost::none;
395 : detail::maybe_common<T>(),
401 : detail::maybe_common<T>(value),
409 : detail::maybe_common<T>(
std::move(value)),
413 maybe& operator=(
const maybe&) =
default;
414 maybe& operator=(maybe&&) =
default;
419 this->emplace(value);
427 this->emplace(std::move(value));
434 bool operator==(
const alloc_holder&) =
delete;
444 return lhs._value == rhs._value;
454 return lhs._value != rhs._value;
465 if (this->is_nothing())
467 this->emplace(base_alloc_holder().get());
470 return *this->_value;
477 swap(this->_value, that._value);
478 swap(base_alloc_holder(), that.base_alloc_holder());
485 return base_alloc_holder().get();
490 const detail::maybe_common<T>& base_common() const BOND_NOEXCEPT {
return *
this; }
491 alloc_holder& base_alloc_holder() BOND_NOEXCEPT {
return *
this; }
492 const alloc_holder& base_alloc_holder() const BOND_NOEXCEPT {
return *
this; }
497inline void swap(maybe<T>& x, maybe<T>& y)
Helper type that holds an allocator.
Definition: alloc.h:22
maybe_common & operator=(maybe_common &&)=default
Move assign from another maybe.
void set_nothing() BOND_NOEXCEPT
Set to nothing.
Definition: maybe.h:80
T & value()
Get a reference to the value.
Definition: maybe.h:98
friend bool operator!=(const maybe_common &lhs, const T &rhs)
Compares a maybe and a value for inequality.
Definition: maybe.h:166
T value_type
The type of the value that may be inside the maybe.
Definition: maybe.h:36
friend bool operator==(const T &lhs, const maybe_common &rhs)
Compares a value and a maybe for equality.
Definition: maybe.h:177
maybe_common & operator=(const maybe_common &)=default
Assign from another maybe.
const T & value() const
Get a constant reference to the value.
Definition: maybe.h:110
const T & value(const std::nothrow_t &) const BOND_NOEXCEPT
Get a constant reference to the value.
Definition: maybe.h:138
T & emplace(Args &&... args)
Construct a value in place.
Definition: maybe.h:90
friend bool operator==(const maybe_common &lhs, const T &rhs)
Compares a maybe and a value for equality.
Definition: maybe.h:155
bool is_nothing() const BOND_NOEXCEPT
Check if this object contains nothing.
Definition: maybe.h:66
friend bool operator!=(const T &lhs, const maybe_common &rhs)
Compares and a value and a maybe for inequality.
Definition: maybe.h:188
T & value(const std::nothrow_t &) BOND_NOEXCEPT
Get a reference to the value.
Definition: maybe.h:126
friend bool operator==(const maybe &lhs, const maybe &rhs)
Compares two maybes for value equality.
Definition: maybe.h:281
T & set_value()
Set the maybe to hold a value, if needed.
Definition: maybe.h:302
friend bool operator!=(const maybe &lhs, const maybe &rhs)
Compares two maybes for value inequality.
Definition: maybe.h:291
maybe(maybe &&)=default
Move a maybe.
maybe & operator=(const T &value)
Assign by copying a value.
Definition: maybe.h:261
void swap(maybe &that)
Swap this object with that.
Definition: maybe.h:313
maybe(const maybe &)=default
Copy a maybe.
maybe(T &&value)
Create a maybe that holds a value by moving from value.
Definition: maybe.h:253
maybe & operator=(T &&value)
Move-assign from a value.
Definition: maybe.h:269
maybe()=default
Create a maybe that holds nothing.
maybe(const T &value)
Create a maybe that holds a value by copying value.
Definition: maybe.h:245
maybe & operator=(const T &value)
Assign by copying value.
Definition: maybe.h:417
friend bool operator==(const maybe &lhs, const maybe &rhs)
Compares two maybes for value equality.
Definition: maybe.h:442
maybe(const maybe &that, const allocator_type &alloc)
Allocator-extended copy constructor. Uses alloc as the new allocator, makes a copy of that.
Definition: maybe.h:362
friend bool operator!=(const maybe &lhs, const maybe &rhs)
Compares two maybes for value inequality.
Definition: maybe.h:452
typename T::allocator_type allocator_type
The type of the allocator in use.
Definition: maybe.h:349
maybe(T &&value)
Create a maybe that holds a value by moving from value.
Definition: maybe.h:408
maybe(const allocator_type &alloc)
Construct a maybe that holds nothing, but remember the allocator so that it can be used to construct ...
Definition: maybe.h:394
maybe(const maybe &)=default
Copy a maybe.
maybe(const T &value)
Create a maybe that holds a copy of value.
Definition: maybe.h:400
void swap(maybe &that)
Swap this object with that.
Definition: maybe.h:474
maybe & operator=(T &&value)
Move-assign from value.
Definition: maybe.h:425
T & set_value()
Set to non-empty, if needed.
Definition: maybe.h:463
allocator_type get_allocator() const BOND_NOEXCEPT
Get the allocator that this maybe uses.
Definition: maybe.h:483
maybe(maybe &&)=default
Move a maybe.
maybe(maybe &&that, const allocator_type &alloc)
Allocator-extended move constructor. Uses alloc as the new allocator, makes moved from that.
Definition: maybe.h:377
namespace bond
Definition: apply.h:17
void swap(blob &src, blob &dst) BOND_NOEXCEPT
Swap two blobs.
Definition: blob.h:277
boost::enable_if< is_signed_int_or_enum< SignedT >, bool >::type operator==(const Variant &variant, SignedT value)
Compares variant for equality against the provided signed integer or enum value.
Definition: metadata.h:24