Skip to content

Commit

Permalink
Merge: RB-20-register
Browse files Browse the repository at this point in the history
  • Loading branch information
KitetsuK committed Sep 20, 2023
2 parents 933dec7 + cd82d9c commit e8f4925
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 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 @@ -13,5 +13,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;
};
20 changes: 20 additions & 0 deletions src/poc/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include "Registry.hpp"

int main()
{
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 e8f4925

Please sign in to comment.