Visual Computing Library  devel
Loading...
Searching...
No Matches
polymorphic_object_vector.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_SPACE_CORE_VECTOR_POLYMORPHIC_OBJECT_VECTOR_H
24#define VCL_SPACE_CORE_VECTOR_POLYMORPHIC_OBJECT_VECTOR_H
25
26#include "pointer_vector.h"
27
28namespace vcl {
29
62template<Cloneable T, int N = -1>
63class PolymorphicObjectVector : protected PointerVector<std::shared_ptr<T>, N>
64{
66 using BaseVector = Base::Vector;
67
68public:
69 // types
70 using ValueType = Base::ValueType;
71
73
74 // the iterator of this vector is the const iterator of the base Vector
75 // because we don't want to allow to modify the shared pointers contained
76 // in the vector, but allow only to modify the objects pointed by the shared
77 // pointers
78 using Iterator = BaseVector::ConstIterator;
79
80 // the const iterator of this vector is the const iterator of the
81 // PointerVector, which transforms pointers to const pointers
83
84 using Base::SIZE;
85
86 // constructors
87 using Base::Base;
88
89 // exposing members of base class, later we will redefine non-const members
90 // that should return values instead of references, and functions that
91 // allow to set directly shared pointers
92 using Base::at;
93 using Base::atMod;
94 using Base::back;
95 using Base::clear;
96 using Base::contains;
97 using Base::data;
98 using Base::empty;
99 using Base::erase;
100 using Base::fill;
101 using Base::find;
102 using Base::front;
103 using Base::indexOf;
104 using Base::insert;
105 using Base::pushBack;
106 using Base::resize;
107 using Base::set;
108 using Base::size;
109 using Base::swap;
110 using Base::operator[];
111 using Base::operator();
112 using Base::begin;
113 using Base::end;
114
126 {
127 if constexpr (N < 0) {
129 }
130 std::transform(
131 other.begin(), other.end(), Base::begin(), [](const auto& e) {
132 return e->clone();
133 });
134 }
135
145
161 PolymorphicObjectVector(std::size_t size, const T& value)
162 {
163 if constexpr (N >= 0) {
164 if (size != N) {
165 throw WrongSizeException(
166 "Vector must have " + std::to_string(N) + " size.");
167 }
168 fill(value);
169 }
170 else {
171 resize(size, value);
172 }
173 }
174
192 template<typename ItType>
194 {
195 set(View(first, last));
196 }
197
212 template<Range RangeType>
217
231 ValueType at(uint i) { return Base::at(i); }
232
243 ValueType atMod(int i) { return Base::atMod(i); }
244
253 ValueType front() { return Base::front(); }
254
263 ValueType back() { return Base::back(); }
264
274 void set(uint i, const T& e)
275 {
276 assert(i < Base::size());
277 Base::at(i) = e.clone();
278 }
279
289 void set(Base::ConstIterator it, const T& e)
290 {
291 assert(it < Base::end());
292 Base::at(it - Base::begin()) = e.clone();
293 }
294
311 template<Range Rng>
312 void set(Rng&& r) requires InputRange<Rng, T>
313 {
314 uint n = std::ranges::distance(r);
315
316 if constexpr (N >= 0) {
317 n = std::min((uint) N, n);
318 }
319 else {
321 }
322
323 // for each element in the range, clone it and store it in the
324 // vector
325 std::transform(
326 std::ranges::begin(r),
327 std::ranges::begin(r) + n,
328 Base::begin(),
329 [](const auto& e) {
330 return e.clone();
331 });
332 }
333
343 void fill(const T& e)
344 {
345 for (uint i = 0; i < size(); i++) {
346 Base::at(i) = e.clone();
347 }
348 }
349
350 /* Member functions specific for dynamic vector */
351
366 void resize(uint n, const T& v) requires (N < 0)
367 {
368 if (n > Base::size()) {
369 uint oldSize = Base::size();
371 for (uint i = oldSize; i < n; i++) {
372 Base::at(i) = v.clone();
373 }
374 }
375 else {
377 }
378 }
379
392 void pushBack(const T& v) requires (N < 0) { Base::pushBack(v.clone()); }
393
408 void pushBack(T&& v) requires (N < 0)
409 {
410 Base::pushBack(std::move(v).clone());
411 }
412
427 void insert(uint i, const T& v) requires (N < 0)
428 {
429 assert(i < Base::size() + 1);
430 Base::insert(i, v.clone());
431 }
432
449 void insert(uint i, T&& v) requires (N < 0)
450 {
451 assert(i < Base::size() + 1);
452 Base::insert(i, std::move(v).clone());
453 }
454
461
472 {
473 a.swap(b);
474 }
475
476 /* Operators */
477
484 ValueType operator[](uint i) { return Base::operator[](i); }
485
492 ValueType operator()(uint i) { return Base::operator()(i); }
493
505
506 /* Iterator member functions */
507
513 Iterator begin() { return Base::begin(); }
514
520 Iterator end() { return Base::end(); }
521};
522
523} // namespace vcl
524
525#endif // VCL_SPACE_CORE_VECTOR_POLYMORPHIC_OBJECT_VECTOR_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
The ConstPointerIterator class is a utility class that wraps an iterator of a container of [shared] p...
Definition const_pointer_iterator.h:57
The PointerVector class is a utility container that stores a sequence of pointers that preserve their...
Definition pointer_vector.h:73
std::size_t size() const
Returns the size of the container.
Definition vector.h:218
void resize(uint n, const T &v=T())
Resize the Vector to the specified size.
Definition vector.h:554
void erase(uint i)
Remove the element at the specified index from the Vector.
Definition vector.h:661
bool contains(const MakeConstPointerT< T > &e) const
Check if the Vector contains the specified element.
Definition vector.h:468
bool empty() const noexcept
Returns whether the vector is empty (i.e. whether its size is 0).
Definition vector.h:648
void set(uint i, const T &e)
Set the value of the element at the specified position.
Definition vector.h:363
void fill(const T &e)
Fill all elements of the Vector with the specified value.
Definition vector.h:454
Vector()=default
Creates an empty Vector object.
uint indexOf(const MakeConstPointerT< T > &e) const
Get the index of the first occurrence of the specified element in the Vector.
Definition vector.h:516
void pushBack(const T &v)
Add an element to the end of the Vector.
Definition vector.h:570
void insert(uint i, const T &v)
Insert an element at the specified position in the Vector.
Definition vector.h:600
void clear()
Remove all elements from the Vector.
Definition vector.h:675
friend void swap(Vector &a, Vector &b)
Definition vector.h:539
static const int SIZE
Size of the vector at compile time. It will be -1 if the Vector has dynamic size.
Definition vector.h:108
The PolymorphicObjectVector class is a container that stores a collection of polymorphic objects,...
Definition polymorphic_object_vector.h:64
std::size_t size() const
Returns the size of the container.
Definition vector.h:218
PolymorphicObjectVector & operator=(PolymorphicObjectVector other)
Assignment operator of the PolymorphicObjectVector.
Definition polymorphic_object_vector.h:500
ValueType atMod(int i)
Access the specified element, computing first the module of the position w.r.t. the size of the conta...
Definition polymorphic_object_vector.h:243
friend void swap(PolymorphicObjectVector &a, PolymorphicObjectVector &b)
Definition polymorphic_object_vector.h:471
void pushBack(T &&v)
Add an element to the end of the Vector.
Definition polymorphic_object_vector.h:408
ValueType at(uint i)
Access the specified element with bounds checking.
Definition polymorphic_object_vector.h:231
Iterator begin()
Return an iterator pointing to the beginning of the Vector.
Definition polymorphic_object_vector.h:513
void resize(uint n, const T &v)
Resize the Vector to the specified size.
Definition polymorphic_object_vector.h:366
void fill(const T &e)
Fill all elements of the Vector with clones of the specified value.
Definition polymorphic_object_vector.h:343
PolymorphicObjectVector(ItType first, ItType last)
Constructs the container with the contents of the range [first, last).
Definition polymorphic_object_vector.h:193
void set(Base::ConstIterator it, const T &e)
Set the value of the element at the specified position.
Definition polymorphic_object_vector.h:289
void swap(PolymorphicObjectVector &other)
Swaps the contents of the container with those of other.
Definition polymorphic_object_vector.h:460
void set(uint i, const T &e)
Set the value of the element at the specified position.
Definition polymorphic_object_vector.h:274
void pushBack(const T &v)
Add an element to the end of the Vector.
Definition polymorphic_object_vector.h:392
PolymorphicObjectVector(PolymorphicObjectVector &&other) noexcept
Move constructor.
Definition polymorphic_object_vector.h:141
ValueType operator[](uint i)
Returns a reference to the element at specified location i. No bounds checking is performed.
Definition polymorphic_object_vector.h:484
void set(Rng &&r)
Set the elements of the Vector using the values from a range.
Definition polymorphic_object_vector.h:312
PolymorphicObjectVector(const PolymorphicObjectVector &other)
Creates a Vector object with copied of the elements of the other Vector.
Definition polymorphic_object_vector.h:125
PolymorphicObjectVector(RangeType &&rng)
Constructs the container with the contents of the range rng.
Definition polymorphic_object_vector.h:213
void insert(uint i, const T &v)
Insert an element at the specified position in the Vector.
Definition polymorphic_object_vector.h:427
void insert(uint i, T &&v)
Insert an element at the specified position in the Vector.
Definition polymorphic_object_vector.h:449
ValueType front()
Access the first element of the Vector.
Definition polymorphic_object_vector.h:253
PolymorphicObjectVector(std::size_t size, const T &value)
Creates a Vector object with the specified size.
Definition polymorphic_object_vector.h:161
ValueType back()
Access the last element of the Vector.
Definition polymorphic_object_vector.h:263
Iterator end()
Return an iterator pointing to the end of the Vector.
Definition polymorphic_object_vector.h:520
ValueType operator()(uint i)
Returns a reference to the element at specified location i. No bounds checking is performed.
Definition polymorphic_object_vector.h:492
The Vector class is a generic container of objects of type T, that could have fixed or dynamic size,...
Definition vector.h:62
The View class is a simple class that stores and exposes two iterators begin and end.
Definition view.h:67
Exception thrown when the size (generally of a container) is not the expected one.
Definition exceptions.h:45
Utility concept that is evaluated true the Range R is an Input Range and has a value_type that is con...
Definition range.h:89