// Copyright (c) 2016-2021 Thomas Fussell // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE // // @license: http://www.opensource.org/licenses/mit-license.php // @author: see AUTHORS file #pragma once #include "xlnt/utils/exceptions.hpp" #include "xlnt/utils/numeric.hpp" #include "xlnt/xlnt_config.hpp" #include namespace xlnt { /// /// Many settings in xlnt are allowed to not have a value set. This class /// encapsulates a value which may or may not be set. Memory is allocated /// within the optional class. /// template class optional { #if ((defined(_MSC_VER) && _MSC_VER <= 1900) || (defined(__GNUC__) && __GNUC__ < 5)) // Disable enhanced type checking on Visual Studio <= 2015 and GCC <5 #define XLNT_NOEXCEPT_VALUE_COMPAT(...) (false) #else #define XLNT_NOEXCEPT_VALUE_COMPAT(...) (__VA_ARGS__) using ctor_copy_T_noexcept = typename std::conditional{}, std::true_type, std::false_type>::type; using ctor_move_T_noexcept = typename std::conditional{}, std::true_type, std::false_type>::type; using copy_ctor_noexcept = ctor_copy_T_noexcept; using move_ctor_noexcept = ctor_move_T_noexcept; using set_copy_noexcept_t = typename std::conditional{} && std::is_nothrow_assignable{}, std::true_type, std::false_type>::type; using set_move_noexcept_t = typename std::conditional{} && std::is_nothrow_move_assignable{}, std::true_type, std::false_type>::type; using clear_noexcept_t = typename std::conditional{}, std::true_type, std::false_type>::type; #endif /// /// Default equality operation, just uses operator== /// template ::value>::type * = nullptr> constexpr bool compare_equal(const U &lhs, const U &rhs) const { return lhs == rhs; } /// /// equality operation for floating point numbers. Provides "fuzzy" equality /// template ::value>::type * = nullptr> constexpr bool compare_equal(const U &lhs, const U &rhs) const { return detail::float_equals(lhs, rhs); } public: /// /// Default contructor. is_set() will be false initially. /// optional() noexcept : has_value_(false) { } /// /// Constructs this optional with a value. /// noexcept if T copy ctor is noexcept /// optional(const T &value) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(ctor_copy_T_noexcept{})) : has_value_(true) { new (&storage_) T(value); } /// /// Constructs this optional with a value. /// noexcept if T move ctor is noexcept /// optional(T &&value) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(ctor_move_T_noexcept{})) : has_value_(true) { new (&storage_) T(std::move(value)); } /// /// Copy constructs this optional from other /// noexcept if T copy ctor is noexcept /// optional(const optional &other) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(copy_ctor_noexcept{})) : has_value_(other.has_value_) { if (has_value_) { new (&storage_) T(other.value_ref()); } } /// /// Move constructs this optional from other. Clears the value from other if set /// noexcept if T move ctor is noexcept /// optional(optional &&other) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(move_ctor_noexcept{})) : has_value_(other.has_value_) { if (has_value_) { new (&storage_) T(std::move(other.value_ref())); other.clear(); } } /// /// Copy assignment of this optional from other /// noexcept if set and clear are noexcept for T& /// optional &operator=(const optional &other) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(set_copy_noexcept_t{} && clear_noexcept_t{})) { if (other.has_value_) { set(other.value_ref()); } else { clear(); } return *this; } /// /// Move assignment of this optional from other /// noexcept if set and clear are noexcept for T&& /// optional &operator=(optional &&other) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(set_move_noexcept_t{} && clear_noexcept_t{})) { if (other.has_value_) { set(std::move(other.value_ref())); other.clear(); } else { clear(); } return *this; } /// /// Destructor cleans up the T instance if set /// ~optional() noexcept // note:: unconditional because msvc freaks out otherwise { clear(); } /// /// Returns true if this object currently has a value set. This should /// be called before accessing the value with optional::get(). /// bool is_set() const noexcept { return has_value_; } /// /// Copies the value into the stored value /// void set(const T &value) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(set_copy_noexcept_t{})) { #if defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" #endif if (has_value_) { value_ref() = value; } else { new (&storage_) T(value); has_value_ = true; } #if defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic pop #endif } /// /// Moves the value into the stored value /// void set(T &&value) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(set_move_noexcept_t{})) { // note seperate overload for two reasons (as opposed to perfect forwarding) // 1. have to deal with implicit conversions internally with perfect forwarding // 2. have to deal with the noexcept specfiers for all the different variations // overload is just far and away the simpler solution #if defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" #endif if (has_value_) { value_ref() = std::move(value); } else { new (&storage_) T(std::move(value)); has_value_ = true; } #if defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic pop #endif } /// /// Assignment operator overload. Equivalent to setting the value using optional::set. /// optional &operator=(const T &rhs) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(set_copy_noexcept_t{})) { set(rhs); return *this; } /// /// Assignment operator overload. Equivalent to setting the value using optional::set. /// optional &operator=(T &&rhs) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(set_move_noexcept_t{})) { set(std::move(rhs)); return *this; } /// /// After this is called, is_set() will return false until a new value is provided. /// void clear() noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(clear_noexcept_t{})) { if (has_value_) { reinterpret_cast(&storage_)->~T(); } has_value_ = false; } /// /// Gets the value. If no value has been initialized in this object, /// an xlnt::invalid_attribute exception will be thrown. /// T &get() { if (!has_value_) { throw invalid_attribute(); } return value_ref(); } /// /// Gets the value. If no value has been initialized in this object, /// an xlnt::invalid_attribute exception will be thrown. /// const T &get() const { if (!has_value_) { throw invalid_attribute(); } return value_ref(); } /// /// Returns true if neither this nor other have a value /// or both have a value and those values are equal according to /// their equality operator. /// bool operator==(const optional &other) const noexcept { if (has_value_ != other.has_value_) { return false; } if (!has_value_) { return true; } // equality is overloaded to provide fuzzy equality when T is a fp number return compare_equal(value_ref(), other.value_ref()); } /// /// Returns false if neither this nor other have a value /// or both have a value and those values are equal according to /// their equality operator. /// bool operator!=(const optional &other) const noexcept { return !operator==(other); } private: // helpers for getting a T out of storage T &value_ref() noexcept { return *reinterpret_cast(&storage_); } const T &value_ref() const noexcept { return *reinterpret_cast(&storage_); } bool has_value_; typename std::aligned_storage::type storage_; }; #ifdef XLNT_NOEXCEPT_VALUE_COMPAT #undef XLNT_NOEXCEPT_VALUE_COMPAT #endif } // namespace xlnt