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 +++--- source/fud_assert.cpp | 1 + source/fud_directory.cpp | 8 ++++---- source/fud_string.cpp | 12 ++++++------ source/fud_utf8.cpp | 4 ++-- source/fud_utf8_iterator.cpp | 12 ++++++------ test/test_directory.cpp | 4 ++-- test/test_utf8.cpp | 8 ++++---- 12 files changed, 54 insertions(+), 43 deletions(-) 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 diff --git a/source/fud_assert.cpp b/source/fud_assert.cpp index a7c9d76..e16f65e 100644 --- a/source/fud_assert.cpp +++ b/source/fud_assert.cpp @@ -22,6 +22,7 @@ #include "fud_string_view.hpp" #include +#include namespace fud { diff --git a/source/fud_directory.cpp b/source/fud_directory.cpp index f46d530..1a1223c 100644 --- a/source/fud_directory.cpp +++ b/source/fud_directory.cpp @@ -152,9 +152,9 @@ Result Directory::info() return retValue; } -Result, FudStatus> Directory::getNextEntry() +Result, FudStatus> Directory::getNextEntry() { - using RetType = Result, FudStatus>; + using RetType = Result, FudStatus>; errno = 0; auto* dirEntry = readdir(m_directory); @@ -163,7 +163,7 @@ Result, FudStatus> Directory::getNextEntry() m_errorCode = errno; return RetType::error(FudStatus::Failure); } - return RetType::okay(std::nullopt); + return RetType::okay(NullOpt); } const char* entryNameCString = dirEntry->d_name; @@ -190,7 +190,7 @@ Result, FudStatus> Directory::getNextEntry() if (retValue.isOkay()) { m_errorCode = 0; - return RetType::okay(retValue.takeOkay()); + return RetType::okay(Option::take(retValue.takeOkay())); } return RetType::error(retValue.getError()); diff --git a/source/fud_string.cpp b/source/fud_string.cpp index 3ba6603..caa35e9 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -276,10 +276,10 @@ FudStatus String::reserve(size_t newCapacity) return resize(newCapacity); } -[[nodiscard]] std::optional String::back() +[[nodiscard]] Option String::back() { if (!valid()) { - return std::nullopt; + return NullOpt; } utf8 backChar = data()[m_length - 1]; @@ -287,17 +287,17 @@ FudStatus String::reserve(size_t newCapacity) return backChar; } - return std::nullopt; + return NullOpt; } -std::optional String::pop() +Option String::pop() { if (!valid()) { - return std::nullopt; + return NullOpt; } if (m_length < 1) { - return std::nullopt; + return NullOpt; } m_length--; diff --git a/source/fud_utf8.cpp b/source/fud_utf8.cpp index bffb5c1..bc12c15 100644 --- a/source/fud_utf8.cpp +++ b/source/fud_utf8.cpp @@ -90,11 +90,11 @@ namespace impl { bool isAsciiPredicate(FudUtf8 character, bool (*predicate)(char)) { auto maybeAscii = character.getAscii(); - if (!maybeAscii.has_value()) { + if (!maybeAscii.hasValue()) { return false; } - auto asciiChar = *maybeAscii; + auto asciiChar = maybeAscii.value(); return predicate(asciiChar.asChar()); } diff --git a/source/fud_utf8_iterator.cpp b/source/fud_utf8_iterator.cpp index a815c64..00ce146 100644 --- a/source/fud_utf8_iterator.cpp +++ b/source/fud_utf8_iterator.cpp @@ -19,33 +19,33 @@ namespace fud { -std::optional Utf8Iterator::peek() const +Option Utf8Iterator::peek() const { if (m_index >= m_view.length()) { - return std::nullopt; + return NullOpt; } auto character = FudUtf8::from(m_view, m_index); if (!character.valid()) { - return std::nullopt; + return NullOpt; } return character; } -std::optional Utf8Iterator::next() +Option Utf8Iterator::next() { if (m_index >= m_view.length()) { m_index = m_view.length(); - return std::nullopt; + return NullOpt; } auto character = FudUtf8::from(m_view, m_index); if (!character.valid()) { m_index = m_view.length(); - return std::nullopt; + return NullOpt; } m_index += character.size(); diff --git a/test/test_directory.cpp b/test/test_directory.cpp index e30dafb..58efc13 100644 --- a/test/test_directory.cpp +++ b/test/test_directory.cpp @@ -83,7 +83,7 @@ TEST(FudDirectory, Basic) auto dirEntryResult = directory.getNextEntry(); EXPECT_TRUE(dirEntryResult.isOkay()); auto dirEntryOpt = dirEntryResult.takeOkay(); - if (dirEntryOpt == std::nullopt) { + if (dirEntryOpt.isNone()) { break; } auto dirEntry{std::move(dirEntryOpt.value())}; @@ -99,7 +99,7 @@ TEST(FudDirectory, Basic) auto finalDirEntryResult = directory.getNextEntry(); EXPECT_TRUE(finalDirEntryResult.isOkay()); - EXPECT_EQ(finalDirEntryResult.getOkay(), std::nullopt); + EXPECT_TRUE(finalDirEntryResult.takeOkay().isNone()); // ASSERT_EQ(removeRecursive(testDirName), FudStatus::Success); } diff --git a/test/test_utf8.cpp b/test/test_utf8.cpp index 4b57b64..759c826 100644 --- a/test/test_utf8.cpp +++ b/test/test_utf8.cpp @@ -173,7 +173,7 @@ TEST(Utf8Test, Utf8MultiByte) Utf8Iterator utf8Iter{stringBuffer}; auto characterOpt = utf8Iter.next(); - ASSERT_TRUE(characterOpt.has_value()); + ASSERT_TRUE(characterOpt.hasValue()); // MULTI_BYTE_LITERAL "test今日素敵はですねƩ®😀z" const Array multiByteCharacters{ @@ -196,8 +196,8 @@ TEST(Utf8Test, Utf8MultiByte) }; size_t idx = 0; - while (characterOpt.has_value()) { - auto character = *characterOpt; + while (characterOpt.hasValue()) { + auto character = characterOpt.value(); if (character != FudUtf8{Utf8Variant{Ascii{'\0'}}}) { EXPECT_TRUE(character.size() >= 1); ASSERT_LT(idx, multiByteCharacters.size()); @@ -212,7 +212,7 @@ TEST(Utf8Test, Utf8MultiByte) characterOpt = utf8Iter.next(); } utf8Iter.reset(); - ASSERT_TRUE(utf8Iter.next().has_value()); + ASSERT_TRUE(utf8Iter.next().hasValue()); FudUtf8 invalid = FudUtf8::invalidAscii(); ASSERT_FALSE(invalid.valid()); -- cgit v1.2.3