123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545 |
- #ifndef FASTRTPS_UTILS_COLLECTIONS_RESOURCELIMITEDVECTOR_HPP_
- #define FASTRTPS_UTILS_COLLECTIONS_RESOURCELIMITEDVECTOR_HPP_
- #include "ResourceLimitedContainerConfig.hpp"
- #include <assert.h>
- #include <algorithm>
- #include <type_traits>
- #include <vector>
- namespace eprosima {
- namespace fastrtps {
- template <
- typename _Ty,
- typename _KeepOrderEnabler = std::false_type,
- typename _LimitsConfig = ResourceLimitedContainerConfig,
- typename _Alloc = std::allocator<_Ty>,
- typename _Collection = std::vector<_Ty, _Alloc> >
- class ResourceLimitedVector
- {
- public:
- using configuration_type = _LimitsConfig;
- using collection_type = _Collection;
- using value_type = _Ty;
- using allocator_type = _Alloc;
- using pointer = typename collection_type::pointer;
- using const_pointer = typename collection_type::const_pointer;
- using reference = typename collection_type::reference;
- using const_reference = typename collection_type::const_reference;
- using size_type = typename collection_type::size_type;
- using difference_type = typename collection_type::difference_type;
- using iterator = typename collection_type::iterator;
- using const_iterator = typename collection_type::const_iterator;
- using reverse_iterator = typename collection_type::reverse_iterator;
- using const_reverse_iterator = typename collection_type::const_reverse_iterator;
-
- ResourceLimitedVector(
- configuration_type cfg = configuration_type(),
- const allocator_type& alloc = allocator_type())
- : configuration_(cfg)
- , collection_(alloc)
- {
- collection_.reserve(cfg.initial);
- }
- ResourceLimitedVector(
- const ResourceLimitedVector& other)
- : configuration_(other.configuration_)
- , collection_(other.collection_.get_allocator())
- {
- collection_.reserve(other.collection_.capacity());
- collection_.assign(other.collection_.begin(), other.collection_.end());
- }
- virtual ~ResourceLimitedVector () = default;
- ResourceLimitedVector& operator = (
- const ResourceLimitedVector& other)
- {
- clear();
- for (const_reference item : other)
- {
- push_back(item);
- }
- assert(size() == other.size());
- return *this;
- }
-
- pointer push_back(
- const value_type& val)
- {
- return emplace_back(val);
- }
-
- pointer push_back(
- value_type&& val)
- {
- return emplace_back(std::move(val));
- }
-
- template<typename ... Args>
- pointer emplace_back(
- Args&& ... args)
- {
- if (!ensure_capacity())
- {
-
- return nullptr;
- }
-
- collection_.emplace_back(args ...);
-
- return &collection_.back();
- }
-
- bool remove(
- const value_type& val)
- {
- iterator it = std::find(collection_.begin(), collection_.end(), val);
- if (it != collection_.end())
- {
- do_remove(it);
- return true;
- }
- return false;
- }
-
- template<class UnaryPredicate>
- bool remove_if(
- UnaryPredicate pred)
- {
- iterator it = std::find_if(collection_.begin(), collection_.end(), pred);
- if (it != collection_.end())
- {
- do_remove(it);
- return true;
- }
- return false;
- }
-
- template <class InputIterator>
- void assign(
- InputIterator first,
- InputIterator last)
- {
- size_type n = std::distance(first, last);
- n = std::min(n, configuration_.maximum);
- collection_.assign(first, first + n);
- }
-
- void assign(
- size_type n,
- const value_type& val)
- {
- n = std::min(n, configuration_.maximum);
- collection_.assign(n, val);
- }
-
- void assign(
- std::initializer_list<value_type> il)
- {
- size_type n = std::min(il.size(), configuration_.maximum);
- collection_.assign(il.begin(), il.begin() + n);
- }
-
-
- reference at(
- size_type pos)
- {
- return collection_.at(pos);
- }
- const_reference at(
- size_type pos) const
- {
- return collection_.at(pos);
- }
- reference operator [](
- size_type pos)
- {
- return collection_[pos];
- }
- const_reference operator [](
- size_type pos) const
- {
- return collection_[pos];
- }
- reference front()
- {
- return collection_.front();
- }
- const_reference front() const
- {
- return collection_.front();
- }
- reference back()
- {
- return collection_.back();
- }
- const_reference back() const
- {
- return collection_.back();
- }
- iterator begin() noexcept
- {
- return collection_.begin();
- }
- const_iterator begin() const noexcept
- {
- return collection_.begin();
- }
- const_iterator cbegin() const noexcept
- {
- return collection_.cbegin();
- }
- iterator end() noexcept
- {
- return collection_.end();
- }
- const_iterator end() const noexcept
- {
- return collection_.end();
- }
- const_iterator cend() const noexcept
- {
- return collection_.cend();
- }
- reverse_iterator rbegin() noexcept
- {
- return collection_.rbegin();
- }
- const_reverse_iterator rbegin() const noexcept
- {
- return collection_.rbegin();
- }
- const_reverse_iterator crbegin() const noexcept
- {
- return collection_.crbegin();
- }
- reverse_iterator rend() noexcept
- {
- return collection_.rend();
- }
- const_reverse_iterator rend() const noexcept
- {
- return collection_.rend();
- }
- const_reverse_iterator crend() const noexcept
- {
- return collection_.crend();
- }
- bool empty() const noexcept
- {
- return collection_.empty();
- }
- size_type size() const noexcept
- {
- return collection_.size();
- }
- size_type capacity() const noexcept
- {
- return collection_.capacity();
- }
- size_type max_size() const noexcept
- {
- return std::min(configuration_.maximum, collection_.max_size());
- }
- void clear()
- {
- collection_.clear();
- }
- iterator erase(
- const_iterator pos)
- {
- return collection_.erase(pos);
- }
- iterator erase(
- const_iterator first,
- const_iterator last)
- {
- return collection_.erase(first, last);
- }
- void pop_back()
- {
- collection_.pop_back();
- }
- value_type* data()
- {
- return collection_.data();
- }
- const value_type* data() const
- {
- return collection_.data();
- }
-
-
- operator const collection_type& () const noexcept { return collection_; }
- protected:
- configuration_type configuration_;
- collection_type collection_;
-
- bool ensure_capacity()
- {
- size_type size = collection_.size();
- size_type cap = collection_.capacity();
- if (size == cap)
- {
-
- if (cap < configuration_.maximum)
- {
-
- assert(configuration_.increment > 0);
- cap += configuration_.increment;
- cap = std::min(cap, configuration_.maximum);
- collection_.reserve(cap);
- }
- else
- {
- return false;
- }
- }
- return true;
- }
-
- template <typename Enabler = _KeepOrderEnabler>
- typename std::enable_if<!Enabler::value, void>::type do_remove(
- iterator it)
- {
-
- if (it != --collection_.end())
- {
- *it = std::move(collection_.back());
- }
-
- collection_.pop_back();
- }
-
- template <typename Enabler = _KeepOrderEnabler>
- typename std::enable_if<Enabler::value, void>::type do_remove(
- iterator it)
- {
- collection_.erase(it);
- }
- };
- }
- }
- #endif
|