data: Moved EdgeComponent system to Token and extended it to cover all type specific Edge and Token fields
This change moves the EdgeComponent system to Token and redefines them as TokenComponents. * Performance tests on the side showed that using typeid() in getComponent() is faster than dynamically casting every pointer. * The hasComponent() method was deliberately left out in the implementation, because it used to look up the right component and discard it again. This check for presence can also be achieved by just using getComponent(). * The method addComponent() is protected in Token, so that Node and Edge can guard which Component is set on which type by implementing specific setters for each Component. * The GraphTestSuite was extended to include tests for the Component implementation (and missing tests for locationIds in Token and copying of Node and Edge were added).
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
#ifndef TOKEN_H
|
||||
#define TOKEN_H
|
||||
|
||||
#include <typeinfo>
|
||||
#include <vector>
|
||||
|
||||
#include "data/graph/token_component/TokenComponent.h"
|
||||
#include "utility/types.h"
|
||||
|
||||
class Token
|
||||
@@ -21,19 +23,55 @@ public:
|
||||
void addLocationId(Id locationId);
|
||||
void removeLocationId(Id locationId);
|
||||
|
||||
template <typename ComponentType>
|
||||
ComponentType* getComponent() const;
|
||||
|
||||
template <typename ComponentType>
|
||||
std::shared_ptr<ComponentType> removeComponent();
|
||||
|
||||
protected:
|
||||
// Constructor for plain copies of Node and Edge
|
||||
Token(Id id);
|
||||
Token(const Token& other);
|
||||
|
||||
void addComponent(std::shared_ptr<TokenComponent> component);
|
||||
void copyComponentsFrom(const Token& other);
|
||||
|
||||
private:
|
||||
static Id s_nextId;
|
||||
|
||||
Token(const Token&);
|
||||
void operator=(const Token&);
|
||||
|
||||
const Id m_id; // own id
|
||||
|
||||
std::vector<Id> m_locationIds;
|
||||
std::vector<std::shared_ptr<TokenComponent>> m_components;
|
||||
};
|
||||
|
||||
template <typename ComponentType>
|
||||
ComponentType* Token::getComponent() const
|
||||
{
|
||||
for (std::shared_ptr<TokenComponent> component: m_components)
|
||||
{
|
||||
if (typeid(ComponentType) == typeid(*(component.get())))
|
||||
{
|
||||
return dynamic_cast<ComponentType*>(component.get());
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template <typename ComponentType>
|
||||
std::shared_ptr<ComponentType> Token::removeComponent()
|
||||
{
|
||||
for (size_t i = 0; i < m_components.size(); i++)
|
||||
{
|
||||
std::shared_ptr<TokenComponent> component = m_components[i];
|
||||
if (typeid(ComponentType) == typeid(*(component.get())))
|
||||
{
|
||||
m_components.erase(m_components.begin() + i);
|
||||
return std::dynamic_pointer_cast<ComponentType>(component);
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
#endif // TOKEN_H
|
||||
|
||||
Reference in New Issue
Block a user