-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathattributes.h
128 lines (101 loc) · 3.62 KB
/
attributes.h
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#pragma once
////////////////////////////////////////////////////////////////////////////////
#include <Eigen/Dense>
#include <typeindex>
#include <iostream>
#include <vector>
#include <memory>
#include <map>
////////////////////////////////////////////////////////////////////////////////
struct AttributeBase {
private:
std::type_index m_DerivedType;
public:
AttributeBase(std::type_index t) : m_DerivedType(t) { }
virtual ~AttributeBase() = default;
virtual void resize(size_t n) = 0;
std::type_index type() const { return m_DerivedType; }
};
// -----------------------------------------------------------------------------
template<typename T>
struct Attribute : public AttributeBase {
// Constructor
Attribute(int rows) : AttributeBase(typeid(T)), content_(rows) { content_.setZero(); }
// Resize data
void resize(size_t r) { content_.resize(r); }
// Data
Eigen::Matrix<T, Eigen::Dynamic, 1> content_;
};
////////////////////////////////////////////////////////////////////////////////
// A generic class to manage attributes
class AttributeManager {
public:
template<typename T> using Vector = Eigen::Matrix<T, Eigen::Dynamic, 1>;
private:
size_t m_Size;
std::map<std::string, std::shared_ptr<AttributeBase>> m_Attrs;
public:
AttributeManager(size_t n = 0) : m_Size(n) { }
// Number of attributes
size_t size() const { return m_Size; }
// Resize attributes
void resize(size_t n) { for (auto ptr : m_Attrs) { ptr.second->resize(n); }; m_Size= n; }
// Create a new attribute of type T
template<typename T>
Vector<T> & create(const std::string &name);
// Retrieve attribute by name
template<typename T>
Vector<T> & get(const std::string &name);
// Retrieve attribute by name
template<typename T>
const Vector<T> & get(const std::string &name) const;
// Retrieve attribute type
std::type_index type(const std::string &name) const { return m_Attrs.at(name)->type(); }
// Retrieve attribute keys
std::vector<std::string> keys() const;
// Test whether a given attribute is present
bool exists(const std::string &name) const { return m_Attrs.count(name) > 0; }
};
// -----------------------------------------------------------------------------
// Create a new attribute of type T
template<typename T>
AttributeManager::Vector<T> &
AttributeManager::create(const std::string &name) {
if (m_Attrs.count(name)) {
std::cerr << "[Attributes] Attribute [" << name << "] has already been created." << std::endl;
return get<T>(name);
} else {
auto ptr = std::make_shared<Attribute<T>>(size());
m_Attrs.emplace(name, ptr);
return ptr->content_;
}
}
// Retrieve attribute by name
template<typename T>
AttributeManager::Vector<T> &
AttributeManager::get(const std::string &name) {
assert(m_Attrs.count(name) == 1);
std::shared_ptr<AttributeBase> ptr = m_Attrs.at(name);
auto * derived = dynamic_cast<Attribute<T> *>(ptr.get());
assert(derived); //, "Incompatible type requested for attribute: " + name);
return derived->content_;
}
// Retrieve attribute by name
template<typename T>
const AttributeManager::Vector<T> &
AttributeManager::get(const std::string &name) const {
assert(m_Attrs.count(name) == 1);
std::shared_ptr<const AttributeBase> ptr = m_Attrs.at(name);
const auto * derived = dynamic_cast<const Attribute<T> *>(ptr.get());
assert(derived); //, "Incompatible type requested for attribute: " + name);
return derived->content_;
}
// -----------------------------------------------------------------------------
// Retrieve attribute keys
inline std::vector<std::string> AttributeManager::keys() const {
std::vector<std::string> res;
for (const auto & kv : m_Attrs) {
res.emplace_back(kv.first);
}
return res;
}