libostd/ostd/maybe.hh

487 lines
12 KiB
C++
Raw Normal View History

/* Option type implementation. Inspired by libc++.
*
* This file is part of OctaSTD. See COPYING.md for futher information.
*/
2015-07-13 21:08:55 +02:00
#ifndef OSTD_MAYBE_HH
#define OSTD_MAYBE_HH
2015-07-13 21:08:55 +02:00
#include "ostd/types.hh"
#include "ostd/type_traits.hh"
#include "ostd/memory.hh"
#include "ostd/utility.hh"
#include "ostd/initializer_list.hh"
#include "ostd/functional.hh"
2015-07-13 21:07:14 +02:00
namespace ostd {
struct InPlace {};
constexpr InPlace in_place = InPlace();
struct Nothing {
explicit constexpr Nothing(int) {}
};
constexpr Nothing nothing = Nothing(0);
namespace detail {
template<typename T, bool = IsTriviallyDestructible<T>>
class MaybeStorage {
protected:
using Value = T;
union {
char p_null_state;
Value p_value;
};
bool p_engaged = false;
constexpr MaybeStorage(): p_null_state('\0') {}
2016-06-23 20:18:35 +02:00
MaybeStorage(MaybeStorage const &v): p_engaged(v.p_engaged) {
if (p_engaged) {
::new(address_of(p_value)) Value(v.p_value);
}
}
MaybeStorage(MaybeStorage &&v): p_engaged(v.p_engaged) {
if (p_engaged) {
2017-01-25 01:44:22 +01:00
::new(address_of(p_value)) Value(std::move(v.p_value));
}
}
2016-06-23 20:18:35 +02:00
constexpr MaybeStorage(Value const &v): p_value(v), p_engaged(true) {}
2017-01-25 01:44:22 +01:00
constexpr MaybeStorage(Value &&v): p_value(std::move(v)), p_engaged(true) {}
2016-09-02 19:52:47 +02:00
template<typename ...A>
constexpr MaybeStorage(InPlace, A &&...args):
2017-01-25 01:44:22 +01:00
p_value(std::forward<A>(args)...), p_engaged(true)
{}
~MaybeStorage() {
if (p_engaged) {
p_value.~Value();
}
}
};
template<typename T>
class MaybeStorage<T, true> {
protected:
using Value = T;
union {
char p_null_state;
Value p_value;
};
bool p_engaged = false;
constexpr MaybeStorage(): p_null_state('\0') {}
2016-06-23 20:18:35 +02:00
MaybeStorage(MaybeStorage const &v): p_engaged(v.p_engaged) {
if (p_engaged) {
::new(address_of(p_value)) Value(v.p_value);
}
}
MaybeStorage(MaybeStorage &&v): p_engaged(v.p_engaged) {
if (p_engaged) {
2017-01-25 01:44:22 +01:00
::new(address_of(p_value)) Value(std::move(v.p_value));
}
}
2016-06-23 20:18:35 +02:00
constexpr MaybeStorage(Value const &v): p_value(v), p_engaged(true) {}
2017-01-25 01:44:22 +01:00
constexpr MaybeStorage(Value &&v): p_value(std::move(v)), p_engaged(true) {}
template<typename ...A>
constexpr MaybeStorage(InPlace, A &&...args):
2017-01-25 01:44:22 +01:00
p_value(std::forward<A>(args)...), p_engaged(true)
{}
};
}
template<typename T>
class Maybe: private detail::MaybeStorage<T> {
using Base = detail::MaybeStorage<T>;
public:
using Value = T;
static_assert(
!IsReference<T>,
"Initialization of Maybe with a reference type is not allowed."
);
static_assert(
!IsSame<RemoveCv<T>, InPlace>,
"Initialization of Maybe with InPlace is not allowed."
);
static_assert(
!IsSame<RemoveCv<T>, Nothing>,
"Initialization of Maybe with Nothing is not allowed."
);
static_assert(
IsObject<T>,
"Initialization of Maybe with non-object type is not allowed."
);
static_assert(
IsDestructible<T>,
"Initialization of Maybe with a non-destructible object is not allowed."
);
constexpr Maybe() {}
2016-06-23 20:18:35 +02:00
Maybe(Maybe const &) = default;
Maybe(Maybe &&) = default;
constexpr Maybe(Nothing) {}
2016-06-23 20:18:35 +02:00
constexpr Maybe(Value const &v): Base(v) {}
2017-01-25 01:44:22 +01:00
constexpr Maybe(Value &&v): Base(std::move(v)) {}
2016-01-12 23:09:40 +01:00
template<typename ...A, typename = EnableIf<IsConstructible<T, A...>>>
constexpr explicit Maybe(InPlace, A &&...args):
2017-01-25 01:44:22 +01:00
Base(in_place, std::forward<A>(args)...)
{}
2015-07-15 23:03:51 +02:00
template<typename U, typename ...A, typename = EnableIf<
IsConstructible<T, std::initializer_list<U> &, A...>>
>
constexpr explicit Maybe(InPlace, std::initializer_list<U> il, A &&...args):
2017-01-25 01:44:22 +01:00
Base(in_place, il, std::forward<A>(args)...)
{}
~Maybe() = default;
Maybe &operator=(Nothing) {
if (this->p_engaged) {
this->p_value.~Value();
this->p_engaged = false;
}
return *this;
}
2016-06-23 20:18:35 +02:00
Maybe &operator=(Maybe const &v) {
if (this->p_engaged == v.p_engaged) {
if (this->p_engaged) {
this->p_value = v.p_value;
}
} else {
if (this->p_engaged) {
this->p_value.~Value();
} else {
::new(address_of(this->p_value)) Value(v.p_value);
}
this->p_engaged = v.p_engaged;
}
return *this;
}
Maybe &operator=(Maybe &&v) {
if (this->p_engaged == v.p_engaged) {
if (this->p_engaged) {
2017-01-25 01:44:22 +01:00
this->p_value = std::move(v.p_value);
}
} else {
if (this->p_engaged) {
this->p_value.~Value();
} else {
2017-01-25 01:44:22 +01:00
::new(address_of(this->p_value)) Value(std::move(v.p_value));
}
this->p_engaged = v.p_engaged;
}
return *this;
}
template<typename U, typename = EnableIf<
2016-01-12 23:24:40 +01:00
IsSame<RemoveReference<U>, Value> &&
2016-01-12 23:09:40 +01:00
IsConstructible<Value, U> && IsAssignable<Value &, U>
>>
Maybe &operator=(U &&v) {
if (this->p_engaged) {
2017-01-25 01:44:22 +01:00
this->p_value = std::forward<U>(v);
} else {
2017-01-25 01:44:22 +01:00
::new(address_of(this->p_value)) Value(std::forward<U>(v));
this->p_engaged = true;
}
return *this;
}
2016-01-12 23:09:40 +01:00
template<typename ...A, typename = EnableIf<IsConstructible<Value, A...>>>
void emplace(A &&...args) {
*this = nothing;
2017-01-25 01:44:22 +01:00
::new(address_of(this->p_value)) Value(std::forward<A>(args)...);
this->p_engaged = true;
}
template<typename U, typename ...A, typename = EnableIf<
2016-01-12 23:09:40 +01:00
IsConstructible<Value, std::initializer_list<U> &, A...>
>>
void emplace(std::initializer_list<U> il, A &&...args) {
*this = nothing;
::new(address_of(this->p_value))
2017-01-25 01:44:22 +01:00
Value(il, std::forward<A>(args)...);
this->p_engaged = true;
}
2016-06-23 20:18:35 +02:00
constexpr Value const *operator->() const {
return address_of(this->p_value);
}
2016-09-02 19:15:46 +02:00
constexpr Value *operator->() {
return address_of(this->p_value);
}
2016-09-02 19:15:46 +02:00
constexpr Value const &operator*() const & {
return this->p_value;
}
2016-09-02 19:15:46 +02:00
constexpr Value &operator*() & {
return this->p_value;
}
2016-09-02 19:15:46 +02:00
constexpr Value const &&operator*() const && {
2017-01-25 01:44:22 +01:00
return std::move(this->p_value);
2016-09-02 19:15:46 +02:00
}
constexpr Value &&operator*() && {
2017-01-25 01:44:22 +01:00
return std::move(this->p_value);
2016-09-02 19:15:46 +02:00
}
constexpr explicit operator bool() const { return this->p_engaged; }
2016-09-02 19:15:46 +02:00
constexpr Value const &value() const & {
return this->p_value;
}
2016-09-02 19:15:46 +02:00
constexpr Value &value() & {
return this->p_value;
}
2016-09-02 19:15:46 +02:00
constexpr Value const &&value() const && {
2017-01-25 01:44:22 +01:00
return std::move(this->p_value);
2016-09-02 19:15:46 +02:00
}
constexpr Value &&value() && {
2017-01-25 01:44:22 +01:00
return std::move(this->p_value);
2016-09-02 19:15:46 +02:00
}
template<typename U>
constexpr Value value_or(U &&v) const & {
static_assert(
IsCopyConstructible<Value>,
"Maybe<T>::value_or: T must be copy constructible"
);
static_assert(
IsConvertible<U, Value>,
"Maybe<T>::value_or: U must be convertible to T"
);
2017-01-25 01:44:22 +01:00
return this->p_engaged ? this->p_value : Value(std::forward<U>(v));
}
template<typename U>
2016-09-02 19:15:46 +02:00
constexpr Value value_or(U &&v) && {
static_assert(
IsMoveConstructible<Value>,
2016-09-02 19:20:31 +02:00
"Maybe<T>::value_or: T must be move constructible"
);
static_assert(
IsConvertible<U, Value>,
"Maybe<T>::value_or: U must be convertible to T"
);
2017-01-25 01:44:22 +01:00
return this->p_engaged ? std::move(this->p_value) : Value(std::forward<U>(v));
}
void swap(Maybe &v) {
2017-01-29 15:56:02 +01:00
using std::swap;
if (this->p_engaged == v.p_engaged) {
if (this->p_engaged) {
2017-01-29 15:56:02 +01:00
swap(this->p_value, v.p_value);
}
} else {
if (this->p_engaged) {
2017-01-25 01:44:22 +01:00
::new(address_of(v.p_value)) Value(std::move(this->p_value));
this->p_value.~Value();
} else {
2017-01-25 01:44:22 +01:00
::new(address_of(this->p_value)) Value(std::move(v.p_value));
v.p_value.~Value();
}
2017-01-29 15:56:02 +01:00
swap(this->p_engaged, v.p_engaged);
}
}
};
2017-01-29 15:56:02 +01:00
template<typename T>
inline void swap(Maybe<T> &a, Maybe<T> &b) {
a.swap(b);
}
/* maybe vs maybe */
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator==(Maybe<T> const &a, Maybe<T> const &b) {
return (bool(a) != bool(b)) ? false : (!bool(a) ? true : (*a == *b));
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator!=(Maybe<T> const &a, Maybe<T> const &b) {
return !(a == b);
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator<(Maybe<T> const &a, Maybe<T> const &b) {
return !bool(b) ? false : (!bool(a) ? true : (*a < *b));
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator>(Maybe<T> const &a, Maybe<T> const &b) {
return b < a;
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator<=(Maybe<T> const &a, Maybe<T> const &b) {
return !(b < a);
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator>=(Maybe<T> const &a, Maybe<T> const &b) {
return !(a < b);
}
/* maybe vs nothing */
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator==(Maybe<T> const &v, Nothing) {
return !bool(v);
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator==(Nothing, Maybe<T> const &v) {
return !bool(v);
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator!=(Maybe<T> const &v, Nothing) {
return bool(v);
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator!=(Nothing, Maybe<T> const &v) {
return bool(v);
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator<(Maybe<T> const &, Nothing) {
return false;
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator<(Nothing, Maybe<T> const &v) {
return bool(v);
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator<=(Maybe<T> const &v, Nothing) {
return !bool(v);
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator<=(Nothing, Maybe<T> const &) {
return true;
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator>(Maybe<T> const &v, Nothing) {
return bool(v);
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator>(Nothing, Maybe<T> const &) {
return false;
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator>=(Maybe<T> const &, Nothing) {
return true;
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator>=(Nothing, Maybe<T> const &v) {
return !bool(v);
}
/* maybe vs T */
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator==(Maybe<T> const &a, T const &b) {
return bool(a) ? (*a == b) : false;
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator==(T const &b, Maybe<T> const &a) {
return bool(a) ? (*a == b) : false;
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator!=(Maybe<T> const &a, T const &b) {
return bool(a) ? !(*a == b) : true;
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator!=(T const &b, Maybe<T> const &a) {
return bool(a) ? !(*a == b) : true;
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator<(Maybe<T> const &a, T const &b) {
return bool(a) ? Less<T>()(*a, b) : true;
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator<(T const &b, Maybe<T> const &a) {
return bool(a) ? Less<T>()(b, *a) : false;
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator<=(Maybe<T> const &a, T const &b) {
return !(a > b);
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator<=(T const &b, Maybe<T> const &a) {
return !(b > a);
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator>(Maybe<T> const &a, T const &b) {
return bool(a) ? (b < a) : true;
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator>(T const &b, Maybe<T> const &a) {
return bool(a) ? (a < b) : true;
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator>=(Maybe<T> const &a, T const &b) {
return !(a < b);
}
template<typename T>
2016-06-23 20:18:35 +02:00
inline constexpr bool operator>=(T const &b, Maybe<T> const &a) {
return !(b < a);
}
/* make maybe */
template<typename T>
inline constexpr Maybe<Decay<T>> make_maybe(T &&v) {
2017-01-25 01:44:22 +01:00
return Maybe<Decay<T>>(std::forward<T>(v));
}
2016-09-02 19:52:47 +02:00
template<typename T, typename ...A>
inline constexpr Maybe<T> make_maybe(A &&...args) {
2017-01-25 01:44:22 +01:00
return Maybe<T>(in_place, std::forward<A>(args)...);
2016-09-02 19:52:47 +02:00
}
template<typename T, typename U, typename ...A>
inline constexpr Maybe<T> make_maybe(std::initializer_list<U> il, A &&...args) {
2017-01-25 01:44:22 +01:00
return Maybe<T>(in_place, il, std::forward<A>(args)...);
2016-09-02 19:52:47 +02:00
}
2015-07-13 21:07:14 +02:00
} /* namespace ostd */
2016-02-07 22:17:15 +01:00
#endif