Skip to content

Commit

Permalink
Add registry
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumeAbel committed Sep 20, 2023
2 parents 78e7cd8 + 2d1e858 commit cd82d9c
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 13 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ build/
.vs/
*.ini
!dllTest/*

#CMake
*.vcxproj*
CmakeCache*
CmakeFiles/
Debug/
cmake_*
*.dir/
*.sln
x64/
1 change: 0 additions & 1 deletion src/poc/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ target_sources(
${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Registry.cpp
)
9 changes: 0 additions & 9 deletions src/poc/src/Registry.cpp

This file was deleted.

37 changes: 35 additions & 2 deletions src/poc/src/Registry.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
#include <any>
#include <typeinfo>
#include <typeindex>
#include <unordered_map>
#include "SparseArray.hpp"
#include <memory>

class Registry {
public:
Registry();
~Registry();
template <class Component>
using array = std::shared_ptr<SparseArray<Component>>;

template <class Component>
array<Component> registerComponent()
{
if (_data.find(typeid(Component)) == _data.end()) {
_data[typeid(Component)] = std::make_shared<SparseArray<Component>>();
}
return castReturn<Component>();
};
template <class Component>
array<Component> getComponents()
{
return castReturn<Component>();
};
template <class Component>
array<Component> const &getComponents() const
{
return castReturn<Component>();
};
private:
template<class Component>
array<Component> castReturn()
{
return std::any_cast<array<Component>>(_data[typeid(Component)]);
}
std::unordered_map<std::type_index, std::any> _data;
};
33 changes: 33 additions & 0 deletions src/poc/src/SparseArray.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iterator>
#include <list>

template <typename Component>
class SparseArray {
public:
void add(Component component)
{
_components.push_back(component);
};
void erase(int id)
{
auto it = _components.begin();
std::advance(it, id);
_components.erase(it);
};
Component &operator[](size_t idx)
{
return _components[idx];
}

std::list<Component>::iterator begin()
{
return _components.begin();
}

std::list<Component>::iterator end()
{
return _components.end();
}
private:
std::list<Component> _components;
};
15 changes: 14 additions & 1 deletion src/poc/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
#include <iostream>
#include "Registry.hpp"

int main()
{
std::cout << "RTYPE POC" << std::endl;
Registry registry;
Registry::array<int> arrInt = registry.registerComponent<int>();
arrInt->add(4);
arrInt->add(69);
Registry::array<float> arrFloat = registry.registerComponent<float>();
arrFloat->add(69.69);
Registry::array<float> scdContainer = registry.getComponents<float>();
for (auto begin = arrInt->begin(); begin != arrInt->end(); begin++) {
std::cout << *begin << std::endl;
}
for (auto begin = scdContainer->begin(); begin != scdContainer->end(); begin++) {
std::cout << *begin << std::endl;
}
return 0;
}

0 comments on commit cd82d9c

Please sign in to comment.