Visual Computing Library  devel
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_BASE_CUSTOM_COMPONENT_VECTOR_HANDLE_H
24#define VCL_MESH_CONTAINERS_BASE_CUSTOM_COMPONENT_VECTOR_HANDLE_H
25
26#include <vclib/base.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 using Base::Base;
72
73 Iterator(Base it) : Base(it) {}
74
75 reference operator*() const { return Base::operator*().get(); }
76
77 pointer operator->() const { return &(Base::operator*().get()); }
78 };
79
81 public std::vector<std::reference_wrapper<T>>::const_iterator
82 {
83 using Base = std::vector<std::reference_wrapper<T>>::const_iterator;
84
85 public:
86 using value_type = T;
87 using reference = const value_type&;
88 using pointer = const value_type*;
89
90 using Base::Base;
91
92 ConstIterator(Base it) : Base(it) {}
93
94 reference operator*() const { return Base::operator*().get(); }
95
96 pointer operator->() const { return &(Base::operator*().get()); }
97 };
98
100
101 CustomComponentVectorHandle(std::vector<std::any>& cc)
102 {
103 mVec.reserve(cc.size());
104 for (uint i = 0; i < cc.size(); ++i) {
105 mVec.push_back(std::any_cast<T&>(cc[i]));
106 }
107 }
108
109 T& at(uint i) { return mVec[i].get(); }
110
111 const T& at(uint i) const { return mVec[i].get(); }
112
113 T& front() { return mVec.begin()->get(); }
114
115 const T& front() const { return mVec.begin()->get(); }
116
117 T& back() { return std::prev(mVec.end())->get(); }
118
119 const T& back() const { return std::prev(mVec.end())->get(); }
120
121 uint size() const { return mVec.size(); }
122
123 T& operator[](uint i) { return mVec[i].get(); }
124
125 const T& operator[](uint i) const { return mVec[i].get(); }
126
127 Iterator begin() { return Iterator(mVec.begin()); }
128
129 Iterator end() { return Iterator(mVec.end()); }
130
131 ConstIterator begin() const { return ConstIterator(mVec.begin()); }
132
133 ConstIterator end() const { return ConstIterator(mVec.end()); }
134};
135
136template<typename T>
137using ConstCustomComponentVectorHandle = CustomComponentVectorHandle<const T>;
138
139} // namespace vcl
140
141#endif // VCL_MESH_CONTAINERS_BASE_CUSTOM_COMPONENT_VECTOR_HANDLE_H
A class representing a box in N-dimensional space.
Definition box.h:46
PointT size() const
Computes the size of the box.
Definition box.h:267
Definition custom_component_vector_handle.h:82
Definition custom_component_vector_handle.h:63
Allows to access directly to a custom component.
Definition custom_component_vector_handle.h:58