From c3cf6df863828798ed8230b0f0966bcf3b2d08dd Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Sun, 27 Oct 2024 21:50:16 -0500 Subject: Excise std::optional. --- include/fud_directory.hpp | 4 ++-- include/fud_option.hpp | 17 +++++++++++------ include/fud_string.hpp | 15 ++++++++++----- include/fud_utf8.hpp | 6 +++--- include/fud_utf8_iterator.hpp | 6 +++--- 5 files changed, 29 insertions(+), 19 deletions(-) (limited to 'include') diff --git a/include/fud_directory.hpp b/include/fud_directory.hpp index d2bd53d..e935950 100644 --- a/include/fud_directory.hpp +++ b/include/fud_directory.hpp @@ -21,12 +21,12 @@ #include "fud_result.hpp" #include "fud_status.hpp" #include "fud_string.hpp" +#include "fud_option.hpp" #include #include #include #include -#include namespace fud { @@ -110,7 +110,7 @@ class Directory { Result info(); - Result, FudStatus> getNextEntry(); + Result, FudStatus> getNextEntry(); FudStatus reset(); diff --git a/include/fud_option.hpp b/include/fud_option.hpp index 931ef82..3b0eb1b 100644 --- a/include/fud_option.hpp +++ b/include/fud_option.hpp @@ -92,17 +92,22 @@ class Option { { if constexpr (IsRef) { new (m_data.data()) std::reference_wrapper(std::ref(value)); - if (!m_engaged) { - std::abort(); - } } else { new (m_data.data()) ValueType(value); - if (!m_engaged) { - std::abort(); - } } } + constexpr static Option take(T&& value) noexcept + { + Option option{}; + if constexpr (IsRef) { + new (option.m_data.data()) std::reference_wrapper(std::ref(value)); + } else { + new (option.m_data.data()) ValueType(std::move(value)); + } + return option; + } + constexpr Option(const Option& rhs) noexcept : m_data(rhs.m_data), m_engaged(rhs.m_engaged) { } diff --git a/include/fud_string.hpp b/include/fud_string.hpp index df03ad9..d2d3761 100644 --- a/include/fud_string.hpp +++ b/include/fud_string.hpp @@ -20,10 +20,11 @@ #include "fud_allocator.hpp" #include "fud_assert.hpp" +#include "fud_c_string.hpp" +#include "fud_option.hpp" #include "fud_result.hpp" #include "fud_status.hpp" #include "fud_string_view.hpp" -#include "fud_c_string.hpp" #include "fud_utf8.hpp" #include @@ -65,7 +66,7 @@ class String { Array lengths{}; Array strPointers{}; size_t index = 0; - for (const auto* cStringItem: {cStrings...}) { + for (const auto* cStringItem : {cStrings...}) { const char* cString = nullptr; if constexpr (std::is_same_v) { cString = cStringItem; @@ -106,7 +107,11 @@ class String { size_t cumulativeLength = 0; for (size_t idx = 0; idx < strPointers.size(); ++idx) { const auto* cString = strPointers[idx]; - auto copyStatus = copyMem(data + cumulativeLength, output.m_capacity - cumulativeLength, cString, lengths[idx]); + auto copyStatus = copyMem( + data + cumulativeLength, + output.m_capacity - cumulativeLength, + cString, + lengths[idx]); fudAssert(copyStatus == FudStatus::Success); cumulativeLength += lengths[idx]; } @@ -189,7 +194,7 @@ class String { FudStatus reserve(size_t newCapacity); - [[nodiscard]] std::optional back(); + [[nodiscard]] Option back(); [[nodiscard]] constexpr size_t remainingLength() const { @@ -211,7 +216,7 @@ class String { FudStatus pushBack(const FudUtf8& letter); - std::optional pop(); + Option pop(); FudStatus append(const char* source); diff --git a/include/fud_utf8.hpp b/include/fud_utf8.hpp index 50e50aa..31c215a 100644 --- a/include/fud_utf8.hpp +++ b/include/fud_utf8.hpp @@ -20,10 +20,10 @@ #include "fud_array.hpp" #include "fud_c_string.hpp" +#include "fud_option.hpp" #include "fud_unique_array.hpp" #include -#include #include namespace fud { @@ -500,12 +500,12 @@ struct FudUtf8 { return std::strong_ordering::greater; } - std::optional getAscii() const + Option getAscii() const { if (m_variant.index() == static_cast(Utf8Type::Ascii)) { return std::get(m_variant); } - return std::nullopt; + return NullOpt; } }; diff --git a/include/fud_utf8_iterator.hpp b/include/fud_utf8_iterator.hpp index ec35927..25aaae6 100644 --- a/include/fud_utf8_iterator.hpp +++ b/include/fud_utf8_iterator.hpp @@ -19,10 +19,10 @@ #define FUD_UTF8_ITERATOR_HPP #include "fud_string.hpp" +#include "fud_option.hpp" #include "fud_utf8.hpp" #include -#include namespace fud { @@ -47,8 +47,8 @@ class Utf8Iterator { m_index = 0; } - [[nodiscard]] std::optional peek() const; - std::optional next(); + [[nodiscard]] Option peek() const; + Option next(); }; } // namespace fud -- cgit v1.2.3