Created ComponentWrapperFactory to make test mocking easier and manual component registration a little less painful

This commit is contained in:
sippeangelo
2015-12-03 19:07:33 +01:00
parent 9fb8af8491
commit aeb6c39603
3 changed files with 136 additions and 95 deletions
+40
View File
@@ -4,6 +4,7 @@
#include "../Common.h"
#include "EntityWrapper.h"
#include "ComponentInfo.h"
#include "Util/Any.h"
struct ComponentWrapper
{
@@ -62,4 +63,43 @@ struct ComponentWrapper
SubscriptProxy operator[](std::string propertyName) { return SubscriptProxy(this, propertyName); }
};
// TODO: Move this to Tests once entity importing is finished
class ComponentWrapperFactory
{
public:
ComponentWrapperFactory() = default;
ComponentWrapperFactory(std::string componentTypeName, std::size_t allocation = 0)
{
m_ComponentInfo.Name = componentTypeName;
m_ComponentInfo.Meta.Allocation = allocation;
}
template <typename T>
void AddProperty(std::string fieldName, T defaultValue)
{
m_DefaultValues.push_back(defaultValue);
m_ComponentInfo.FieldTypes[fieldName] = typeid(T).name();
m_ComponentInfo.FieldOffsets[fieldName] = m_ComponentInfo.Meta.Stride;
m_ComponentInfo.Meta.Stride += sizeof(T);
}
ComponentInfo& Finalize()
{
m_ComponentInfo.Defaults = std::shared_ptr<char>(new char[m_ComponentInfo.Meta.Stride]);
std::size_t offset = 0;
for (auto& val : m_DefaultValues) {
memcpy(m_ComponentInfo.Defaults.get() + offset, val.Data.get(), val.Size);
offset += val.Size;
}
return m_ComponentInfo;
}
operator ComponentInfo&() { return Finalize(); }
private:
ComponentInfo m_ComponentInfo;
std::vector<Any> m_DefaultValues;
};
#endif
+42
View File
@@ -0,0 +1,42 @@
#ifndef Util_Any_h__
#define Util_Any_h__
#include <memory>
struct Any
{
Any() { }
template <typename T>
Any(const T& value)
{
Data = std::shared_ptr<char>(new char[sizeof(T)]);
Size = sizeof(T);
memcpy(Data.get(), &value, Size);
}
template <typename T>
Any(T&& value)
{
Data = std::shared_ptr<char>(new char[sizeof(T)]);
Size = sizeof(T);
memcpy(Data.get(), &value, Size);
}
template <typename T>
Any& operator=(const T& value)
{
return Any(value);
}
template <typename T>
Any& operator=(T&& value)
{
return Any(value);
}
std::shared_ptr<char> Data = nullptr;
std::size_t Size = 0;
};
#endif