(Component) Added clone virtual method

Replaced Component<C>::GetId by GetComponentId<C>()


Former-commit-id: 4fae3f73c7fad0a5c36dae16e49b6f12c8bf6ae2
This commit is contained in:
Lynix 2015-03-01 11:21:58 +01:00
parent 8b77825fb8
commit 9c0c991fe0
3 changed files with 18 additions and 3 deletions

View File

@ -17,6 +17,8 @@ namespace Ndk
BaseComponent(nzUInt32 componentId); BaseComponent(nzUInt32 componentId);
virtual ~BaseComponent(); virtual ~BaseComponent();
virtual BaseComponent* Clone() const = 0;
nzUInt32 GetComponentId() const; nzUInt32 GetComponentId() const;
static nzUInt32 GetNextId(); static nzUInt32 GetNextId();

View File

@ -19,8 +19,11 @@ namespace Ndk
Component(); Component();
virtual ~Component(); virtual ~Component();
static nzUInt32 GetId(); virtual BaseComponent* Clone() const override;
}; };
template<typename ComponentType>
constexpr nzUInt32 GetComponentId();
} }
#include <NDK/Component.inl> #include <NDK/Component.inl>

View File

@ -2,11 +2,13 @@
// This file is part of the "Nazara Development Kit" // This file is part of the "Nazara Development Kit"
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp // For conditions of distribution and use, see copyright notice in Prerequesites.hpp
#include <type_traits>
namespace Ndk namespace Ndk
{ {
template<typename ComponentType> template<typename ComponentType>
Component<ComponentType>::Component() : Component<ComponentType>::Component() :
BaseComponent(GetId()) BaseComponent(GetComponentId<ComponentType>())
{ {
} }
@ -14,7 +16,15 @@ namespace Ndk
Component<ComponentType>::~Component() = default; Component<ComponentType>::~Component() = default;
template<typename ComponentType> template<typename ComponentType>
nzUInt32 Component<ComponentType>::GetId() virtual BaseComponent* Component<ComponentType>::Clone() const
{
static_assert<std::is_trivially_copy_constructible<ComponentType>::value, "ComponentType should be copy-constructible">
return new ComponentType(static_cast<ComponentType&>(*this));
}
template<typename ComponentType>
constexpr nzUInt32 GetComponentId()
{ {
return ComponentType::ComponentId; return ComponentType::ComponentId;
} }