Class Option

Synopsis

#include <include/internal/catch_option.hpp>

template<typename T>
class Option

Description

No description yet.

Mentioned in

  • Other / Release Notes / Fixes

Inheritance

Decsendents: LazyStat

Methods

Option overload
~Option
none
operator bool
operator!
operator* overload
operator-> overload
operator= overload
reset
some
valueOr

Source

Lines 14-69 in include/internal/catch_option.hpp.

template<typename T>
class Option {
public:
    Option() : nullableValue( nullptr ) {}
    Option( T const& _value )
    : nullableValue( new( storage ) T( _value ) )
    {}
    Option( Option const& _other )
    : nullableValue( _other ? new( storage ) T( *_other ) : nullptr )
    {}
    ~Option() {
        reset();
    }
    Option& operator= ( Option const& _other ) {
        if( &_other != this ) {
            reset();
            if( _other )
                nullableValue = new( storage ) T( *_other );
        }
        return *this;
    }
    Option& operator = ( T const& _value ) {
        reset();
        nullableValue = new( storage ) T( _value );
        return *this;
    }
    void reset() {
        if( nullableValue )
            nullableValue->~T();
        nullableValue = nullptr;
    }
    T& operator*() { return *nullableValue; }
    T const& operator*() const { return *nullableValue; }
    T* operator->() { return nullableValue; }
    const T* operator->() const { return nullableValue; }
    T valueOr( T const& defaultValue ) const {
        return nullableValue ? *nullableValue : defaultValue;
    }
    bool some() const { return nullableValue != nullptr; }
    bool none() const { return nullableValue == nullptr; }
    bool operator !() const { return nullableValue == nullptr; }
    explicit operator bool() const {
        return some();
    }
private:
    T *nullableValue;
    alignas(alignof(T)) char storage[sizeof(T)];
};





Add Discussion as Guest

Log in