#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2023 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_SUPPORT_OPTIONAL_H #define NUCLEX_SUPPORT_OPTIONAL_H #include "Nuclex/Support/Config.h" #if !defined(NUCLEX_SUPPORT_SOURCE) #warning Nuclex::Support::Optional has been deprecated in favor of C++17 std::optional #endif //#include // #include // for std::logic_error #include // for std::uint8_t namespace Nuclex { namespace Support { // ------------------------------------------------------------------------------------------- // /// Stores either a value or nothing, allowing optional values on the stack /// Type of value the optional will contain /// /// This library targets C++14, where std::optional hadn't been introduced yet. /// If you are targeting C++17 or later, there is no need to use this class. /// template class NUCLEX_SUPPORT_TYPE Optional { /// An instance that is empty public: NUCLEX_SUPPORT_API const static Optional Empty; /// Initializes a new optional not holding a value public: NUCLEX_SUPPORT_API Optional() : carriesValue(false) {} /// Initializes a new Optional containing the specified value /// Value that will be carried by the optional public: NUCLEX_SUPPORT_API Optional(const TValue &value) : carriesValue(true) { new(this->valueMemory) TValue(value); } /// Initializes a new Optional containing the specified value /// Value that will be carried by the optional public: NUCLEX_SUPPORT_API Optional(TValue &&value) : carriesValue(true) { new(this->valueMemory) TValue(std::move(value)); } /// Initializes a new optional copying the contents of an existing instance /// Other instance that will be copied public: NUCLEX_SUPPORT_API Optional(const Optional &other) : carriesValue(other.carriesValue) { if(this->carriesValue) { new(this->valueMemory) TValue(*reinterpret_cast(other.valueMemory)); } } /// Initializes a new Optional taking over an existing instance /// Other instance that will be taken over public: NUCLEX_SUPPORT_API Optional(Optional &&other) : carriesValue(other.carriesValue) { if(other.carriesValue) { new(this->valueMemory) TValue( std::move(*reinterpret_cast(other.valueMemory)) ); reinterpret_cast(other.valueMemory)->~TValue(); other.carriesValue = false; } } /// Frees all memory used by the instance public: NUCLEX_SUPPORT_API ~Optional() { if(this->carriesValue) { reinterpret_cast(this->valueMemory)->~TValue(); } } /// Checks whether the Any is currently holding a value /// True if the Any holds a value, false otherwise public: NUCLEX_SUPPORT_API bool HasValue() const { return this->carriesValue; } /// Destroys the contents of the Any public: NUCLEX_SUPPORT_API void Reset() { if(this->carriesValue) { reinterpret_cast(this->valueMemory)->~TValue(); this->carriesValue = false; } } /// Assigns the contents of another Optional to this instance /// Other Optional whose contents will be assigned to this one /// The current Optional after the value has been assigned public: NUCLEX_SUPPORT_API Optional &operator =(const Optional &other) { if(this->carriesValue) { reinterpret_cast(this->valueMemory)->~TValue(); } if(other.carriesValue) { new(this->valueMemory) TValue(*reinterpret_cast(other.valueMemory)); } this->carriesValue = other.carriesValue; return *this; } /// Moves the contents of another Optional to this instance /// Other Optional whose contents will be moved to this one /// The current Optional after the value has been moved public: NUCLEX_SUPPORT_API Optional &operator =(Optional &&other) { if(this->carriesValue) { reinterpret_cast(this->valueMemory)->~TValue(); } if(other.carriesValue) { new(this->valueMemory) TValue( std::move(*reinterpret_cast(other.valueMemory)) ); reinterpret_cast(other.valueMemory)->~TValue(); this->carriesValue = true; other.carriesValue = false; } else{ this->carriesValue = false; } return *this; } /// Retrieves the value stored in the any /// Type of value that will be retrieved from the any /// The value stored by the any public: NUCLEX_SUPPORT_API const TValue &Get() const { if(!this->carriesValue) { throw std::logic_error(u8"Optional does not contain a value"); } return *reinterpret_cast(this->valueMemory); } /// Retrieves the value stored in the any /// Type of value that will be retrieved from the any /// The value stored by the any public: NUCLEX_SUPPORT_API TValue &Get() { if(!this->carriesValue) { throw std::logic_error(u8"Optional does not contain a value"); } return *reinterpret_cast(this->valueMemory); } /// Whether the optional container is currently holding a value private: bool carriesValue; /// Memory used to store the contained value, if any private: std::uint8_t valueMemory[sizeof(TValue)]; }; // ------------------------------------------------------------------------------------------- // }} // namespace Nuclex::Support #endif // NUCLEX_SUPPORT_OPTIONAL_H