Skip to content

Commit

Permalink
Fix vectorany. Add arena and linked list nodes files.
Browse files Browse the repository at this point in the history
  • Loading branch information
linuscu committed Oct 9, 2024
1 parent 5f4ed7e commit b2dfb4b
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 121 deletions.
24 changes: 24 additions & 0 deletions packages/memory/include/memory/Arena.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <string>
#include <unordered_map>
#include <map>
#include <vector>
#include <algorithm> // For lower_bound
#include <typeinfo>
#include <optional>
#include <iterator> // For std::forward_iterator_tag
#include <cstddef> // For std::ptrdiff_t

#include "logging/Log.h"
#include "meta/Reflection.h"

namespace l::container {


class Arena {
public:
Arena()

};
}
4 changes: 1 addition & 3 deletions packages/memory/include/memory/Handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
#include <functional>
#include <memory>

namespace l {
namespace memory {
namespace l::memory {

template<class T>
class Handle {
Expand Down Expand Up @@ -76,5 +75,4 @@ namespace memory {
T* mInstance;
};

}
}
18 changes: 18 additions & 0 deletions packages/memory/include/memory/LinkedListNodes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <string>
#include <unordered_map>
#include <map>
#include <vector>
#include <algorithm> // For lower_bound
#include <typeinfo>
#include <optional>
#include <iterator> // For std::forward_iterator_tag
#include <cstddef> // For std::ptrdiff_t

#include "logging/Log.h"
#include "meta/Reflection.h"

namespace l::container {

}
119 changes: 119 additions & 0 deletions packages/memory/include/memory/VectorAny.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#pragma once

#include <string>
#include <unordered_map>
#include <map>
#include <vector>
#include <algorithm> // For lower_bound
#include <typeinfo>
#include <optional>
#include <iterator> // For std::forward_iterator_tag
#include <cstddef> // For std::ptrdiff_t

#include "logging/Log.h"
#include "meta/Reflection.h"

namespace l::container {

template<size_t DataSize>
class ContainerTypeBase {
public:

ContainerTypeBase() = default;
~ContainerTypeBase() = default;

void* data() {
return (char*)this + sizeof(uint8_t) * 2;
}

template<class T>
bool isType() {
return mDataTypeCheckSum == l::meta::class_checksum8<T>();
}

template<class T>
void setType() {
mDataTypeCheckSum = l::meta::class_checksum8<T>();
}

uint8_t mTypeInfo;
uint8_t mDataTypeCheckSum;
uint8_t mData[DataSize];
};

template<size_t DataSize>
class IteratorBase {
public:
using ContainerType = ContainerTypeBase<DataSize>;

IteratorBase(ContainerType* ptr) : m_ptr(ptr) {}
~IteratorBase() = default;

ContainerType& operator*() const { return *m_ptr; }
ContainerType* operator->() { return m_ptr; }

template<class T>
T* get() {
if (m_ptr->mDataTypeCheckSum != l::meta::class_checksum8<T>()) {
return nullptr;
}
return reinterpret_cast<T*>(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<const size_t DataSize>
class VectorAny {
public:
static const size_t ElementSize = DataSize + 2;

using ContainerType = ContainerTypeBase<DataSize>;
using Iterator = IteratorBase<DataSize>;

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<class T>
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<T>();
memcpy(ptr->data(), (void*)&value, sizeof(T));
}

void erase(size_t pos) {
mContainer.erase(mContainer.begin() + pos);
}

protected:
std::vector<ContainerType> mContainer;
};
}
115 changes: 0 additions & 115 deletions packages/memory/include/memory/VectorPolymorphic.h

This file was deleted.

19 changes: 16 additions & 3 deletions packages/memory/tests/common/VectorPolymorphicTest.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "testing/Test.h"
#include "logging/Log.h"

#include "memory/VectorPolymorphic.h"
#include "memory/VectorAny.h"

using namespace l;

Expand All @@ -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() {
Expand All @@ -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<Base>();
auto it = storage[0];
auto a = it.get<Base>();
TEST_TRUE(a != nullptr, "");
TEST_TRUE(a->base == 0, "");

Expand Down

0 comments on commit b2dfb4b

Please sign in to comment.