diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2025-01-04 09:56:12 -0600 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2025-01-04 09:56:12 -0600 |
commit | 0b400af9519444deef4cc6ad2c43c30e2092ab4f (patch) | |
tree | e6b0d78a2c292a7df5724f150e123b55d7871819 /include/fud_option.hpp | |
parent | 253385f4bca5ccd2fee22fea8333dfe435c0df0a (diff) |
Fix bug related to string copying.
Diffstat (limited to 'include/fud_option.hpp')
-rw-r--r-- | include/fud_option.hpp | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/include/fud_option.hpp b/include/fud_option.hpp index 3c94eaa..9d3068c 100644 --- a/include/fud_option.hpp +++ b/include/fud_option.hpp @@ -75,10 +75,11 @@ class Option { private: static_assert(!std::is_same_v<T, option_detail::NullOptionType>); static constexpr bool IsRef = std::is_reference_v<T>; - using ValueType = typename std::remove_reference<T>::type; - static constexpr size_t Size = IsRef ? sizeof(std::reference_wrapper<ValueType>) : sizeof(ValueType); public: + using ValueType = std::remove_reference_t<T>; + static constexpr size_t Size = IsRef ? sizeof(std::reference_wrapper<ValueType>) : sizeof(ValueType); + constexpr Option() noexcept : m_engaged{false} { } @@ -100,6 +101,7 @@ class Option { constexpr static Option take(T&& value) noexcept { Option option{}; + option.m_engaged = true; if constexpr (IsRef) { new (option.m_data.data()) std::reference_wrapper<ValueType>(std::ref(value)); } else { @@ -152,7 +154,8 @@ class Option { return !m_engaged; } - operator bool() const { + operator bool() const + { return hasValue(); } @@ -160,8 +163,10 @@ class Option { { fudAssert(m_engaged); if constexpr (IsRef) { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return *reinterpret_cast<const std::reference_wrapper<ValueType>*>(m_data.data()); } else { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return *reinterpret_cast<const ValueType*>(m_data.data()); } } @@ -170,8 +175,10 @@ class Option { { fudAssert(m_engaged); if constexpr (IsRef) { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return *reinterpret_cast<std::reference_wrapper<ValueType>*>(m_data.data()); } else { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return *reinterpret_cast<ValueType*>(m_data.data()); } } @@ -180,6 +187,7 @@ class Option { { fudAssert(m_engaged); static_assert(!IsRef); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return std::move(*reinterpret_cast<const ValueType*>(m_data.data())); } @@ -200,7 +208,7 @@ class Option { } template <typename F> - constexpr auto map(F&& func) const & -> Option<decltype(std::forward<F>(func)(value()))> + constexpr auto map(F&& func) const& -> Option<decltype(std::forward<F>(func)(value()))> { using U = decltype(std::forward<F>(func)(value())); // static_assert(std::is_same_v<decltype(std::forward<F>(func)(value())), Option<U>>()); @@ -217,6 +225,7 @@ class Option { if constexpr (IsRef) { // reinterpret_cast<std::reference_wrapper<ValueType>*>(m_data.data()); } else { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) reinterpret_cast<ValueType*>(m_data.data())->~ValueType(); } cleanup(); |