Fast RTPS  Version 2.8.0
Fast RTPS
ResourceLimitedVector.hpp
1// Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
20#ifndef FASTRTPS_UTILS_COLLECTIONS_RESOURCELIMITEDVECTOR_HPP_
21#define FASTRTPS_UTILS_COLLECTIONS_RESOURCELIMITEDVECTOR_HPP_
22
23#include "ResourceLimitedContainerConfig.hpp"
24
25#include <assert.h>
26#include <algorithm>
27#include <type_traits>
28#include <vector>
29
30namespace eprosima {
31namespace fastrtps {
32
52template <
53 typename _Ty,
54 typename _KeepOrderEnabler = std::false_type,
55 typename _LimitsConfig = ResourceLimitedContainerConfig,
56 typename _Alloc = std::allocator<_Ty>,
57 typename _Collection = std::vector<_Ty, _Alloc>>
59{
60public:
61
62 using configuration_type = _LimitsConfig;
63 using collection_type = _Collection;
64 using value_type = _Ty;
65 using allocator_type = _Alloc;
66 using pointer = typename collection_type::pointer;
67 using const_pointer = typename collection_type::const_pointer;
68 using reference = typename collection_type::reference;
69 using const_reference = typename collection_type::const_reference;
70 using size_type = typename collection_type::size_type;
71 using difference_type = typename collection_type::difference_type;
72 using iterator = typename collection_type::iterator;
73 using const_iterator = typename collection_type::const_iterator;
74 using reverse_iterator = typename collection_type::reverse_iterator;
75 using const_reverse_iterator = typename collection_type::const_reverse_iterator;
76
91 const allocator_type& alloc = allocator_type())
92 : configuration_(cfg)
93 , collection_(alloc)
94 {
95 collection_.reserve(cfg.initial);
96 }
97
99 const ResourceLimitedVector& other)
101 , collection_(other.collection_.get_allocator())
102 {
103 collection_.reserve(other.collection_.capacity());
104 collection_.assign(other.collection_.begin(), other.collection_.end());
105 }
106
107 virtual ~ResourceLimitedVector () = default;
108
110 const ResourceLimitedVector& other)
111 {
112 clear();
113 for (const_reference item : other)
114 {
115 push_back(item);
116 }
117
118 assert(size() == other.size());
119 return *this;
120 }
121
131 const_iterator pos,
132 const value_type& value)
133 {
134 auto dist = std::distance(collection_.cbegin(), pos);
135 if (!ensure_capacity())
136 {
137 return end();
138 }
139
140 return collection_.insert(collection_.cbegin() + dist, value);
141 }
142
152 const_iterator pos,
153 value_type&& value)
154 {
155 auto dist = std::distance(collection_.cbegin(), pos);
156 if (!ensure_capacity())
157 {
158 return end();
159 }
160
161 return collection_.insert(collection_.cbegin() + dist, std::move(value));
162 }
163
175 const value_type& val)
176 {
177 return emplace_back(val);
178 }
179
191 value_type&& val)
192 {
193 if (!ensure_capacity())
194 {
195 // Indicate error by returning null pointer
196 return nullptr;
197 }
198
199 // Move the element at the end of the collection
200 collection_.push_back(std::move(val));
201
202 // Return pointer to newly created element
203 return &collection_.back();
204 }
205
216 template<typename ... Args>
218 Args&& ... args)
219 {
220 if (!ensure_capacity())
221 {
222 // Indicate error by returning null pointer
223 return nullptr;
224 }
225
226 // Construct new element at the end of the collection
227 collection_.emplace_back(args ...);
228
229 // Return pointer to newly created element
230 return &collection_.back();
231 }
232
243 bool remove(
244 const value_type& val)
245 {
246 iterator it = std::find(collection_.begin(), collection_.end(), val);
247 if (it != collection_.end())
248 {
249 do_remove(it);
250 return true;
251 }
252 return false;
253 }
254
270 template<class UnaryPredicate>
272 UnaryPredicate pred)
273 {
274 iterator it = std::find_if(collection_.begin(), collection_.end(), pred);
275 if (it != collection_.end())
276 {
277 do_remove(it);
278 return true;
279 }
280 return false;
281 }
282
297 template <class InputIterator>
298 void assign(
299 InputIterator first,
300 InputIterator last)
301 {
302 size_type n = static_cast<size_type>(std::distance(first, last));
303 n = (std::min)(n, configuration_.maximum);
304 InputIterator value = first;
305 std::advance(value, n);
306 collection_.assign(first, value);
307 }
308
319 void assign(
320 size_type n,
321 const value_type& val)
322 {
323 n = (std::min)(n, configuration_.maximum);
324 collection_.assign(n, val);
325 }
326
338 void assign(
339 std::initializer_list<value_type> il)
340 {
341 size_type n = (std::min)(il.size(), configuration_.maximum);
342 collection_.assign(il.begin(), il.begin() + n);
343 }
344
351 size_type pos)
352 {
353 return collection_.at(pos);
354 }
355
357 size_type pos) const
358 {
359 return collection_.at(pos);
360 }
361
363 size_type pos)
364 {
365 return collection_[pos];
366 }
367
369 size_type pos) const
370 {
371 return collection_[pos];
372 }
373
375 {
376 return collection_.front();
377 }
378
380 {
381 return collection_.front();
382 }
383
385 {
386 return collection_.back();
387 }
388
390 {
391 return collection_.back();
392 }
393
394 iterator begin() noexcept
395 {
396 return collection_.begin();
397 }
398
399 const_iterator begin() const noexcept
400 {
401 return collection_.begin();
402 }
403
404 const_iterator cbegin() const noexcept
405 {
406 return collection_.cbegin();
407 }
408
409 iterator end() noexcept
410 {
411 return collection_.end();
412 }
413
414 const_iterator end() const noexcept
415 {
416 return collection_.end();
417 }
418
419 const_iterator cend() const noexcept
420 {
421 return collection_.cend();
422 }
423
425 {
426 return collection_.rbegin();
427 }
428
430 {
431 return collection_.rbegin();
432 }
433
435 {
436 return collection_.crbegin();
437 }
438
440 {
441 return collection_.rend();
442 }
443
445 {
446 return collection_.rend();
447 }
448
450 {
451 return collection_.crend();
452 }
453
454 bool empty() const noexcept
455 {
456 return collection_.empty();
457 }
458
459 size_type size() const noexcept
460 {
461 return collection_.size();
462 }
463
464 size_type capacity() const noexcept
465 {
466 return collection_.capacity();
467 }
468
469 size_type max_size() const noexcept
470 {
471 return (std::min)(configuration_.maximum, collection_.max_size());
472 }
473
474 void clear()
475 {
476 collection_.clear();
477 }
478
480 const_iterator pos)
481 {
482 return collection_.erase(pos);
483 }
484
486 const_iterator first,
487 const_iterator last)
488 {
489 return collection_.erase(first, last);
490 }
491
492 void pop_back()
493 {
494 collection_.pop_back();
495 }
496
498 {
499 return collection_.data();
500 }
501
502 const value_type* data() const
503 {
504 return collection_.data();
505 }
506
508
516 operator const collection_type& () const noexcept
517 {
518 return collection_;
519 }
520
521protected:
522
525
534 {
535 size_type size = collection_.size();
536 size_type cap = collection_.capacity();
537 if (size == cap)
538 {
539 // collection is full, check resource limit
540 if (cap < configuration_.maximum)
541 {
542 // increase collection capacity
543 assert(configuration_.increment > 0);
544 cap += configuration_.increment;
545 cap = (std::min)(cap, configuration_.maximum);
546 collection_.reserve(cap);
547 }
548 else
549 {
550 return false;
551 }
552 }
553
554 return true;
555 }
556
566 template <typename Enabler = _KeepOrderEnabler>
567 typename std::enable_if<!Enabler::value, void>::type do_remove(
568 iterator it)
569 {
570 // Copy last element into the element being removed
571 if (it != --collection_.end())
572 {
573 *it = std::move(collection_.back());
574 }
575
576 // Then drop last element
577 collection_.pop_back();
578 }
579
590 template <typename Enabler = _KeepOrderEnabler>
591 typename std::enable_if<Enabler::value, void>::type do_remove(
592 iterator it)
593 {
594 collection_.erase(it);
595 }
596
597};
598
599} // namespace fastrtps
600} // namespace eprosima
601
602#endif /* FASTRTPS_UTILS_COLLECTIONS_RESOURCELIMITEDVECTOR_HPP_ */
Resource limited wrapper of std::vector.
Definition: ResourceLimitedVector.hpp:59
void pop_back()
Definition: ResourceLimitedVector.hpp:492
size_type size() const noexcept
Definition: ResourceLimitedVector.hpp:459
configuration_type configuration_
Definition: ResourceLimitedVector.hpp:523
const_reference front() const
Definition: ResourceLimitedVector.hpp:379
typename collection_type::iterator iterator
Definition: ResourceLimitedVector.hpp:72
pointer push_back(value_type &&val)
Add element at the end.
Definition: ResourceLimitedVector.hpp:190
typename collection_type::difference_type difference_type
Definition: ResourceLimitedVector.hpp:71
void assign(std::initializer_list< value_type > il)
Assign vector content.
Definition: ResourceLimitedVector.hpp:338
typename collection_type::reverse_iterator reverse_iterator
Definition: ResourceLimitedVector.hpp:74
std::enable_if< Enabler::value, void >::type do_remove(iterator it)
Remove element.
Definition: ResourceLimitedVector.hpp:591
void assign(size_type n, const value_type &val)
Assign vector content.
Definition: ResourceLimitedVector.hpp:319
const_iterator begin() const noexcept
Definition: ResourceLimitedVector.hpp:399
typename collection_type::const_pointer const_pointer
Definition: ResourceLimitedVector.hpp:67
std::enable_if<!Enabler::value, void >::type do_remove(iterator it)
Remove element.
Definition: ResourceLimitedVector.hpp:567
iterator erase(const_iterator pos)
Definition: ResourceLimitedVector.hpp:479
_LimitsConfig configuration_type
Definition: ResourceLimitedVector.hpp:62
iterator insert(const_iterator pos, value_type &&value)
Insert value before pos.
Definition: ResourceLimitedVector.hpp:151
bool empty() const noexcept
Definition: ResourceLimitedVector.hpp:454
typename collection_type::const_reverse_iterator const_reverse_iterator
Definition: ResourceLimitedVector.hpp:75
ResourceLimitedVector(const ResourceLimitedVector &other)
Definition: ResourceLimitedVector.hpp:98
typename collection_type::pointer pointer
Definition: ResourceLimitedVector.hpp:66
const_reference at(size_type pos) const
Definition: ResourceLimitedVector.hpp:356
reference at(size_type pos)
Wrappers to other basic vector methods.
Definition: ResourceLimitedVector.hpp:350
const_iterator end() const noexcept
Definition: ResourceLimitedVector.hpp:414
const_reference back() const
Definition: ResourceLimitedVector.hpp:389
bool remove_if(UnaryPredicate pred)
Remove element.
Definition: ResourceLimitedVector.hpp:271
typename collection_type::const_iterator const_iterator
Definition: ResourceLimitedVector.hpp:73
reference front()
Definition: ResourceLimitedVector.hpp:374
_Collection collection_type
Definition: ResourceLimitedVector.hpp:63
bool ensure_capacity()
Make room for one item.
Definition: ResourceLimitedVector.hpp:533
iterator begin() noexcept
Definition: ResourceLimitedVector.hpp:394
const_iterator cend() const noexcept
Definition: ResourceLimitedVector.hpp:419
const value_type * data() const
Definition: ResourceLimitedVector.hpp:502
iterator erase(const_iterator first, const_iterator last)
Definition: ResourceLimitedVector.hpp:485
_Ty value_type
Definition: ResourceLimitedVector.hpp:64
reference operator[](size_type pos)
Definition: ResourceLimitedVector.hpp:362
_Alloc allocator_type
Definition: ResourceLimitedVector.hpp:65
pointer push_back(const value_type &val)
Add element at the end.
Definition: ResourceLimitedVector.hpp:174
const_reverse_iterator crbegin() const noexcept
Definition: ResourceLimitedVector.hpp:434
const_iterator cbegin() const noexcept
Definition: ResourceLimitedVector.hpp:404
ResourceLimitedVector(configuration_type cfg=configuration_type(), const allocator_type &alloc=allocator_type())
Construct a ResourceLimitedVector.
Definition: ResourceLimitedVector.hpp:89
size_type max_size() const noexcept
Definition: ResourceLimitedVector.hpp:469
const_reverse_iterator rend() const noexcept
Definition: ResourceLimitedVector.hpp:444
reverse_iterator rbegin() noexcept
Definition: ResourceLimitedVector.hpp:424
void clear()
Definition: ResourceLimitedVector.hpp:474
typename collection_type::size_type size_type
Definition: ResourceLimitedVector.hpp:70
iterator insert(const_iterator pos, const value_type &value)
Insert value before pos.
Definition: ResourceLimitedVector.hpp:130
typename collection_type::reference reference
Definition: ResourceLimitedVector.hpp:68
const_reverse_iterator crend() const noexcept
Definition: ResourceLimitedVector.hpp:449
reverse_iterator rend() noexcept
Definition: ResourceLimitedVector.hpp:439
void assign(InputIterator first, InputIterator last)
Assign vector content.
Definition: ResourceLimitedVector.hpp:298
collection_type collection_
Definition: ResourceLimitedVector.hpp:524
size_type capacity() const noexcept
Definition: ResourceLimitedVector.hpp:464
pointer emplace_back(Args &&... args)
Construct and insert element at the end.
Definition: ResourceLimitedVector.hpp:217
value_type * data()
Definition: ResourceLimitedVector.hpp:497
bool remove(const value_type &val)
Remove element.
Definition: ResourceLimitedVector.hpp:243
typename collection_type::const_reference const_reference
Definition: ResourceLimitedVector.hpp:69
reference back()
Definition: ResourceLimitedVector.hpp:384
iterator end() noexcept
Definition: ResourceLimitedVector.hpp:409
const_reverse_iterator rbegin() const noexcept
Definition: ResourceLimitedVector.hpp:429
ResourceLimitedVector & operator=(const ResourceLimitedVector &other)
Definition: ResourceLimitedVector.hpp:109
eProsima namespace.
Definition: LibrarySettingsAttributes.h:23