27 #include "xlnt/utils/exceptions.hpp" 28 #include "xlnt/utils/numeric.hpp" 29 #include "xlnt/xlnt_config.hpp" 30 #include <type_traits> 42 #if ((defined(_MSC_VER) && _MSC_VER <= 1900) || (defined(__GNUC__) && __GNUC__ < 5)) 44 #define XLNT_NOEXCEPT_VALUE_COMPAT(...) (false) 46 #define XLNT_NOEXCEPT_VALUE_COMPAT(...) (__VA_ARGS__) 47 using ctor_copy_T_noexcept =
typename std::conditional<std::is_nothrow_copy_constructible<T>{}, std::true_type, std::false_type>::type;
48 using ctor_move_T_noexcept =
typename std::conditional<std::is_nothrow_move_constructible<T>{}, std::true_type, std::false_type>::type;
49 using copy_ctor_noexcept = ctor_copy_T_noexcept;
50 using move_ctor_noexcept = ctor_move_T_noexcept;
51 using set_copy_noexcept_t =
typename std::conditional<std::is_nothrow_copy_constructible<T>{} && std::is_nothrow_assignable<T, T>{}, std::true_type, std::false_type>::type;
52 using set_move_noexcept_t =
typename std::conditional<std::is_nothrow_move_constructible<T>{} && std::is_nothrow_move_assignable<T>{}, std::true_type, std::false_type>::type;
53 using clear_noexcept_t =
typename std::conditional<std::is_nothrow_destructible<T>{}, std::true_type, std::false_type>::type;
55 template <typename U = T, typename std::enable_if<!std::is_floating_point<U>::value>::type * =
nullptr>
59 constexpr
bool compare_equal(
const U &lhs,
const U &rhs)
const 67 template <typename U = T, typename std::enable_if<std::is_floating_point<U>::value>::type * =
nullptr>
68 constexpr
bool compare_equal(
const U &lhs,
const U &rhs)
const 70 return detail::float_equals(lhs, rhs);
86 optional(
const T &value) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(ctor_copy_T_noexcept{}))
89 new (&storage_) T(value);
96 optional(T &&value) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(ctor_move_T_noexcept{}))
99 new (&storage_) T(std::move(value));
107 : has_value_(other.has_value_)
111 new (&storage_) T(other.value_ref());
120 : has_value_(other.has_value_)
124 new (&storage_) T(std::move(other.value_ref()));
135 if (other.has_value_)
137 set(other.value_ref());
152 if (other.has_value_)
154 set(std::move(other.value_ref()));
184 void set(
const T &value) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(set_copy_noexcept_t{}))
186 #if defined(__GNUC__) && !defined(__clang__) 187 #pragma GCC diagnostic push 188 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" 196 new (&storage_) T(value);
199 #if defined(__GNUC__) && !defined(__clang__) 200 #pragma GCC diagnostic pop 207 void set(T &&value) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(set_move_noexcept_t{}))
213 #if defined(__GNUC__) && !defined(__clang__) 214 #pragma GCC diagnostic push 215 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" 219 value_ref() = std::move(value);
223 new (&storage_) T(std::move(value));
226 #if defined(__GNUC__) && !defined(__clang__) 227 #pragma GCC diagnostic pop 252 void clear() noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(clear_noexcept_t{}))
256 reinterpret_cast<T *
>(&storage_)->~T();
296 if (has_value_ != other.has_value_)
305 return compare_equal(value_ref(), other.value_ref());
320 T &value_ref() noexcept
322 return *
reinterpret_cast<T *
>(&storage_);
325 const T &value_ref()
const noexcept
327 return *
reinterpret_cast<const T *
>(&storage_);
331 typename std::aligned_storage<sizeof(T), alignof(T)>::type storage_;
334 #ifdef XLNT_NOEXCEPT_VALUE_COMPAT 335 #undef XLNT_NOEXCEPT_VALUE_COMPAT optional() noexcept
Default contructor. is_set() will be false initially.
Definition: optional.hpp:77
void clear() noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(clear_noexcept_t{}))
After this is called, is_set() will return false until a new value is provided.
Definition: optional.hpp:252
bool is_set() const noexcept
Returns true if this object currently has a value set. This should be called before accessing the val...
Definition: optional.hpp:176
optional & operator=(T &&rhs) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(set_move_noexcept_t{}))
Assignment operator overload. Equivalent to setting the value using optional::set.
Definition: optional.hpp:243
optional(optional &&other) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(move_ctor_noexcept{}))
Move constructs this optional from other. Clears the value from other if set noexcept if T move ctor ...
Definition: optional.hpp:119
bool operator==(const optional< T > &other) const noexcept
Returns true if neither this nor other have a value or both have a value and those values are equal a...
Definition: optional.hpp:294
optional(T &&value) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(ctor_move_T_noexcept{}))
Constructs this optional with a value. noexcept if T move ctor is noexcept
Definition: optional.hpp:96
Enumerates the possible types a cell can be determined by it's current value.
Definition: cell.hpp:37
Exception when setting a class's attribute to an invalid value
Definition: exceptions.hpp:240
Many settings in xlnt are allowed to not have a value set. This class encapsulates a value which may ...
Definition: format.hpp:44
optional & operator=(const T &rhs) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(set_copy_noexcept_t{}))
Assignment operator overload. Equivalent to setting the value using optional::set.
Definition: optional.hpp:234
~optional() noexcept
Destructor cleans up the T instance if set
Definition: optional.hpp:167
optional & operator=(const optional &other) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(set_copy_noexcept_t{} &&clear_noexcept_t{}))
Copy assignment of this optional from other noexcept if set and clear are noexcept for T& ...
Definition: optional.hpp:133
optional(const T &value) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(ctor_copy_T_noexcept{}))
Constructs this optional with a value. noexcept if T copy ctor is noexcept
Definition: optional.hpp:86
optional(const optional &other) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(copy_ctor_noexcept{}))
Copy constructs this optional from other noexcept if T copy ctor is noexcept
Definition: optional.hpp:106
optional & operator=(optional &&other) noexcept(XLNT_NOEXCEPT_VALUE_COMPAT(set_move_noexcept_t{} &&clear_noexcept_t{}))
Move assignment of this optional from other noexcept if set and clear are noexcept for T&& ...
Definition: optional.hpp:150
bool operator!=(const optional< T > &other) const noexcept
Returns false if neither this nor other have a value or both have a value and those values are equal ...
Definition: optional.hpp:313