17#include <lagrange/Logger.h>
18#include <lagrange/common.h>
19#include <lagrange/experimental/Attribute.h>
20#include <lagrange/utils/assert.h>
21#include <lagrange/utils/warning.h>
24namespace experimental {
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 {
42 bool has(
const std::string& name)
const {
return m_data.find(name) != m_data.end(); }
44 void add(
const std::string& name) { m_data.emplace(name, std::make_unique<Attribute>()); }
46 template <
typename Derived>
47 void add(
const std::string& name, Derived&& values)
49 auto attr = std::make_unique<Attribute>(std::forward<Derived>(values));
50 m_data.emplace(name, std::move(attr));
53 template <
typename Derived>
54 void set(
const std::string& name, Derived&& values)
56 auto itr = m_data.find(name);
58 if (itr->second ==
nullptr) {
59 auto attr = std::make_unique<Attribute>();
60 attr->set(std::forward<Derived>(values));
61 itr->second = std::move(attr);
63 itr->second->set(std::forward<Derived>(values));
69 auto itr = m_data.find(name);
71 return itr->second.get();
74 const Attribute* get(
const std::string& name)
const
76 auto itr = m_data.find(name);
78 return itr->second.get();
81 template <
typename Derived>
82 decltype(
auto) get(
const std::string& name)
86 return ptr->get<Derived>();
89 template <
typename Derived>
90 decltype(
auto) get(
const std::string& name)
const
92 const auto ptr = get(name);
94 return ptr->get<Derived>();
97 template <
typename Derived>
98 decltype(
auto) view(
const std::string& name)
100 auto ptr = get(name);
102 return ptr->view<Derived>();
105 template <
typename Derived>
106 decltype(
auto) view(
const std::string& name)
const
108 const auto ptr = get(name);
110 return ptr->view<Derived>();
113 template <
typename Derived>
114 void import_data(
const std::string& name, Derived&& values)
116 set(name, std::move(values.derived()));
119 template <
typename Derived>
120 void export_data(
const std::string& name, Eigen::PlainObjectBase<Derived>& values)
122 auto attr = get(name);
124 auto value_array = attr->get();
127 bool validate =
true;
131 Derived& value_matrix = value_array->template get<Derived>();
132 values.swap(value_matrix);
133 }
catch (std::runtime_error& e) {
136 logger().warn(
"Export cannot be done without coping: {}", e.what());
137 values = value_array->view<Derived>();
150 void remove(
const std::string& name)
152 auto itr = m_data.find(name);
153 la_runtime_assert(itr != m_data.end(),
"Attribute " + name +
" does not exist.");
157 template <
typename Archive>
158 void serialize_impl(Archive& ar)
162 ar.object([&](
auto& ar) {
163 ar(
"data", DATA) & m_data;
165 LA_IGNORE_SHADOW_WARNING_END
169 std::map<std::string, std::unique_ptr<Attribute>> m_data;
174template <
typename Archive>
175void serialize(std::pair<std::string, Attribute>& entry, Archive& ar)
177 enum { KEY = 0, VALUE = 1 };
179 ar.object([&](
auto& ar) {
180 ar(
"key", KEY) & entry.first;
181 ar(
"value", VALUE) & entry.second;
185template <
typename Archive>
187 std::map<std::string, std::unique_ptr<Attribute>>& attrs,
190 std::vector<std::pair<std::string, Attribute>> data;
191 if (!ar.is_input()) {
192 for (
auto& itr : attrs) {
193 data.emplace_back(itr.first, *itr.second);
201 for (
const auto& itr : data) {
202 attrs[itr.first] = std::make_unique<Attribute>(itr.second);
207template <
typename Archive>
208void serialize(AttributeManager& attributes, Archive& ar)
210 attributes.serialize_impl(ar);
213LA_IGNORE_SHADOW_WARNING_END
Definition: Attribute.h:23
Definition: AttributeManager.h:27
Smart pointer with value semantics.
Definition: value_ptr.h:134
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:40
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
#define LA_IGNORE_SHADOW_WARNING_BEGIN
Ignore shadow warnings.
Definition: warning.h:68
Main namespace for Lagrange.
Definition: AABBIGL.h:30