// Copyright (c) 2016-2018 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, WRISING 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 #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 XLNT_API optional { public: /// /// Default contructor. is_set() will be false initially. /// optional() : has_value_(false), value_(T()) { } /// /// Constructs this optional with a value. /// explicit optional(const T &value) : has_value_(true), value_(value) { } /// /// 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 { return has_value_; } /// /// Sets the value to value. /// void set(const T &value) { has_value_ = true; value_ = value; } /// /// 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_; } /// /// 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_; } /// /// Resets the internal value using its default constructor. After this is /// called, is_set() will return false until a new value is provided. /// void clear() { has_value_ = false; value_ = T(); } /// /// Assignment operator. Equivalent to setting the value using optional::set. /// optional &operator=(const T &rhs) { has_value_ = true; value_ = rhs; return *this; } /// /// 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 { return has_value_ == other.has_value_ && (!has_value_ || (has_value_ && value_ == other.value_)); } private: bool has_value_; T value_; }; } // namespace xlnt