// This file is part of VSTGUI. It is subject to the license terms // in the LICENSE file found in the top-level directory of this // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE #pragma once #include //------------------------------------------------------------------------ namespace VSTGUI { //------------------------------------------------------------------------ /** simplified optional * * @ingroup new_in_4_5 */ template struct Optional { static_assert (std::is_default_constructible::value, "Type T must be default constructible"); Optional (T&& v); explicit Optional (const T& v); Optional (); Optional (Optional&&) = default; Optional& operator= (Optional&&) = default; Optional (const Optional&) = delete; Optional& operator= (const Optional&) = delete; explicit operator bool () const; const T* operator-> () const; T* operator-> (); const T& operator* () const&; T& operator* () &; T&& value (); const T& value () const; void reset (); private: std::pair _value; }; //------------------------------------------------------------------------ template inline Optional::type> makeOptional (T&& value) { return Optional::type> (std::forward (value)); } //------------------------------------------------------------------------ template inline Optional::Optional (T&& v) : _value {true, std::move (v)} { } //------------------------------------------------------------------------ template inline Optional::Optional (const T& v) : _value {true, v} { } //------------------------------------------------------------------------ template inline Optional::Optional () { _value.first = false; } //------------------------------------------------------------------------ template inline Optional::operator bool () const { return _value.first; } //------------------------------------------------------------------------ template inline const T* Optional::operator-> () const { return _value.second; } //------------------------------------------------------------------------ template inline T* Optional::operator-> () { return &_value.second; } //------------------------------------------------------------------------ template inline const T& Optional::operator* () const& { return _value.second; } //------------------------------------------------------------------------ template inline T& Optional::operator* () & { return _value.second; } //------------------------------------------------------------------------ template inline T&& Optional::value () { assert (_value.first); return std::move (_value.second); } //------------------------------------------------------------------------ template inline const T& Optional::value () const { assert (_value.first); return _value.second; } //------------------------------------------------------------------------ template inline void Optional::reset () { _value.first = false; _value.second = {}; } //------------------------------------------------------------------------ } // VSTGUI