Fast RTPS  Version 2.1.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 
30 namespace eprosima {
31 namespace fastrtps {
32 
52 template <
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 {
60 public:
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 
133  const value_type& val)
134  {
135  return emplace_back(val);
136  }
137 
149  value_type&& val)
150  {
151  return emplace_back(std::move(val));
152  }
153 
164  template<typename ... Args>
166  Args&& ... args)
167  {
168  if (!ensure_capacity())
169  {
170  // Indicate error by returning null pointer
171  return nullptr;
172  }
173 
174  // Construct new element at the end of the collection
175  collection_.emplace_back(args ...);
176 
177  // Return pointer to newly created element
178  return &collection_.back();
179  }
180 
191  bool remove(
192  const value_type& val)
193  {
194  iterator it = std::find(collection_.begin(), collection_.end(), val);
195  if (it != collection_.end())
196  {
197  do_remove(it);
198  return true;
199  }
200  return false;
201  }
202 
218  template<class UnaryPredicate>
219  bool remove_if(
220  UnaryPredicate pred)
221  {
222  iterator it = std::find_if(collection_.begin(), collection_.end(), pred);
223  if (it != collection_.end())
224  {
225  do_remove(it);
226  return true;
227  }
228  return false;
229  }
230 
245  template <class InputIterator>
246  void assign(
247  InputIterator first,
248  InputIterator last)
249  {
250  size_type n = static_cast<size_type>(std::distance(first, last));
251  n = std::min(n, configuration_.maximum);
252  InputIterator value = first;
253  std::advance(value, n);
254  collection_.assign(first, value);
255  }
256 
267  void assign(
268  size_type n,
269  const value_type& val)
270  {
271  n = std::min(n, configuration_.maximum);
272  collection_.assign(n, val);
273  }
274 
286  void assign(
287  std::initializer_list<value_type> il)
288  {
289  size_type n = std::min(il.size(), configuration_.maximum);
290  collection_.assign(il.begin(), il.begin() + n);
291  }
292 
299  size_type pos)
300  {
301  return collection_.at(pos);
302  }
303 
305  size_type pos) const
306  {
307  return collection_.at(pos);
308  }
309 
311  size_type pos)
312  {
313  return collection_[pos];
314  }
315 
317  size_type pos) const
318  {
319  return collection_[pos];
320  }
321 
323  {
324  return collection_.front();
325  }
326 
328  {
329  return collection_.front();
330  }
331 
333  {
334  return collection_.back();
335  }
336 
338  {
339  return collection_.back();
340  }
341 
342  iterator begin() noexcept
343  {
344  return collection_.begin();
345  }
346 
347  const_iterator begin() const noexcept
348  {
349  return collection_.begin();
350  }
351 
352  const_iterator cbegin() const noexcept
353  {
354  return collection_.cbegin();
355  }
356 
357  iterator end() noexcept
358  {
359  return collection_.end();
360  }
361 
362  const_iterator end() const noexcept
363  {
364  return collection_.end();
365  }
366 
367  const_iterator cend() const noexcept
368  {
369  return collection_.cend();
370  }
371 
373  {
374  return collection_.rbegin();
375  }
376 
378  {
379  return collection_.rbegin();
380  }
381 
383  {
384  return collection_.crbegin();
385  }
386 
388  {
389  return collection_.rend();
390  }
391 
392  const_reverse_iterator rend() const noexcept
393  {
394  return collection_.rend();
395  }
396 
397  const_reverse_iterator crend() const noexcept
398  {
399  return collection_.crend();
400  }
401 
402  bool empty() const noexcept
403  {
404  return collection_.empty();
405  }
406 
407  size_type size() const noexcept
408  {
409  return collection_.size();
410  }
411 
412  size_type capacity() const noexcept
413  {
414  return collection_.capacity();
415  }
416 
417  size_type max_size() const noexcept
418  {
419  return std::min(configuration_.maximum, collection_.max_size());
420  }
421 
422  void clear()
423  {
424  collection_.clear();
425  }
426 
428  const_iterator pos)
429  {
430  return collection_.erase(pos);
431  }
432 
434  const_iterator first,
435  const_iterator last)
436  {
437  return collection_.erase(first, last);
438  }
439 
440  void pop_back()
441  {
442  collection_.pop_back();
443  }
444 
446  {
447  return collection_.data();
448  }
449 
450  const value_type* data() const
451  {
452  return collection_.data();
453  }
454 
456 
464  operator const collection_type& () const noexcept
465  {
466  return collection_;
467  }
468 
469 protected:
470 
473 
482  {
483  size_type size = collection_.size();
484  size_type cap = collection_.capacity();
485  if (size == cap)
486  {
487  // collection is full, check resource limit
488  if (cap < configuration_.maximum)
489  {
490  // increase collection capacity
491  assert(configuration_.increment > 0);
492  cap += configuration_.increment;
493  cap = std::min(cap, configuration_.maximum);
494  collection_.reserve(cap);
495  }
496  else
497  {
498  return false;
499  }
500  }
501 
502  return true;
503  }
504 
514  template <typename Enabler = _KeepOrderEnabler>
515  typename std::enable_if<!Enabler::value, void>::type do_remove(
516  iterator it)
517  {
518  // Copy last element into the element being removed
519  if (it != --collection_.end())
520  {
521  *it = std::move(collection_.back());
522  }
523 
524  // Then drop last element
525  collection_.pop_back();
526  }
527 
538  template <typename Enabler = _KeepOrderEnabler>
539  typename std::enable_if<Enabler::value, void>::type do_remove(
540  iterator it)
541  {
542  collection_.erase(it);
543  }
544 
545 };
546 
547 } // namespace fastrtps
548 } // namespace eprosima
549 
550 #endif /* FASTRTPS_UTILS_COLLECTIONS_RESOURCELIMITEDVECTOR_HPP_ */
Resource limited wrapper of std::vector.
Definition: ResourceLimitedVector.hpp:59
void pop_back()
Definition: ResourceLimitedVector.hpp:440
size_type size() const noexcept
Definition: ResourceLimitedVector.hpp:407
configuration_type configuration_
Definition: ResourceLimitedVector.hpp:471
const_reference front() const
Definition: ResourceLimitedVector.hpp:327
typename collection_type::iterator iterator
Definition: ResourceLimitedVector.hpp:72
pointer push_back(value_type &&val)
Add element at the end.
Definition: ResourceLimitedVector.hpp:148
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:286
typename collection_type::reverse_iterator reverse_iterator
Definition: ResourceLimitedVector.hpp:74
void assign(size_type n, const value_type &val)
Assign vector content.
Definition: ResourceLimitedVector.hpp:267
const_iterator begin() const noexcept
Definition: ResourceLimitedVector.hpp:347
typename collection_type::const_pointer const_pointer
Definition: ResourceLimitedVector.hpp:67
iterator erase(const_iterator pos)
Definition: ResourceLimitedVector.hpp:427
ResourceLimitedVector & operator=(const ResourceLimitedVector &other)
Definition: ResourceLimitedVector.hpp:109
_LimitsConfig configuration_type
Definition: ResourceLimitedVector.hpp:62
bool empty() const noexcept
Definition: ResourceLimitedVector.hpp:402
typename collection_type::const_reverse_iterator const_reverse_iterator
Definition: ResourceLimitedVector.hpp:75
std::enable_if<!Enabler::value, void >::type do_remove(iterator it)
Remove element.
Definition: ResourceLimitedVector.hpp:515
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:304
reference at(size_type pos)
Wrappers to other basic vector methods.
Definition: ResourceLimitedVector.hpp:298
const_iterator end() const noexcept
Definition: ResourceLimitedVector.hpp:362
const_reference back() const
Definition: ResourceLimitedVector.hpp:337
std::enable_if< Enabler::value, void >::type do_remove(iterator it)
Remove element.
Definition: ResourceLimitedVector.hpp:539
bool remove_if(UnaryPredicate pred)
Remove element.
Definition: ResourceLimitedVector.hpp:219
typename collection_type::const_iterator const_iterator
Definition: ResourceLimitedVector.hpp:73
reference front()
Definition: ResourceLimitedVector.hpp:322
value_type * data()
Definition: ResourceLimitedVector.hpp:445
_Collection collection_type
Definition: ResourceLimitedVector.hpp:63
bool ensure_capacity()
Make room for one item.
Definition: ResourceLimitedVector.hpp:481
iterator begin() noexcept
Definition: ResourceLimitedVector.hpp:342
const_iterator cend() const noexcept
Definition: ResourceLimitedVector.hpp:367
const value_type * data() const
Definition: ResourceLimitedVector.hpp:450
iterator erase(const_iterator first, const_iterator last)
Definition: ResourceLimitedVector.hpp:433
_Ty value_type
Definition: ResourceLimitedVector.hpp:64
reference operator[](size_type pos)
Definition: ResourceLimitedVector.hpp:310
_Alloc allocator_type
Definition: ResourceLimitedVector.hpp:65
pointer push_back(const value_type &val)
Add element at the end.
Definition: ResourceLimitedVector.hpp:132
const_reverse_iterator crbegin() const noexcept
Definition: ResourceLimitedVector.hpp:382
const_iterator cbegin() const noexcept
Definition: ResourceLimitedVector.hpp:352
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:417
const_reverse_iterator rend() const noexcept
Definition: ResourceLimitedVector.hpp:392
reverse_iterator rbegin() noexcept
Definition: ResourceLimitedVector.hpp:372
void clear()
Definition: ResourceLimitedVector.hpp:422
typename collection_type::size_type size_type
Definition: ResourceLimitedVector.hpp:70
typename collection_type::reference reference
Definition: ResourceLimitedVector.hpp:68
const_reverse_iterator crend() const noexcept
Definition: ResourceLimitedVector.hpp:397
reverse_iterator rend() noexcept
Definition: ResourceLimitedVector.hpp:387
void assign(InputIterator first, InputIterator last)
Assign vector content.
Definition: ResourceLimitedVector.hpp:246
collection_type collection_
Definition: ResourceLimitedVector.hpp:472
size_type capacity() const noexcept
Definition: ResourceLimitedVector.hpp:412
pointer emplace_back(Args &&... args)
Construct and insert element at the end.
Definition: ResourceLimitedVector.hpp:165
bool remove(const value_type &val)
Remove element.
Definition: ResourceLimitedVector.hpp:191
typename collection_type::const_reference const_reference
Definition: ResourceLimitedVector.hpp:69
reference back()
Definition: ResourceLimitedVector.hpp:332
iterator end() noexcept
Definition: ResourceLimitedVector.hpp:357
const_reverse_iterator rbegin() const noexcept
Definition: ResourceLimitedVector.hpp:377
eProsima namespace.
Definition: LibrarySettingsAttributes.h:23