23#ifndef VCL_MESH_CONTAINERS_DETAIL_CUSTOM_COMPONENTS_VECTOR_MAP_H
24#define VCL_MESH_CONTAINERS_DETAIL_CUSTOM_COMPONENTS_VECTOR_MAP_H
26#include <vclib/mesh/exceptions.h>
28#include <vclib/base.h>
33#include <unordered_map>
36namespace vcl::mesh::detail {
64template<
bool HasCustomComponent>
65class CustomComponentsVectorMap
70class CustomComponentsVectorMap<true>
74 std::unordered_map<std::string, std::vector<std::any>> mMap;
81 mutable std::unordered_map<std::string, bool> mNeedToInitialize;
83 std::unordered_map<std::string, std::type_index> mCompType;
92 mNeedToInitialize.clear();
100 void reserve(uint size)
102 for (
auto& p : mMap) {
103 p.second.reserve(size);
112 void resize(uint size)
114 for (
auto& p : mMap) {
121 if (p.second.size() < size)
122 mNeedToInitialize.at(p.first) =
true;
123 p.second.resize(size);
139 void compact(
const std::vector<uint>& newIndices)
141 for (
auto& p : mMap) {
142 compactVector(p.second, newIndices);
159 template<
typename CompType>
160 void addNewComponent(
const std::string& name, uint size)
162 std::vector<std::any>& v = mMap[name];
163 v.resize(size, CompType());
164 mNeedToInitialize[name] =
false;
165 mCompType.emplace(name,
typeid(CompType));
173 void deleteComponent(
const std::string& name)
176 mNeedToInitialize.erase(name);
177 mCompType.erase(name);
185 void assertComponentExists(
const std::string& compName)
const
188 assert(mMap.find(compName) != mMap.end());
196 bool componentExists(
const std::string& compName)
const
198 return (mMap.find(compName) != mMap.end());
206 std::vector<std::string> allComponentNames()
const
208 std::vector<std::string> names;
209 names.reserve(mMap.size());
210 for (
const auto& p : mMap)
211 names.push_back(p.first);
224 template<
typename CompType>
225 bool isComponentOfType(
const std::string& compName)
const
227 std::type_index t(
typeid(CompType));
229 return t == mCompType.at(compName);
241 std::type_index componentType(
const std::string& compName)
const
243 return mCompType.at(compName);
254 template<
typename CompType>
255 std::vector<std::string> allComponentNamesOfType()
const
257 std::vector<std::string> names;
258 std::type_index t(
typeid(CompType));
259 for (
const auto& p : mCompType) {
261 names.push_back(p.first);
278 template<
typename CompType>
279 const std::vector<std::any>& componentVector(
280 const std::string& compName)
const
282 checkComponentType<CompType>(compName);
283 std::vector<std::any>& v =
284 const_cast<std::vector<std::any>&
>(mMap.at(compName));
286 if (mNeedToInitialize.at(compName)) {
287 for (std::any& a : v) {
291 mNeedToInitialize.at(compName) =
false;
311 template<
typename CompType>
312 std::vector<std::any>& componentVector(
const std::string& compName)
314 checkComponentType<CompType>(compName);
315 std::vector<std::any>& v = mMap.at(compName);
317 if (mNeedToInitialize.at(compName)) {
318 for (std::any& a : v) {
322 mNeedToInitialize.at(compName) =
false;
327 void importSameCustomComponentFrom(
330 const std::string& compName,
331 const CustomComponentsVectorMap<true>& other)
333 if (other.componentExists(compName) && componentExists(compName)) {
334 if (other.componentType(compName) == componentType(compName)) {
335 mMap.at(compName)[thisPos] = other.mMap.at(compName)[otherPos];
340 template<
typename CompType>
341 void serializeCustomComponentsOfType(std::ostream& os)
const
343 std::vector<std::string> compNames =
344 allComponentNamesOfType<CompType>();
345 vcl::serialize(os, compNames);
346 for (
const auto& name : compNames) {
347 bool b = mNeedToInitialize.at(name);
348 vcl::serialize(os, b);
349 const std::vector<std::any>& v = componentVector<CompType>(name);
354 template<
typename CompType>
355 void deserializeCustomComponentsOfType(std::istream& is)
357 std::vector<std::string> compNames;
358 vcl::deserialize(is, compNames);
359 for (
const auto& name : compNames) {
361 vcl::deserialize(is, b);
362 std::vector<std::any> v;
365 mNeedToInitialize[name] = b;
366 mCompType.emplace(name,
typeid(CompType));
371 template<
typename CompType>
372 void checkComponentType(
const std::string& compName)
const
374 std::type_index t(
typeid(CompType));
375 if (t != mCompType.at(compName)) {
376 throw BadCustomComponentTypeException(
377 "Expected type " + std::string(mCompType.at(compName).name()) +
378 " for " + compName +
", but was " + std::string(t.name()) +
A class representing a box in N-dimensional space.
Definition box.h:46