From b2dfb4bb4ebc0a6f17ba2d1ee7745f1aa672c39b Mon Sep 17 00:00:00 2001 From: lnd3 Date: Wed, 9 Oct 2024 16:10:00 +0200 Subject: [PATCH] Fix vectorany. Add arena and linked list nodes files. --- packages/memory/include/memory/Arena.h | 24 ++++ packages/memory/include/memory/Handle.h | 4 +- .../memory/include/memory/LinkedListNodes.h | 18 +++ packages/memory/include/memory/VectorAny.h | 119 ++++++++++++++++++ .../memory/include/memory/VectorPolymorphic.h | 115 ----------------- .../tests/common/VectorPolymorphicTest.cpp | 19 ++- 6 files changed, 178 insertions(+), 121 deletions(-) create mode 100644 packages/memory/include/memory/Arena.h create mode 100644 packages/memory/include/memory/LinkedListNodes.h create mode 100644 packages/memory/include/memory/VectorAny.h delete mode 100644 packages/memory/include/memory/VectorPolymorphic.h diff --git a/packages/memory/include/memory/Arena.h b/packages/memory/include/memory/Arena.h new file mode 100644 index 00000000..933e6aac --- /dev/null +++ b/packages/memory/include/memory/Arena.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include +#include +#include // For lower_bound +#include +#include +#include // For std::forward_iterator_tag +#include // For std::ptrdiff_t + +#include "logging/Log.h" +#include "meta/Reflection.h" + +namespace l::container { + + + class Arena { + public: + Arena() + + }; +} diff --git a/packages/memory/include/memory/Handle.h b/packages/memory/include/memory/Handle.h index 8297bcd2..0ecda05a 100644 --- a/packages/memory/include/memory/Handle.h +++ b/packages/memory/include/memory/Handle.h @@ -5,8 +5,7 @@ #include #include -namespace l { -namespace memory { +namespace l::memory { template class Handle { @@ -76,5 +75,4 @@ namespace memory { T* mInstance; }; -} } \ No newline at end of file diff --git a/packages/memory/include/memory/LinkedListNodes.h b/packages/memory/include/memory/LinkedListNodes.h new file mode 100644 index 00000000..755c8aec --- /dev/null +++ b/packages/memory/include/memory/LinkedListNodes.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include +#include +#include +#include // For lower_bound +#include +#include +#include // For std::forward_iterator_tag +#include // For std::ptrdiff_t + +#include "logging/Log.h" +#include "meta/Reflection.h" + +namespace l::container { + +} diff --git a/packages/memory/include/memory/VectorAny.h b/packages/memory/include/memory/VectorAny.h new file mode 100644 index 00000000..c35fe2da --- /dev/null +++ b/packages/memory/include/memory/VectorAny.h @@ -0,0 +1,119 @@ +#pragma once + +#include +#include +#include +#include +#include // For lower_bound +#include +#include +#include // For std::forward_iterator_tag +#include // For std::ptrdiff_t + +#include "logging/Log.h" +#include "meta/Reflection.h" + +namespace l::container { + + template + class ContainerTypeBase { + public: + + ContainerTypeBase() = default; + ~ContainerTypeBase() = default; + + void* data() { + return (char*)this + sizeof(uint8_t) * 2; + } + + template + bool isType() { + return mDataTypeCheckSum == l::meta::class_checksum8(); + } + + template + void setType() { + mDataTypeCheckSum = l::meta::class_checksum8(); + } + + uint8_t mTypeInfo; + uint8_t mDataTypeCheckSum; + uint8_t mData[DataSize]; + }; + + template + class IteratorBase { + public: + using ContainerType = ContainerTypeBase; + + IteratorBase(ContainerType* ptr) : m_ptr(ptr) {} + ~IteratorBase() = default; + + ContainerType& operator*() const { return *m_ptr; } + ContainerType* operator->() { return m_ptr; } + + template + T* get() { + if (m_ptr->mDataTypeCheckSum != l::meta::class_checksum8()) { + return nullptr; + } + return reinterpret_cast(m_ptr->data()); + } + + IteratorBase& operator+=(int index) { m_ptr += index; return *this; } + + // Prefix increment + IteratorBase& operator++() { m_ptr++; return *this; } + + // Postfix increment + IteratorBase operator++(int) { + IteratorBase tmp = *this; ++(*this); return tmp; + } + + friend bool operator== (const IteratorBase& a, const IteratorBase& b) { return a.m_ptr == b.m_ptr; }; + friend bool operator!= (const IteratorBase& a, const IteratorBase& b) { return a.m_ptr != b.m_ptr; }; + + private: + ContainerType* m_ptr; + }; + + template + class VectorAny { + public: + static const size_t ElementSize = DataSize + 2; + + using ContainerType = ContainerTypeBase; + using Iterator = IteratorBase; + + VectorAny(size_t reserve) { + mContainer.reserve(reserve); + } + virtual ~VectorAny() = default; + + Iterator begin() { return Iterator(mContainer.data()); + } + Iterator end() { + return Iterator(mContainer.data() + mContainer.size()); + } + + Iterator operator[](size_t index) { + return Iterator(mContainer.data() + index); + } + + template + void push_back(T&& value) { + static_assert(sizeof(T) <= sizeof(ContainerType)); + ContainerType* ptr = mContainer.data() + mContainer.size(); + mContainer.push_back({}); + ptr->mDataTypeCheckSum = l::meta::class_checksum8(); + memcpy(ptr->data(), (void*)&value, sizeof(T)); + } + + void erase(size_t pos) { + mContainer.erase(mContainer.begin() + pos); + } + + protected: + std::vector mContainer; + }; +} diff --git a/packages/memory/include/memory/VectorPolymorphic.h b/packages/memory/include/memory/VectorPolymorphic.h deleted file mode 100644 index dbfc3188..00000000 --- a/packages/memory/include/memory/VectorPolymorphic.h +++ /dev/null @@ -1,115 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include // For lower_bound -#include -#include -#include // For std::forward_iterator_tag -#include // For std::ptrdiff_t - -#include "logging/Log.h" -#include "meta/Reflection.h" - -namespace l::container { - - template - class VectorPolymorphic { - public: - static const size_t DataSize = MaxElementSize - 2; - - VectorPolymorphic(size_t reserve) { - mContainer.reserve(reserve); - } - virtual ~VectorPolymorphic() = default; - - class ContainerType { - public: - inline void* data() { - return (char*)this + 2; - } - - template - bool isType() { - auto type = l::meta::class_checksum(); - return mDataTypeCheckSum == (type % 255); - } - - template - void setType() { - auto type = l::meta::class_checksum(); - mDataTypeCheckSum = type % 255; - } - - uint8_t mTypeInfo; - uint8_t mDataTypeCheckSum; - uint8_t mData[DataSize]; - }; - - struct Iterator - { - public: - using iterator_category = std::forward_iterator_tag; - using difference_type = std::ptrdiff_t; - using value_type = ContainerType; - using pointer = value_type*; // or also value_type* - using reference = value_type&; // or also value_type& - - Iterator(pointer ptr) : m_ptr(ptr) {} - - reference operator*() const { return *m_ptr; } - pointer operator->() { return m_ptr; } - - template - T* get() { - if (!m_ptr->isType()) { - return nullptr; - } - return reinterpret_cast(m_ptr->data()); - } - - Iterator& operator+=(int index) { m_ptr+=index; return *this; } - - // Prefix increment - Iterator& operator++() { m_ptr++; return *this; } - - // Postfix increment - Iterator operator++(int) { - Iterator tmp = *this; ++(*this); return tmp; } - - friend bool operator== (const Iterator& a, const Iterator& b) { return a.m_ptr == b.m_ptr; }; - friend bool operator!= (const Iterator& a, const Iterator& b) { return a.m_ptr != b.m_ptr; }; - - private: - pointer m_ptr; - }; - - Iterator begin() { return Iterator(mContainer.data()); - } - Iterator end() { - return Iterator(mContainer.data() + mContainer.size()); - } - - Iterator operator[](size_t index) { - return Iterator(mContainer.data() + index); - } - - template - void push_back(T&& value) { - static_assert(sizeof(T) <= sizeof(ContainerType)); - ContainerType* ptr = mContainer.data() + mContainer.size(); - mContainer.push_back({}); - ptr->setType(); - memcpy(ptr->data(), (void*)&value, sizeof(T)); - } - - void erase(size_t pos) { - mContainer.erase(mContainer.begin() + pos); - } - - protected: - std::vector mContainer; - }; -} diff --git a/packages/memory/tests/common/VectorPolymorphicTest.cpp b/packages/memory/tests/common/VectorPolymorphicTest.cpp index 50485840..bea9eaae 100644 --- a/packages/memory/tests/common/VectorPolymorphicTest.cpp +++ b/packages/memory/tests/common/VectorPolymorphicTest.cpp @@ -1,7 +1,7 @@ #include "testing/Test.h" #include "logging/Log.h" -#include "memory/VectorPolymorphic.h" +#include "memory/VectorAny.h" using namespace l; @@ -28,6 +28,18 @@ TEST(Container, Polymorphic) { int derived; }; + class Derived2 : public Base { + public: + Derived2() { + base = 3; + derived2 = 3; + } + virtual ~Derived2() { + std::cout << "~Derived2()" << std::endl; + } + int derived2; + }; + class DoubleDerived : public Derived { public: DoubleDerived() { @@ -45,12 +57,13 @@ TEST(Container, Polymorphic) { static_assert(sizeof(Base) <= 16); static_assert(sizeof(Derived) <= 24); static_assert(sizeof(DoubleDerived) <= 32); - auto storage = container::VectorPolymorphic<32>(256); + auto storage = container::VectorAny<48>(256); storage.push_back(Base()); storage.push_back(Derived()); storage.push_back(DoubleDerived()); - auto a = storage[0].get(); + auto it = storage[0]; + auto a = it.get(); TEST_TRUE(a != nullptr, ""); TEST_TRUE(a->base == 0, "");