Visual Computing Library  devel
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 "base/base.h"
27#include "detail/custom_components_data.h"
28
29#include <vclib/base.h>
30
31#include <any>
32#include <string>
33#include <vector>
34
35namespace vcl::comp {
36
91template<typename ParentElemType = void>
93{
94 static const bool IS_VERTICAL = !std::is_same_v<ParentElemType, void>;
95
96 detail::CustomComponentsData<ParentElemType, IS_VERTICAL> mData;
97
98public:
102 static const uint COMPONENT_ID = CompId::CUSTOM_COMPONENTS;
103
104 /* Constructors */
105
109 CustomComponents() = default;
110
111 /* Member functions */
112
121 bool hasCustomComponent(const std::string& compName) const
122 {
123 return mData.componentExists(
124 compName, static_cast<const ParentElemType*>(this));
125 }
126
144 template<typename CompType>
145 bool isCustomComponentOfType(const std::string& compName) const
146 {
147 return mData.template isComponentOfType<CompType>(
148 compName, static_cast<const ParentElemType*>(this));
149 }
150
166 std::type_index customComponentType(const std::string& compName) const
167 {
168 return mData.componentType(
169 compName, static_cast<const ParentElemType*>(this));
170 }
171
180 template<typename CompType>
181 std::vector<std::string> customComponentNamesOfType() const
182 {
183 return mData.template componentNamesOfType<CompType>(
184 static_cast<const ParentElemType*>(this));
185 }
186
210 template<typename CompType>
211 const CompType& customComponent(const std::string& compName) const
212 {
213 return mData.template get<CompType>(
214 compName, static_cast<const ParentElemType*>(this));
215 }
216
240 template<typename CompType>
241 CompType& customComponent(const std::string& compName)
242 {
243 return mData.template get<CompType>(
244 compName, static_cast<ParentElemType*>(this));
245 }
246
247 template<typename CompType>
248 void addCustomComponent(
249 const std::string& compName,
250 const CompType& value = CompType()) requires (!IS_VERTICAL)
251 {
252 return mData.template addCustomComponent<CompType>(compName, value);
253 }
254
255 void deleteCustomComponent(const std::string& compName)
256 requires (!IS_VERTICAL)
257 {
258 return mData.deleteCustomComponent(compName);
259 }
260
261 template<typename CompType>
262 void serializeCustomComponentsOfType(std::ostream& os) const
263 requires (!IS_VERTICAL)
264 {
265 std::vector<std::string> compNames =
266 customComponentNamesOfType<CompType>();
267 vcl::serialize(os, compNames);
268 for (const auto& name : compNames) {
269 const CompType& c = customComponent<CompType>(name);
270 vcl::serialize(os, c);
271 }
272 }
273
274 template<typename CompType>
275 void deserializeCustomComponentsOfType(std::istream& is)
276 requires (!IS_VERTICAL)
277 {
278 std::vector<std::string> compNames;
279 vcl::deserialize(is, compNames);
280 for (const auto& name : compNames) {
281 CompType c;
282 vcl::deserialize(is, c);
283 addCustomComponent<CompType>(name, c);
284 }
285 }
286
287protected:
288 template<typename Element>
289 void importFrom(const Element& e, bool = true);
290
291 void serialize(std::ostream& os) const
292 {
293 // todo
294 }
295
296 void deserialize(std::istream& is)
297 {
298 // todo
299 }
300};
301
302/* concept */
303
315template<typename T>
317 IsDerivedFromSpecializationOfV<T, CustomComponents>;
318
319/* importFrom function */
320
321template<typename ParentElemType>
322template<typename Element>
323void CustomComponents<ParentElemType>::importFrom(const Element& e, bool)
324{
325 // if the component is vertical, the import is managed by the container.
326 // if is horizontal, it must be managed by the component itself.
327 if constexpr (!IS_VERTICAL && HasCustomComponents<Element>) {
328 mData = e.CustomComponents::mData;
329 }
330}
331
332} // namespace vcl::comp
333
334#endif // VCL_MESH_COMPONENTS_CUSTOM_COMPONENTS_H
A class representing a box in N-dimensional space.
Definition box.h:46
The CustomComponents Component is a container of additional components associated to an Element (e....
Definition custom_components.h:93
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:166
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:145
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:181
static const uint COMPONENT_ID
The ID of component.
Definition custom_components.h:102
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:211
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:241
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:121
A concept that checks whether a type T (that should be an Element or a Mesh) has the CustomComponents...
Definition custom_components.h:316