From 53c4dcf374c66f1e9190f5a62a52d02fe11a69e6 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Wed, 16 Oct 2024 22:25:08 -0500 Subject: First crack at allocators. --- include/fud_string.hpp | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) (limited to 'include/fud_string.hpp') 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 - static StringResult makeFromCString(Strings... cStrings) + template + static StringResult makeFromCStrings(CStrings... cStrings) { + return makeFromCStringsAlloc(&globalFudAllocator, cStrings...); + } + + template + static StringResult makeFromCStringsAlloc(Allocator* allocator, CStrings... cStrings) + { + if (allocator == nullptr) { + return StringResult::error(FudStatus::NullPointer); + } size_t totalLength = 0; Array lengths{}; Array 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) { 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(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(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; @@ -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 -- cgit v1.2.3