-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathentitystorage.inl
More file actions
62 lines (53 loc) · 3.94 KB
/
entitystorage.inl
File metadata and controls
62 lines (53 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
template<class DataType>
void EntityStorage::StorageEntity::setData(const std::string& attribute, DataType inData)
{
FEA_ASSERT(attributeData.find(attribute) != attributeData.end(), "Trying to set the attribute '" + attribute + "' on an entity which does not have said attribute!");
auto& valuePointer = attributeData.at(attribute);
if(!valuePointer)
valuePointer = std::make_shared<DataType>(std::move(inData));
else
*std::static_pointer_cast<DataType>(valuePointer) = std::move(inData);
}
template<class DataType>
const DataType& EntityStorage::StorageEntity::getData(const std::string& attribute) const
{
FEA_ASSERT(attributeData.find(attribute) != attributeData.end(), "Trying to get the attribute '" + attribute + "' on an entity which does not have said attribute!");
auto& valuePointer = attributeData.at(attribute);
FEA_ASSERT(valuePointer, "Calling getData on attribute '" + attribute + "' which is not initialized!");
return *std::static_pointer_cast<DataType>(valuePointer);
}
template<class DataType>
DataType& EntityStorage::StorageEntity::getData(const std::string& attribute)
{
FEA_ASSERT(attributeData.find(attribute) != attributeData.end(), "Trying to get the attribute '" + attribute + "' on an entity which does not have said attribute!");
auto& valuePointer = attributeData.at(attribute);
FEA_ASSERT(valuePointer, "Calling getData on attribute '" + attribute + "' which is not initialized!");
return *std::static_pointer_cast<DataType>(valuePointer);
}
template<class DataType>
void EntityStorage::registerAttribute(const std::string& attribute)
{
FEA_ASSERT(mAttributes.find(attribute) == mAttributes.end(), "Trying to register attribute '" + attribute + "' as a '" + std::type_index(typeid(DataType)).name() + std::string(" but there is already an attribute registered with that identifier!"));
mAttributes.emplace(attribute, typeid(DataType));
}
template<class DataType>
void EntityStorage::setData(const uint32_t id, const std::string& attribute, DataType inData)
{
FEA_ASSERT(mAttributes.find(attribute) != mAttributes.end(), "Trying to set the attribute '" + attribute + "' on an entity but such an attribute has not been registered!");
FEA_ASSERT(std::type_index(typeid(DataType)) == mAttributes.at(attribute), "Trying to set attibute '" + attribute + "' as a '" + std::type_index(typeid(DataType)).name() + std::string(" but it is of type '") + std::string(mAttributes.at(attribute).name()) + "'");
mEntities.at(id).setData(attribute, std::move(inData));
}
template<class DataType>
const DataType& EntityStorage::getData(const uint32_t id, const std::string& attribute) const
{
FEA_ASSERT(mAttributes.find(attribute) != mAttributes.end(), "Trying to get the attribute '" + attribute + "' on an entity but such an attribute has not been registered!");
FEA_ASSERT(std::type_index(typeid(DataType)) == mAttributes.at(attribute), "Trying to get attibute '" + attribute + "' as a '" + std::type_index(typeid(DataType)).name() + std::string(" but it is of type '") + std::string(mAttributes.at(attribute).name()) + "'");
return mEntities.at(id).getData<DataType>(attribute);
}
template<class DataType>
DataType& EntityStorage::getData(const uint32_t id, const std::string& attribute)
{
FEA_ASSERT(mAttributes.find(attribute) != mAttributes.end(), "Trying to get the attribute '" + attribute + "' on an entity but such an attribute has not been registered!");
FEA_ASSERT(std::type_index(typeid(DataType)) == mAttributes.at(attribute), "Trying to get attibute '" + attribute + "' as a '" + std::type_index(typeid(DataType)).name() + std::string(" but it is of type '") + std::string(mAttributes.at(attribute).name()) + "'");
return mEntities.at(id).getData<DataType>(attribute);
}