Visual Computing Library
Loading...
Searching...
No Matches
custom_components.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_MESH_COMPONENTS_CUSTOM_COMPONENTS_H
24#define VCL_MESH_COMPONENTS_CUSTOM_COMPONENTS_H
25
26#include "detail/custom_components_data.h"
27
28#include <vclib/concepts/mesh/components/custom_components.h>
29
30#include <any>
31#include <string>
32#include <vector>
33
34namespace vcl::comp {
35
90template<typename ParentElemType = void>
92{
93 static const bool IS_VERTICAL = !std::is_same_v<ParentElemType, void>;
94
95 detail::CustomComponentsData<ParentElemType, IS_VERTICAL> mData;
96
97public:
101 static const uint COMPONENT_ID = CompId::CUSTOM_COMPONENTS;
102
103 /* Constructors */
104
108 CustomComponents() = default;
109
110 /* Member functions */
111
120 bool hasCustomComponent(const std::string& compName) const
121 {
122 return mData.componentExists(
123 compName, static_cast<const ParentElemType*>(this));
124 }
125
143 template<typename CompType>
144 bool isCustomComponentOfType(const std::string& compName) const
145 {
146 return mData.template isComponentOfType<CompType>(
147 compName, static_cast<const ParentElemType*>(this));
148 }
149
165 std::type_index customComponentType(const std::string& compName) const
166 {
167 return mData.componentType(
168 compName, static_cast<const ParentElemType*>(this));
169 }
170
179 template<typename CompType>
180 std::vector<std::string> customComponentNamesOfType() const
181 {
182 return mData.template componentNamesOfType<CompType>(
183 static_cast<const ParentElemType*>(this));
184 }
185
209 template<typename CompType>
210 const CompType& customComponent(const std::string& compName) const
211 {
212 return mData.template get<CompType>(
213 compName, static_cast<const ParentElemType*>(this));
214 }
215
239 template<typename CompType>
240 CompType& customComponent(const std::string& compName)
241 {
242 return mData.template get<CompType>(
243 compName, static_cast<ParentElemType*>(this));
244 }
245
246 template<typename CompType>
247 void addCustomComponent(
248 const std::string& compName,
249 const CompType& value = CompType()) requires (!IS_VERTICAL)
250 {
251 return mData.template addCustomComponent<CompType>(compName, value);
252 }
253
254 void deleteCustomComponent(const std::string& compName)
255 requires (!IS_VERTICAL)
256 {
257 return mData.deleteCustomComponent(compName);
258 }
259
260protected:
261 template<typename Element>
262 void importFrom(const Element& e, bool = true)
263 {
264 // if the component is vertical, the import is managed by the container.
265 // if is horizontal, it must be managed by the component itself.
266 if constexpr (!IS_VERTICAL && HasCustomComponents<Element>) {
267 mData = e.CustomComponents::mData;
268 }
269 }
270
271 void serialize(std::ostream& os) const
272 {
273 // todo
274 }
275
276 void deserialize(std::istream& is)
277 {
278 // todo
279 }
280};
281
282} // namespace vcl::comp
283
284#endif // VCL_MESH_COMPONENTS_CUSTOM_COMPONENTS_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
The CustomComponents Component is a container of additional components associated to an Element (e....
Definition custom_components.h:92
std::type_index customComponentType(const std::string &compName) const
Returns the std::type_index of the custom component of the given name.
Definition custom_components.h:165
bool isCustomComponentOfType(const std::string &compName) const
Returns true if the custom component of the given name is of the type given as template argument,...
Definition custom_components.h:144
std::vector< std::string > customComponentNamesOfType() const
Returns a std::vector of std::strings containing the names of the custom components of the type given...
Definition custom_components.h:180
static const uint COMPONENT_ID
The ID of component.
Definition custom_components.h:101
CustomComponents()=default
Initilizes an empty container of custom components.
const CompType & customComponent(const std::string &compName) const
Returns the const reference to the custom component of the given name having the type given as templa...
Definition custom_components.h:210
CompType & customComponent(const std::string &compName)
Returns the reference to the custom component of the given name having the type given as template arg...
Definition custom_components.h:240
bool hasCustomComponent(const std::string &compName) const
Returns true if the element has a custom component with the given name, false otherwise....
Definition custom_components.h:120