Visual Computing Library
Loading...
Searching...
No Matches
custom_component_vector_handle.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_CONTAINERS_CUSTOM_COMPONENT_VECTOR_HANDLE_H
24#define VCL_MESH_CONTAINERS_CUSTOM_COMPONENT_VECTOR_HANDLE_H
25
26#include <vclib/types.h>
27
28#include <any>
29#include <vector>
30
31namespace vcl {
32
56template<typename T>
58{
59 std::vector<std::reference_wrapper<T>> mVec;
60
61public:
62 class Iterator : public std::vector<std::reference_wrapper<T>>::iterator
63 {
64 using Base = std::vector<std::reference_wrapper<T>>::iterator;
65
66 public:
67 using value_type = T;
68 using reference = value_type&;
69 using pointer = value_type*;
70
71 Iterator(Base it) : Base(it) {}
72
73 reference operator*() const { return Base::operator*().get(); }
74
75 pointer operator->() const { return &(Base::operator*().get()); }
76 };
77
79 public std::vector<std::reference_wrapper<T>>::const_iterator
80 {
81 using Base = std::vector<std::reference_wrapper<T>>::const_iterator;
82
83 public:
84 using value_type = T;
85 using reference = const value_type&;
86 using pointer = const value_type*;
87
88 ConstIterator(Base it) : Base(it) {}
89
90 reference operator*() const { return Base::operator*().get(); }
91
92 pointer operator->() const { return &(Base::operator*().get()); }
93 };
94
96
97 CustomComponentVectorHandle(std::vector<std::any>& cc)
98 {
99 mVec.reserve(cc.size());
100 for (uint i = 0; i < cc.size(); ++i) {
101 mVec.push_back(std::any_cast<T&>(cc[i]));
102 }
103 }
104
105 T& at(uint i) { return mVec[i].get(); }
106
107 const T& at(uint i) const { return mVec[i].get(); }
108
109 T& front() { return mVec.begin()->get(); }
110
111 const T& front() const { return mVec.begin()->get(); }
112
113 T& back() { return std::prev(mVec.end())->get(); }
114
115 const T& back() const { return std::prev(mVec.end())->get(); }
116
117 uint size() const { return mVec.size(); }
118
119 T& operator[](uint i) { return mVec[i].get(); }
120
121 const T& operator[](uint i) const { return mVec[i].get(); }
122
123 Iterator begin() { return Iterator(mVec.begin()); }
124
125 Iterator end() { return Iterator(mVec.end()); }
126
127 ConstIterator begin() const { return ConstIterator(mVec.begin()); }
128
129 ConstIterator end() const { return ConstIterator(mVec.end()); }
130};
131
132template<typename T>
133using ConstCustomComponentVectorHandle = CustomComponentVectorHandle<const T>;
134
135} // namespace vcl
136
137#endif // VCL_MESH_CONTAINERS_CUSTOM_COMPONENT_VECTOR_HANDLE_H
Definition custom_component_vector_handle.h:80
Definition custom_component_vector_handle.h:63
Allows to access directly to a custom component.
Definition custom_component_vector_handle.h:58
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43