54 return "[none ; scl::utils::None]";
96 template <
class... Args>
100 ret = std::move(value);
101 return std::move(ret);
135 if(!this->hasValue())
138 return this->payload.
get();
196 this->payload = std::move(rhs.payload);
209 this->payload = std::move(rhs.payload);
228 this->payload.
reset();
232 #define TPL template <class U, class = META::enable_if_t<\ 233 !META::is_same<META::decay_t<U>, META::decay_t<T>>()\ 234 && !META::is_same<META::decay_t<U>, None>()\ 235 && !META::is_same<META::decay_t<U>, Optional>()\ 266 this->payload.
construct(std::forward<U&&>(value));
277 this->payload.
construct(std::forward<U&&>(value));
301 operator bool()
const{
return this->hasValue(); }
323 return this->hasValue() ? this->
get() : defaultValue;
345 return this->doIfPresent(std::forward<F>(f));
356 if(!this->hasValue())
367 return this->doIfEmpty(std::forward<F>(f));
391 template <
class U,
class F>
393 if(this->hasValue()){
404 template <
class U,
class F>
415 if(this->hasValue()){
430 template <
class U,
class F>
432 return this->hasValue() ? mapper(this->
get()) :
none;
438 template <
class U,
class F>
440 return this->flatMap<U>(std::forward<F>(mapper));
462 #define SCL_TPL template <class U, class = META::enable_if_t<\ 463 !META::is_same<U, None>()\ 464 && !META::is_same<U, Optional>()\ 465 && !META::is_instance<scl::utils::Optional, U>()\ 468 bool operator==(
const U& t)
const{
return this->hasValue() && this->value() == t; }
472 bool operator<(
const U& t)
const{
return !this->hasValue() || this->value() < t; }
474 bool operator<=(
const U& t)
const{
return (*
this) == t || (*this) < t; }
476 bool operator>(
const U& t)
const{
return !((*this) <= t); }
494 #define SCL_TPL template<class U> 497 if(!this->hasValue())
507 bool operator<(const Optional<U>& o)
const{
508 if(!this->hasValue())
511 return o.hasValue() && this->value() < o.value();
515 bool operator<=(const Optional<U>& o)
const{
return (*
this) == o || (*this) < o; }
527 META::defines_scl_to_string<T>()
bool hasValue() const
Determine whether or not the storage holds a value.
Optional(const U &value)
Implicit conversion copy constructor.
bool operator==(const Optional< U > &o) const
Optional & operator=(Optional &&rhs) noexcept
Move assignment operator.
Optional()
Default construct (no value)
Optional< U > flatMap(F &&mapper) const
Flat maps this optional to an optional of another type.
bool operator<(const U &t) const
An empty class serving as the type of an empty Optional<T>
bool operator!=(const U &t) const
friend bool operator<(const U &t, const Optional &o)
static Optional inplace(Args &&... args)
Construct an optional inplace.
friend bool operator<=(const U &t, const Optional &o)
friend bool operator<=(None, const Optional &)
Global namespace of the SCL.
scl::tools::meta ::remove_cv_ref_t< Lhs > value_type
bool operator<=(None) const
bool operator>(const U &t) const
static constexpr Optional fromPointer(std::nullptr_t)
Optional< U > map(F &&mapper) const
Maps this Optional<T> to an Optional<U> via the provided mapper function (T -> U) ...
const value_type & get() const
Retrieves the value stored in this Optional<T>
constexpr bool operator>(None) const
static Optional ref(const value_type &ref)
Construct an optional from a reference to an object.
bool operator>=(None) const
bool operator==(const U &t) const
friend bool operator==(const U &t, const Optional &o)
T & get()
Access the underlying data.
constexpr const char * operator()(const None &) const
String representation of a None object.
const value_type *const ptr() const
Get an immutable pointer to the contained value.
friend bool operator>=(None, const Optional &o)
friend bool operator>(const U &t, const Optional &o)
friend bool operator<(None, const Optional &)
const Optional & doIfPresent(F &&f) const
Calls a function if the value is present.
Optional(const Optional &o)
Copy constructor.
const value_type & operator*() const
Access to the value.
const Optional & ifNone(F &&f) const
Alias for scl::utils::Optional::doIfEmpty.
Optional(const value_type &value)
Creates a non empty optional with the given value (copy)
Optional & operator=(const U &value)
Implicit conversion copy assignment.
friend bool operator>=(const U &t, const Optional &o)
friend bool operator!=(None, const Optional &o)
bool operator!=(None) const
Optional< U > flatMapTo(F &&mapper) const
Alias for Optional::flatMap.
constexpr bool operator>=(None) const
bool operator<=(const U &t) const
constexpr bool operator<(None) const
Optional(U &&value)
Implicit conversion move constructor.
const value_type & orThrow(const E &e) const
Tries to retrieve the value, throws the given exception if there's none.
bool operator==(None) const
Exception class used when attempting to access the value of an empty scl::utils::Optional.
friend bool operator>(None, const Optional &)
A class that allows the use of optional types (might be there)
Optional & operator=(U &&value)
Implicit conversion move assignment.
Optional(None _)
Instantiate an optional via an instance of None.
constexpr None none
A constant global variable of type None.
friend bool operator==(None, const Optional &o)
bool operator>=(const U &t) const
bool hasValue() const
Determines whether or not this Optional<T> is empty.
Optional< T > filter(F predicate) const
Filters the value according to the given predicate.
constexpr scl::utils::Placeholder _
const value_type & orElse(const value_type &defaultValue) const
Retrieves the value if there's one or return the default value provided.
Class that handles raw storage (and manual memory management) for a variable type.
bool operator>(None) const
friend bool operator!=(const U &t, const Optional &o)
Optional(Optional &&rhs)
Move constructor.
constexpr bool operator!=(None) const
Optional & operator=(None _)
Assign from None.
Optional & operator=(const Optional &rhs)
Copy assignment operator.
constexpr bool operator==(None) const
constexpr bool operator<=(None) const
static constexpr Optional fromPointer(const T *ptr)
Construct an optional from a pointer.
bool operator>=(const Optional< U > &o) const
bool operator>(const Optional< U > &o) const
const value_type & value() const
A semantic alias for Optional<T>::get.
const Optional & doIfEmpty(F &&f) const
Calls a function if there is no value.
bool operator!=(const Optional< U > &o) const
Optional< U > mapTo(F &&mapper) const
Alias for Optional::map.
bool operator<(None) const
Optional & operator=(const value_type &value)
Assign a value to this optional.
T & construct(Args &&... args)
Alias for RawStorage::constructor.
RawStorage & reset()
Alias for RawStorage::destroy.
RawStorage< T > payload
A raw storage to hold an instance of the object.
const Optional & ifSome(F &&f) const
Alias for scl::utils::Optional::doIfPresent.
const value_type *const operator->() const
Get an immutable pointer to the stored value.