Implemented ComponentPool and initial tests for it

This commit is contained in:
sippeangelo
2015-12-02 15:07:48 +01:00
parent 8aa72735e7
commit b3e68c6c34
8 changed files with 212 additions and 21 deletions
+64
View File
@@ -0,0 +1,64 @@
#ifndef Component_h__
#define Component_h__
#include "../Common.h"
#include "Entity.h"
#include "ComponentInfo.h"
struct ComponentWrapper
{
ComponentWrapper(const ComponentInfo& componentInfo, char* data)
: EntityID(*reinterpret_cast<::EntityID*>(data))
, Info(componentInfo)
, Data(data)
{ }
::EntityID& EntityID;
const ComponentInfo& Info;
char* Data;
template <typename T>
T& Property(std::string name)
{
unsigned int offset = Info.FieldOffsets.at(name);
return *reinterpret_cast<T*>(&Data[offset]);
}
template <typename T>
void SetProperty(std::string name, T& value) { Property<T>(name)=value; }
template <typename T>
void SetProperty(std::string name, const T value) { Property<T>(name)=value; }
// Specialization for string literals
template <std::size_t N>
void SetProperty(std::string name, const char(&value)[N]) { Property<std::string>(name)=std::string(value); }
struct SubscriptProxy
{
friend struct ComponentWrapper;
private:
SubscriptProxy(ComponentWrapper* component, std::string& propertyName)
: m_Component(component)
, m_PropertyName(propertyName)
{ }
ComponentWrapper* m_Component;
std::string& m_PropertyName;
public:
template <typename T>
operator T&() { return m_Component->Property<T>(m_PropertyName); }
template <typename T>
void operator=(const T val) { m_Component->SetProperty<T>(m_PropertyName, val); }
template <typename T>
void operator=(T& val) { m_Component->SetProperty<T>(m_PropertyName, val); }
// Specialization for string literals
template<std::size_t N>
void operator=(const char(&val)[N]) { m_Component->SetProperty<N>(m_PropertyName, val); }
};
SubscriptProxy operator[](std::string propertyName) { return SubscriptProxy(this, propertyName); }
};
#endif