(NDK) Modified component/system registration interface

Let's hope it's the last time I do this


Former-commit-id: 75f8e63e90e067a7d21efa6a1226ae4bbe050ab9
This commit is contained in:
Lynix 2015-04-05 15:57:24 +02:00
parent 28b0e26a88
commit 6a99b38be1
8 changed files with 52 additions and 33 deletions

View File

@ -26,14 +26,11 @@ namespace Ndk
ComponentIndex GetIndex() const;
template<typename ComponentType, unsigned int N>
static ComponentIndex Register(const char (&name)[N]);
static ComponentIndex Register(ComponentId id, Factory factoryFunc);
protected:
ComponentIndex m_componentIndex;
static ComponentIndex RegisterComponent(ComponentId id, Factory factoryFunc);
private:
struct ComponentEntry
{

View File

@ -3,8 +3,6 @@
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
#include <Nazara/Core/Error.hpp>
#include <Ndk/Algorithm.hpp>
#include <type_traits>
namespace Ndk
{
@ -18,25 +16,7 @@ namespace Ndk
return m_componentIndex;
}
template<typename ComponentType, unsigned int N>
ComponentIndex BaseComponent::Register(const char (&name)[N])
{
// Il faut que notre composant possède un constructeur par défaut (pour la factory)
static_assert(std::is_default_constructible<ComponentType>::value, "ComponentType should be default-constructible");
// On récupère la chaîne de caractère sous la forme d'un nombre qui servira d'identifiant unique
ComponentId id = BuildComponentId(name);
// On utilise les lambda pour créer une fonction factory
auto factory = []() -> BaseComponent*
{
return new ComponentType;
};
return Register(id, factory);
}
inline ComponentIndex BaseComponent::Register(ComponentId id, Factory factoryFunc)
inline ComponentIndex BaseComponent::RegisterComponent(ComponentId id, Factory factoryFunc)
{
// Nous allons rajouter notre composant à la fin
ComponentIndex index = s_entries.size();

View File

@ -34,13 +34,13 @@ namespace Ndk
bool HasEntity(const Entity* entity) const;
static SystemIndex GetNextIndex();
protected:
template<typename ComponentType> void Excludes();
template<typename ComponentType1, typename ComponentType2, typename... Rest> void Excludes();
void ExcludesComponent(ComponentIndex index);
static SystemIndex GetNextIndex();
template<typename ComponentType> void Requires();
template<typename ComponentType1, typename ComponentType2, typename... Rest> void Requires();
void RequiresComponent(ComponentIndex index);

View File

@ -35,11 +35,6 @@ namespace Ndk
return m_entityBits.UnboundedTest(entity->GetId());
}
inline SystemIndex BaseSystem::GetNextIndex()
{
return s_nextIndex++;
}
template<typename ComponentType>
void BaseSystem::Excludes()
{
@ -60,6 +55,11 @@ namespace Ndk
m_excludedComponents.UnboundedSet(index);
}
inline SystemIndex BaseSystem::GetNextIndex()
{
return s_nextIndex++;
}
template<typename ComponentType>
void BaseSystem::Requires()
{

View File

@ -19,6 +19,11 @@ namespace Ndk
virtual ~Component();
BaseComponent* Clone() const override;
static ComponentIndex RegisterComponent(ComponentId id);
template<unsigned int N>
static ComponentIndex RegisterComponent(const char (&name)[N]);
};
}

View File

@ -2,6 +2,7 @@
// This file is part of the "Nazara Development Kit"
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
#include <Ndk/Algorithm.hpp>
#include <type_traits>
namespace Ndk
@ -23,4 +24,31 @@ namespace Ndk
return new ComponentType(static_cast<const ComponentType&>(*this));
}
template<typename ComponentType>
ComponentIndex Component<ComponentType>::RegisterComponent(ComponentId id)
{
// Il faut que notre composant possède un constructeur par défaut (pour la factory)
static_assert(std::is_default_constructible<ComponentType>::value, "ComponentType should be default-constructible");
// On utilise les lambda pour créer une fonction factory
auto factory = []() -> BaseComponent*
{
return new ComponentType;
};
// Je ne sais pas si c'est un bug de GCC ou si c'est quelque chose que j'ai mal compris
// mais le fait est que ça ne compile pas si je ne précise pas Basecomponent::
return BaseComponent::RegisterComponent(id, factory);
}
template<typename ComponentType>
template<unsigned int N>
ComponentIndex Component<ComponentType>::RegisterComponent(const char (&name)[N])
{
// On récupère la chaîne de caractère sous la forme d'un nombre qui servira d'identifiant unique
ComponentId id = BuildComponentId(name);
return RegisterComponent(id);
}
}

View File

@ -19,6 +19,8 @@ namespace Ndk
virtual ~System();
BaseSystem* Clone() const override;
static SystemIndex RegisterSystem();
};
}

View File

@ -24,4 +24,11 @@ namespace Ndk
return new SystemType(static_cast<const SystemType&>(*this));
}
template<typename SystemType>
SystemIndex System<SystemType>::RegisterSystem()
{
return GetNextIndex();
}
}