diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-16 22:25:08 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-16 22:25:08 -0500 |
commit | 53c4dcf374c66f1e9190f5a62a52d02fe11a69e6 (patch) | |
tree | ee40277c36fdba58fb06aca87b8ffa67ab5f8558 /include/fud_string.hpp | |
parent | f3ac764684c64fbdd2094853a80b23e570cd5d9c (diff) |
First crack at allocators.
Diffstat (limited to 'include/fud_string.hpp')
-rw-r--r-- | include/fud_string.hpp | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/include/fud_string.hpp b/include/fud_string.hpp index 6880cb7..4367aae 100644 --- a/include/fud_string.hpp +++ b/include/fud_string.hpp @@ -18,6 +18,7 @@ #ifndef FUD_STRING_HPP #define FUD_STRING_HPP +#include "fud_allocator.hpp" #include "fud_assert.hpp" #include "fud_result.hpp" #include "fud_status.hpp" @@ -40,18 +41,25 @@ class String { public: static StringResult makeFromCString(const char* cString); - static StringResult makeFromUtf8(const utf8* utf8String); + static StringResult makeFromCString(const char* cString, Allocator* allocator); - template <typename... Strings> - static StringResult makeFromCString(Strings... cStrings) + template <typename... CStrings> + static StringResult makeFromCStrings(CStrings... cStrings) { + return makeFromCStringsAlloc(&globalFudAllocator, cStrings...); + } + + template <typename... CStrings> + static StringResult makeFromCStringsAlloc(Allocator* allocator, CStrings... cStrings) + { + if (allocator == nullptr) { + return StringResult::error(FudStatus::NullPointer); + } size_t totalLength = 0; Array<size_t, sizeof...(cStrings)> lengths{}; Array<const char*, sizeof...(cStrings)> strPointers{}; size_t index = 0; for (const auto* cStringItem: {cStrings...}) { - // for (size_t index = 0; index < strPointers.size(); ++index) { - // const auto* cString = strPointers[index]; const char* cString = nullptr; if constexpr (std::is_same_v<decltype(cStringItem), const char*>) { cString = cStringItem; @@ -78,13 +86,14 @@ class String { String output{}; auto* data = output.m_buffer.data(); output.m_length = totalLength; + output.m_allocator = allocator; if (output.m_length >= output.m_capacity) { output.m_capacity = output.m_length + 1; - data = static_cast<utf8*>(fudAlloc(output.m_capacity)); - if (data == nullptr) { - return StringResult::error(FudStatus::AllocFailure); + auto dataResult = output.m_allocator->allocate(output.m_capacity); + if (dataResult.isError()) { + return StringResult::error(dataResult.getError()); } - output.m_data = data; + output.m_data = static_cast<utf8*>(dataResult.getOkay()); } size_t cumulativeLength = 0; @@ -102,14 +111,21 @@ class String { } String() noexcept = default; - String(const String& rhs); + + String(const String& rhs) = delete; + String(String&& rhs) noexcept; ~String(); - String& operator=(const String& rhs); + String& operator=(const String& rhs) = delete; + String& operator=(String&& rhs) noexcept; + static StringResult from(const String& rhs); + + FudStatus copy(const String& rhs); + [[nodiscard]] constexpr size_t length() const { return m_length; @@ -206,6 +222,7 @@ class String { private: void cleanup(); + FudStatus resize(size_t newCapacity); using BufType = Array<utf8, SSO_BUF_SIZE>; @@ -213,13 +230,17 @@ class String { BufType m_buffer{BufType::constFill(0)}; utf8* m_data; }; + size_t m_length{0}; + size_t m_capacity{SSO_BUF_SIZE}; [[nodiscard]] constexpr bool isLarge() const { return m_capacity > SSO_BUF_SIZE; } + + Allocator* m_allocator{&globalFudAllocator}; }; } // namespace fud |