29 std::vector<std::string> get_names()
const
31 std::vector<std::string> names;
32 for (
const auto& itr : m_data) {
33 names.push_back(itr.first);
38 size_t get_size()
const {
return m_data.size(); }
40 bool has(
const std::string& name)
const {
return m_data.find(name) != m_data.end(); }
42 void add(
const std::string& name) { m_data.emplace(name, std::make_unique<Attribute>()); }
44 template <
typename Derived>
45 void add(
const std::string& name, Derived&& values)
47 auto attr = std::make_unique<Attribute>(std::forward<Derived>(values));
48 m_data.emplace(name, std::move(attr));
51 template <
typename Derived>
52 void set(
const std::string& name, Derived&& values)
54 auto itr = m_data.find(name);
56 if (itr->second ==
nullptr) {
57 auto attr = std::make_unique<Attribute>();
58 attr->set(std::forward<Derived>(values));
59 itr->second = std::move(attr);
61 itr->second->set(std::forward<Derived>(values));
67 auto itr = m_data.find(name);
69 return itr->second.get();
72 const Attribute* get(
const std::string& name)
const
74 auto itr = m_data.find(name);
76 return itr->second.get();
79 template <
typename Derived>
80 decltype(
auto) get(
const std::string& name)
84 return ptr->get<Derived>();
87 template <
typename Derived>
88 decltype(
auto) get(
const std::string& name)
const
90 const auto ptr = get(name);
92 return ptr->get<Derived>();
95 template <
typename Derived>
96 decltype(
auto) view(
const std::string& name)
100 return ptr->view<Derived>();
103 template <
typename Derived>
104 decltype(
auto) view(
const std::string& name)
const
106 const auto ptr = get(name);
108 return ptr->view<Derived>();
111 template <
typename Derived>
112 void import_data(
const std::string& name, Derived&& values)
114 set(name, std::move(values.derived()));
117 template <
typename Derived>
118 void export_data(
const std::string& name, Eigen::PlainObjectBase<Derived>& values)
120 auto attr = get(name);
122 auto value_array = attr->get();
125 bool validate =
true;
129 Derived& value_matrix = value_array->template get<Derived>();
130 values.swap(value_matrix);
131 }
catch (std::runtime_error& e) {
134 logger().warn(
"Export cannot be done without coping: {}", e.what());
135 values = value_array->view<Derived>();
148 void remove(
const std::string& name)
150 auto itr = m_data.find(name);
151 la_runtime_assert(itr != m_data.end(),
"Attribute " + name +
" does not exist.");
155 template <
typename Archive>
156 void serialize_impl(Archive& ar)
160 ar.object([&](
auto& ar) { ar(
"data", DATA) & m_data; });
161 LA_IGNORE_SHADOW_WARNING_END
165 std::map<std::string, std::unique_ptr<Attribute>> m_data;