23#ifndef VCL_MESH_COMPONENTS_DETAIL_CUSTOM_COMPONENTS_DATA_H
24#define VCL_MESH_COMPONENTS_DETAIL_CUSTOM_COMPONENTS_DATA_H
26#include <vclib/types.h>
31#include <unordered_map>
34namespace vcl::comp::detail {
48template<
typename ElementType,
bool VERTICAL>
49class CustomComponentsData
51 std::unordered_map<std::string, std::any> mMap;
52 std::unordered_map<std::string, std::type_index> mCompType;
55 bool componentExists(
const std::string& compName,
const ElementType*)
const
57 return (mMap.find(compName) != mMap.end());
60 template<
typename CompType>
61 bool isComponentOfType(
const std::string& compName,
const ElementType*)
64 std::type_index t(
typeid(CompType));
65 return t == mCompType.at(compName);
68 std::type_index componentType(
69 const std::string& compName,
70 const ElementType*)
const
72 return mCompType.at(compName);
75 template<
typename CompType>
76 std::vector<std::string> componentNamesOfType(
const ElementType*)
const
78 std::vector<std::string> names;
79 std::type_index t(
typeid(CompType));
80 for (
const auto& p : mCompType) {
82 names.push_back(p.first);
87 template<
typename CompType>
88 const CompType& get(
const std::string& compName,
const ElementType*)
const
90 return std::any_cast<const CompType&>(mMap.at(compName));
93 template<
typename CompType>
94 CompType& get(
const std::string& compName, ElementType*)
96 return std::any_cast<CompType&>(mMap.at(compName));
99 template<
typename CompType>
100 void addCustomComponent(
101 const std::string& compName,
102 const CompType c = CompType())
105 mCompType.emplace(compName,
typeid(CompType));
108 void deleteCustomComponent(
const std::string& compName)
110 mMap.erase(compName);
111 mCompType.erase(compName);
119template<
typename ElementType>
120class CustomComponentsData<ElementType, true>
123 bool componentExists(
const std::string& compName,
const ElementType* elem)
126 return ccVec(elem).componentExists(compName);
129 template<
typename CompType>
130 bool isComponentOfType(
const std::string& compName,
const ElementType* elem)
133 return ccVec(elem).template isComponentOfType<CompType>(compName);
136 std::type_index componentType(
137 const std::string& compName,
138 const ElementType* elem)
const
140 return ccVec(elem).componentType(compName);
143 template<
typename CompType>
144 std::vector<std::string> componentNamesOfType(
const ElementType* elem)
const
146 return ccVec(elem).template allComponentNamesOfType<CompType>();
149 template<
typename CompType>
150 const CompType& get(
const std::string& compName,
const ElementType* elem)
153 return std::any_cast<const CompType&>(
154 ccVec(elem).
template componentVector<CompType>(
155 compName)[thisId(elem)]);
158 template<
typename CompType>
159 CompType& get(
const std::string& compName, ElementType* elem)
161 return std::any_cast<CompType&>(
162 ccVec(elem).
template componentVector<CompType>(
163 compName)[thisId(elem)]);
167 uint thisId(
const ElementType* elem)
const
169 assert(elem->parentMesh());
170 return elem->index();
173 auto& ccVec(ElementType* elem)
175 assert(elem->parentMesh());
177 return elem->parentMesh()->template customComponents<ElementType>();
180 const auto& ccVec(
const ElementType* elem)
const
182 assert(elem->parentMesh());
184 return elem->parentMesh()->template customComponents<ElementType>();