diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-17 17:12:27 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-17 17:12:27 +0200 |
commit | a50a3153be5214234db24a79ab4b706b37e132a0 (patch) | |
tree | 5302111f0dcb1e119141b5db706275768cc1849c /mwe/ecs-memory-efficient/inc | |
parent | e0fab8139a4dd8a33e54cb7fdea85d2bd3feaeb9 (diff) |
`make format`
Diffstat (limited to 'mwe/ecs-memory-efficient/inc')
-rw-r--r-- | mwe/ecs-memory-efficient/inc/ComponentManager.h | 26 | ||||
-rw-r--r-- | mwe/ecs-memory-efficient/inc/ComponentManager.hpp | 14 | ||||
-rw-r--r-- | mwe/ecs-memory-efficient/inc/ContiguousContainer.h | 38 | ||||
-rw-r--r-- | mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp | 124 | ||||
-rw-r--r-- | mwe/ecs-memory-efficient/inc/GameObjectMax.h | 12 | ||||
-rw-r--r-- | mwe/ecs-memory-efficient/inc/GameObjectMax.hpp | 15 |
6 files changed, 116 insertions, 113 deletions
diff --git a/mwe/ecs-memory-efficient/inc/ComponentManager.h b/mwe/ecs-memory-efficient/inc/ComponentManager.h index 066795a..8279a9a 100644 --- a/mwe/ecs-memory-efficient/inc/ComponentManager.h +++ b/mwe/ecs-memory-efficient/inc/ComponentManager.h @@ -5,28 +5,28 @@ class ComponentManager { public: - static ComponentManager& GetInstance(); //Singleton + static ComponentManager & GetInstance(); //Singleton - ComponentManager(const ComponentManager&) = delete; //Singleton - ComponentManager(ComponentManager&&) = delete; //Singleton - ComponentManager& operator=(const ComponentManager&) = delete; //Singleton - ComponentManager& operator=(ComponentManager&&) = delete; //Singleton + ComponentManager(const ComponentManager &) = delete; //Singleton + ComponentManager(ComponentManager &&) = delete; //Singleton + ComponentManager & operator=(const ComponentManager &) = delete; //Singleton + ComponentManager & operator=(ComponentManager &&) = delete; //Singleton - template<typename... Args> - void addSpriteComponent(Args&&... args); - template<typename... Args> - void addRigidbodyComponent(Args&&... args); - template<typename... Args> - void addColiderComponent(Args&&... args); + template <typename... Args> + void addSpriteComponent(Args &&... args); + template <typename... Args> + void addRigidbodyComponent(Args &&... args); + template <typename... Args> + void addColiderComponent(Args &&... args); std::vector<std::reference_wrapper<Sprite>> getAllSpriteReferences(); std::vector<std::reference_wrapper<Rigidbody>> getAllRigidbodyReferences(); std::vector<std::reference_wrapper<Colider>> getAllColiderReferences(); private: - static ComponentManager mInstance; //Singleton + static ComponentManager mInstance; //Singleton - ComponentManager(); //Singleton + ComponentManager(); //Singleton ContiguousContainer<Sprite> mSpriteContainer; ContiguousContainer<Rigidbody> mRigidbodyContainer; diff --git a/mwe/ecs-memory-efficient/inc/ComponentManager.hpp b/mwe/ecs-memory-efficient/inc/ComponentManager.hpp index 1607c0c..a914a6b 100644 --- a/mwe/ecs-memory-efficient/inc/ComponentManager.hpp +++ b/mwe/ecs-memory-efficient/inc/ComponentManager.hpp @@ -1,15 +1,15 @@ - -template<typename... Args> -void ComponentManager::addSpriteComponent(Args&&... args) { + +template <typename... Args> +void ComponentManager::addSpriteComponent(Args &&... args) { mSpriteContainer.pushBack(std::forward<Args>(args)...); } -template<typename... Args> -void ComponentManager::addRigidbodyComponent(Args&&... args) { +template <typename... Args> +void ComponentManager::addRigidbodyComponent(Args &&... args) { mRigidbodyContainer.pushBack(std::forward<Args>(args)...); } -template<typename... Args> -void ComponentManager::addColiderComponent(Args&&... args){ +template <typename... Args> +void ComponentManager::addColiderComponent(Args &&... args) { mColiderContainer.pushBack(std::forward<Args>(args)...); } diff --git a/mwe/ecs-memory-efficient/inc/ContiguousContainer.h b/mwe/ecs-memory-efficient/inc/ContiguousContainer.h index dd0321e..e3b57ba 100644 --- a/mwe/ecs-memory-efficient/inc/ContiguousContainer.h +++ b/mwe/ecs-memory-efficient/inc/ContiguousContainer.h @@ -1,34 +1,34 @@ #pragma once -#include <cstdlib> // For malloc and free -#include <new> // For placement new -#include <utility> // For std::move and std::forward +#include <cstdlib> // For malloc and free +#include <new> // For placement new #include <stdexcept> // For std::bad_alloc -#include <vector> // For returning references +#include <utility> // For std::move and std::forward +#include <vector> // For returning references -template<typename T> +template <typename T> class ContiguousContainer { public: - ContiguousContainer(); - ~ContiguousContainer(); + ContiguousContainer(); + ~ContiguousContainer(); - // Use perfect forwarding for pushBack - template<typename... Args> - void pushBack(Args&&... args); - - void popBack(); - T& operator[](size_t index); - size_t getSize() const; + // Use perfect forwarding for pushBack + template <typename... Args> + void pushBack(Args &&... args); + + void popBack(); + T & operator[](size_t index); + size_t getSize() const; // Function to return references to all stored objects - std::vector<std::reference_wrapper<T>> getAllReferences(); + std::vector<std::reference_wrapper<T>> getAllReferences(); private: - T* mData; - size_t mSize; - size_t mCapacity; + T * mData; + size_t mSize; + size_t mCapacity; - void resize(size_t new_capacity); // Resize function to allocate more space + void resize(size_t new_capacity); // Resize function to allocate more space }; #include "ContiguousContainer.hpp" diff --git a/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp b/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp index 878a85f..408d5aa 100644 --- a/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp +++ b/mwe/ecs-memory-efficient/inc/ContiguousContainer.hpp @@ -1,84 +1,84 @@ -template<typename T> -ContiguousContainer<T>::ContiguousContainer() - : mSize(0), mCapacity(10) { - // Allocate memory for 10 objects initially - mData = static_cast<T*>(malloc(mCapacity * sizeof(T))); - if (!mData) { - throw std::bad_alloc(); - } +template <typename T> +ContiguousContainer<T>::ContiguousContainer() : mSize(0), mCapacity(10) { + // Allocate memory for 10 objects initially + mData = static_cast<T *>(malloc(mCapacity * sizeof(T))); + if (!mData) { + throw std::bad_alloc(); + } } -template<typename T> +template <typename T> ContiguousContainer<T>::~ContiguousContainer() { - // Destroy all constructed objects - for (size_t i = 0; i < mSize; ++i) { - mData[i].~T(); - } - // Free the allocated memory - free(mData); + // Destroy all constructed objects + for (size_t i = 0; i < mSize; ++i) { + mData[i].~T(); + } + // Free the allocated memory + free(mData); } -template<typename T> -template<typename... Args> -void ContiguousContainer<T>::pushBack(Args&&... args) { - if (mSize == mCapacity) { - // Double the capacity if the container is full - resize(mCapacity * 2); - } - // Use placement new with perfect forwarding to construct the object in place - new (mData + mSize) T(std::forward<Args>(args)...); - ++mSize; +template <typename T> +template <typename... Args> +void ContiguousContainer<T>::pushBack(Args &&... args) { + if (mSize == mCapacity) { + // Double the capacity if the container is full + resize(mCapacity * 2); + } + // Use placement new with perfect forwarding to construct the object in place + new (mData + mSize) T(std::forward<Args>(args)...); + ++mSize; } -template<typename T> +template <typename T> void ContiguousContainer<T>::popBack() { - if (mSize > 0) { - --mSize; - // Explicitly call the destructor - mData[mSize].~T(); - } + if (mSize > 0) { + --mSize; + // Explicitly call the destructor + mData[mSize].~T(); + } } -template<typename T> -T& ContiguousContainer<T>::operator[](size_t index) { - if (index >= mSize) { - throw std::out_of_range("Index out of range"); - } - return mData[index]; +template <typename T> +T & ContiguousContainer<T>::operator[](size_t index) { + if (index >= mSize) { + throw std::out_of_range("Index out of range"); + } + return mData[index]; } -template<typename T> +template <typename T> size_t ContiguousContainer<T>::getSize() const { - return mSize; + return mSize; } // Function that returns a vector of references to all stored objects -template<typename T> -std::vector<std::reference_wrapper<T>> ContiguousContainer<T>::getAllReferences() { - std::vector<std::reference_wrapper<T>> references; - references.reserve(mSize); // Reserve space to avoid reallocation - for (size_t i = 0; i < mSize; ++i) { - references.push_back(std::ref(mData[i])); - } - return references; +template <typename T> +std::vector<std::reference_wrapper<T>> +ContiguousContainer<T>::getAllReferences() { + std::vector<std::reference_wrapper<T>> references; + references.reserve(mSize); // Reserve space to avoid reallocation + for (size_t i = 0; i < mSize; ++i) { + references.push_back(std::ref(mData[i])); + } + return references; } -template<typename T> +template <typename T> void ContiguousContainer<T>::resize(size_t new_capacity) { - // Allocate new memory block with the updated capacity - T* new_data = static_cast<T*>(malloc(new_capacity * sizeof(T))); - if (!new_data) { - throw std::bad_alloc(); - } + // Allocate new memory block with the updated capacity + T * new_data = static_cast<T *>(malloc(new_capacity * sizeof(T))); + if (!new_data) { + throw std::bad_alloc(); + } - // Move or copy existing objects to the new memory block - for (size_t i = 0; i < mSize; ++i) { - new (new_data + i) T(std::move(mData[i])); // Move the objects - mData[i].~T(); // Call the destructor for the old object - } + // Move or copy existing objects to the new memory block + for (size_t i = 0; i < mSize; ++i) { + new (new_data + i) T(std::move(mData[i])); // Move the objects + mData[i].~T(); // Call the destructor for the old object + } - // Free the old memory block - free(mData); - mData = new_data; - mCapacity = new_capacity; + // Free the old memory block + free(mData); + mData = new_data; + mCapacity = new_capacity; } diff --git a/mwe/ecs-memory-efficient/inc/GameObjectMax.h b/mwe/ecs-memory-efficient/inc/GameObjectMax.h index 62cd3e6..760e330 100644 --- a/mwe/ecs-memory-efficient/inc/GameObjectMax.h +++ b/mwe/ecs-memory-efficient/inc/GameObjectMax.h @@ -7,12 +7,12 @@ class GameObject { public: GameObject(std::uint32_t id, std::string name, std::string tag, int layer); - template<typename... Args> - void addSpriteComponent(Args&&... args); - template<typename... Args> - void addRigidbodyComponent(Args&&... args); - template<typename... Args> - void addColiderComponent(Args&&... args); + template <typename... Args> + void addSpriteComponent(Args &&... args); + template <typename... Args> + void addRigidbodyComponent(Args &&... args); + template <typename... Args> + void addColiderComponent(Args &&... args); std::uint32_t mId; std::string mName; diff --git a/mwe/ecs-memory-efficient/inc/GameObjectMax.hpp b/mwe/ecs-memory-efficient/inc/GameObjectMax.hpp index aac9811..be3ffa2 100644 --- a/mwe/ecs-memory-efficient/inc/GameObjectMax.hpp +++ b/mwe/ecs-memory-efficient/inc/GameObjectMax.hpp @@ -1,16 +1,19 @@ #include "ComponentManager.h" template <typename... Args> -void GameObject::addSpriteComponent(Args&&... args) { - ComponentManager::GetInstance().addSpriteComponent(std::forward<Args>(args)...); +void GameObject::addSpriteComponent(Args &&... args) { + ComponentManager::GetInstance().addSpriteComponent( + std::forward<Args>(args)...); } template <typename... Args> -void GameObject::addRigidbodyComponent(Args&&... args) { - ComponentManager::GetInstance().addRigidbodyComponent(std::forward<Args>(args)...); +void GameObject::addRigidbodyComponent(Args &&... args) { + ComponentManager::GetInstance().addRigidbodyComponent( + std::forward<Args>(args)...); } template <typename... Args> -void GameObject::addColiderComponent(Args&&... args) { - ComponentManager::GetInstance().addColiderComponent(std::forward<Args>(args)...); +void GameObject::addColiderComponent(Args &&... args) { + ComponentManager::GetInstance().addColiderComponent( + std::forward<Args>(args)...); } |