2022-11-15 12:19:11 +01:00
|
|
|
#pragma once
|
|
|
|
|
#include <concepts>
|
|
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
|
|
namespace Seele
|
|
|
|
|
{
|
|
|
|
|
template<typename... Types>
|
|
|
|
|
struct Dependencies;
|
|
|
|
|
template<>
|
|
|
|
|
struct Dependencies<>
|
2023-11-10 19:18:09 +01:00
|
|
|
{
|
|
|
|
|
int x;
|
2023-12-22 19:46:07 +01:00
|
|
|
template<typename... Right>
|
2024-01-16 19:24:49 +01:00
|
|
|
Dependencies<Right...> operator|(const Dependencies<Right...>&)
|
2023-12-22 19:46:07 +01:00
|
|
|
{
|
|
|
|
|
return Dependencies<Right...>();
|
|
|
|
|
}
|
2023-11-10 19:18:09 +01:00
|
|
|
};
|
2022-11-15 12:19:11 +01:00
|
|
|
template<typename This, typename... Rest>
|
|
|
|
|
struct Dependencies<This, Rest...> : public Dependencies<Rest...>
|
|
|
|
|
{
|
|
|
|
|
template<typename... Right>
|
2024-01-16 19:24:49 +01:00
|
|
|
Dependencies<This, Rest..., Right...> operator|(const Dependencies<Right...>&)
|
2022-11-15 12:19:11 +01:00
|
|
|
{
|
|
|
|
|
return Dependencies<This, Rest..., Right...>();
|
|
|
|
|
}
|
2023-11-10 19:18:09 +01:00
|
|
|
int x;
|
2022-11-15 12:19:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace Component
|
|
|
|
|
{
|
2023-12-22 19:46:07 +01:00
|
|
|
template<typename Comp>
|
|
|
|
|
Comp& getComponent();
|
2022-11-15 12:19:11 +01:00
|
|
|
}
|
2023-12-22 19:46:07 +01:00
|
|
|
|
2023-11-10 19:18:09 +01:00
|
|
|
template<typename Comp>
|
|
|
|
|
concept has_dependencies = requires(Comp) { Comp::dependencies; };
|
2022-11-15 12:19:11 +01:00
|
|
|
|
|
|
|
|
#define REQUIRE_COMPONENT(x) \
|
|
|
|
|
private: \
|
2024-01-16 19:24:49 +01:00
|
|
|
x& get##x() { return getComponent<x>(); } \
|
|
|
|
|
const x& get##x() const { return getComponent<x>(); } \
|
2022-11-15 12:19:11 +01:00
|
|
|
public: \
|
2023-11-11 13:56:12 +01:00
|
|
|
constexpr static Dependencies<x> dependencies = {};
|
2022-11-15 12:19:11 +01:00
|
|
|
|
|
|
|
|
#define DECLARE_COMPONENT(x) \
|
2023-12-22 19:46:07 +01:00
|
|
|
void accessComponent(x& val); \
|
2022-11-15 12:19:11 +01:00
|
|
|
template<> \
|
2023-12-22 19:46:07 +01:00
|
|
|
x& getComponent<x>();
|
|
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
|
|
|
|
|
#define DEFINE_COMPONENT(x) \
|
2023-12-22 19:46:07 +01:00
|
|
|
thread_local x* tl_##x = nullptr; \
|
|
|
|
|
void Seele::Component::accessComponent(x& ref) { tl_##x = &ref; } \
|
|
|
|
|
template<> \
|
|
|
|
|
x& Seele::Component::getComponent<x>() { return *tl_##x; }
|
|
|
|
|
|
2022-11-15 12:19:11 +01:00
|
|
|
|
|
|
|
|
} // namespace Seele
|