summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/fud_directory.hpp4
-rw-r--r--include/fud_option.hpp17
-rw-r--r--include/fud_string.hpp15
-rw-r--r--include/fud_utf8.hpp6
-rw-r--r--include/fud_utf8_iterator.hpp6
-rw-r--r--source/fud_assert.cpp1
-rw-r--r--source/fud_directory.cpp8
-rw-r--r--source/fud_string.cpp12
-rw-r--r--source/fud_utf8.cpp4
-rw-r--r--source/fud_utf8_iterator.cpp12
-rw-r--r--test/test_directory.cpp4
-rw-r--r--test/test_utf8.cpp8
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 <cstdint>
#include <cstdio>
#include <ctime>
#include <dirent.h>
-#include <optional>
namespace fud {
@@ -110,7 +110,7 @@ class Directory {
Result<DirectoryEntry, FudStatus> info();
- Result<std::optional<DirectoryEntry>, FudStatus> getNextEntry();
+ Result<Option<DirectoryEntry>, 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<ValueType>(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<ValueType>(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 <climits>
@@ -65,7 +66,7 @@ class String {
Array<size_t, sizeof...(cStrings)> lengths{};
Array<const char*, sizeof...(cStrings)> 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<decltype(cStringItem), const char*>) {
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<utf8> back();
+ [[nodiscard]] Option<utf8> back();
[[nodiscard]] constexpr size_t remainingLength() const
{
@@ -211,7 +216,7 @@ class String {
FudStatus pushBack(const FudUtf8& letter);
- std::optional<utf8> pop();
+ Option<utf8> 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 <cstdint>
-#include <optional>
#include <type_traits>
namespace fud {
@@ -500,12 +500,12 @@ struct FudUtf8 {
return std::strong_ordering::greater;
}
- std::optional<Ascii> getAscii() const
+ Option<Ascii> getAscii() const
{
if (m_variant.index() == static_cast<size_t>(Utf8Type::Ascii)) {
return std::get<Ascii>(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 <cstddef>
-#include <optional>
namespace fud {
@@ -47,8 +47,8 @@ class Utf8Iterator {
m_index = 0;
}
- [[nodiscard]] std::optional<FudUtf8> peek() const;
- std::optional<FudUtf8> next();
+ [[nodiscard]] Option<FudUtf8> peek() const;
+ Option<FudUtf8> 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 <cstdio>
+#include <exception>
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<DirectoryEntry, FudStatus> Directory::info()
return retValue;
}
-Result<std::optional<DirectoryEntry>, FudStatus> Directory::getNextEntry()
+Result<Option<DirectoryEntry>, FudStatus> Directory::getNextEntry()
{
- using RetType = Result<std::optional<DirectoryEntry>, FudStatus>;
+ using RetType = Result<Option<DirectoryEntry>, FudStatus>;
errno = 0;
auto* dirEntry = readdir(m_directory);
@@ -163,7 +163,7 @@ Result<std::optional<DirectoryEntry>, 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<std::optional<DirectoryEntry>, FudStatus> Directory::getNextEntry()
if (retValue.isOkay()) {
m_errorCode = 0;
- return RetType::okay(retValue.takeOkay());
+ return RetType::okay(Option<DirectoryEntry>::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<utf8> String::back()
+[[nodiscard]] Option<utf8> 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<utf8> String::pop()
+Option<utf8> 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<FudUtf8> Utf8Iterator::peek() const
+Option<FudUtf8> 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<FudUtf8> Utf8Iterator::next()
+Option<FudUtf8> 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<FudUtf8, 16> 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());