Direct-BT
2.3.1
Direct-BT - Direct Bluetooth Programming.
|
Go to the documentation of this file.
25 #ifndef JAU_COW_ITERATOR_HPP_
26 #define JAU_COW_ITERATOR_HPP_
33 #include <type_traits>
43 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
46 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
82 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
85 template<
typename,
typename,
typename,
bool,
bool,
bool>
friend class cow_darray;
97 typedef std::iterator_traits<iterator_type> sub_traits_t;
100 std::unique_lock<std::recursive_mutex> lock_;
105 : cow_parent_(cow_parent), lock_(cow_parent_.get_write_mutex()), store_ref_(store),
109 : cow_parent_(cow_parent), lock_(cow_parent_.get_write_mutex()),
110 store_ref_(cow_parent.copy_store()), iterator_(store_ref_->
begin()) { }
115 typedef typename storage_t::size_type
size_type;
120 typedef typename sub_traits_t::value_type
value_type;
122 typedef typename sub_traits_t::pointer
pointer;
124 #if __cplusplus > 201703L && __cpp_lib_concepts
125 using iterator_concept = std::__detail::__iter_concept<_Iterator>;
153 if(
nullptr != store_ref_ ) {
154 cow_parent_.set_store(std::move(store_ref_));
156 lock_ = std::unique_lock<std::recursive_mutex>();
157 store_ref_ =
nullptr;
165 constexpr cow_rw_iterator(
const cow_rw_iterator& o) noexcept
166 : cow_parent_(o.cow_parent_), lock_(cow_parent_.get_write_mutex()),
167 store_ref_(o.store_ref_), iterator_(o.iterator_) { }
180 cow_parent_ = o.cow_parent_;
181 lock_ = std::unique_lock<std::recursive_mutex>( cow_parent_.get_write_mutex() );
182 store_ref_ = o.store_ref_;
183 iterator_ = o.iterator_;
192 constexpr cow_rw_iterator(cow_rw_iterator && o) noexcept
193 : cow_parent_( o.cow_parent_ ), lock_( std::move( o.lock_ ) ),
194 store_ref_( std::move( o.store_ref_ ) ),
195 iterator_( std::move(o.iterator_ ) ) {
210 cow_parent_ = o.cow_parent_;
211 lock_ = std::move(o.lock_);
212 store_ref_ = std::move(o.store_ref_);
213 iterator_ = std::move(o.iterator_);
222 void swap(cow_rw_iterator& o) noexcept {
270 constexpr
bool empty() const noexcept {
return store_ref_->empty(); }
276 constexpr
bool capacity_reached() const noexcept {
return store_ref_->capacity_reached(); }
287 constexpr
size_type size() const noexcept {
return store_ref_->size(); }
304 constexpr
bool is_end() const noexcept {
return iterator_ == store_ref_->end(); }
310 { iterator_ = store_ref_->
end();
return *
this; }
320 constexpr
bool is_begin() const noexcept {
return iterator_ == store_ref_->begin(); }
326 { iterator_ = store_ref_->
begin();
return *
this; }
346 return store_ref_ == rhs.store_ref_ && iterator_ == rhs.iterator_ ? 0
347 : ( iterator_ < rhs.iterator_ ? -1 : 1);
385 return &(*iterator_);
399 return &(*iterator_);
428 {
return iterator_[i]; }
439 { iterator_ += i;
return *
this; }
447 { iterator_ -= i;
return *
this; }
457 {
return iterator_ - rhs.iterator_; }
476 constexpr
void pop_back() noexcept {
477 store_ref_->pop_back();
478 iterator_ = store_ref_->end();
487 constexpr
void erase () {
488 iterator_ = store_ref_->erase(iterator_);
498 iterator_ = store_ref_->erase(iterator_, iterator_+count);
512 iterator_ = store_ref_->insert(iterator_, x);
526 iterator_ = store_ref_->insert(iterator_, std::move(x));
543 template<
typename... Args>
544 constexpr
void emplace(Args&&... args) {
545 iterator_ = store_ref_->emplace(iterator_, std::forward<Args>(args)... );
557 template<
class InputIt >
558 constexpr
void insert( InputIt first, InputIt last ) {
559 iterator_ = store_ref_->insert(iterator_, first, last);
570 store_ref_->push_back(x);
571 iterator_ = store_ref_->end();
582 store_ref_->push_back(std::move(x));
583 iterator_ = store_ref_->end();
599 template<
typename... Args>
601 reference res = store_ref_->emplace_back(std::forward<Args>(args)...);
602 iterator_ = store_ref_->end();
615 template<
class InputIt >
616 constexpr
void push_back( InputIt first, InputIt last ) {
617 store_ref_->push_back(first, last);
618 iterator_ = store_ref_->end();
649 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
650 class cow_ro_iterator {
651 friend cow_rw_iterator<Storage_type, Storage_ref_type, CoW_container>;
652 template<
typename,
typename,
typename,
bool,
bool,
bool>
friend class cow_darray;
664 typedef std::iterator_traits<iterator_type> sub_traits_t;
670 : store_ref_(store), iterator_(it) { }
682 typedef typename sub_traits_t::pointer
pointer;
684 #if __cplusplus > 201703L && __cpp_lib_concepts
685 using iterator_concept = std::__detail::__iter_concept<_Iterator>;
690 : store_ref_(
nullptr), iterator_() { }
694 : store_ref_(o.store_ref_), iterator_(o.iterator_) {}
699 store_ref_ = o.store_ref_;
700 iterator_ = o.iterator_;
707 : store_ref_(std::move(o.store_ref_)), iterator_(std::move(o.iterator_)) {
714 store_ref_ = std::move(o.store_ref_);
715 iterator_ = std::move(o.iterator_);
754 constexpr
bool empty() const noexcept {
return store_ref_->empty(); }
760 constexpr
bool capacity_reached() const noexcept {
return store_ref_->capacity_reached(); }
788 constexpr
bool is_end() const noexcept {
return iterator_ == store_ref_->cend(); }
794 { iterator_ = store_ref_->
cend();
return *
this; }
804 constexpr
bool is_begin() const noexcept {
return iterator_ == store_ref_->cbegin(); }
810 { iterator_ = store_ref_->
cbegin();
return *
this; }
833 return store_ref_ == rhs.store_ref_ && iterator_ == rhs.iterator_ ? 0
834 : ( iterator_ < rhs.iterator_ ? -1 : 1);
838 return store_ref_ == rhs.store_ref_ && iterator_ == rhs.iterator_ ? 0
839 : ( iterator_ < rhs.iterator_ ? -1 : 1);
869 return &(*iterator_);
898 {
return iterator_[i]; }
902 { iterator_ += i;
return *
this; }
910 { iterator_ -= i;
return *
this; }
920 {
return iterator_ - rhs.iterator_; }
923 {
return iterator_ - rhs.iterator_; }
943 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
949 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
958 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
961 {
return lhs.compare(rhs) == 0; }
963 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
966 {
return lhs.compare(rhs) != 0; }
968 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
971 {
return rhs.compare(lhs) == 0; }
973 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
976 {
return rhs.compare(lhs) != 0; }
978 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
981 {
return lhs.compare(rhs) <= 0; }
983 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
986 {
return rhs.compare(lhs) > 0; }
988 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
991 {
return lhs.compare(rhs) < 0; }
993 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
996 {
return rhs.compare(lhs) >= 0; }
998 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
1001 {
return lhs.compare(rhs) >= 0; }
1003 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
1006 {
return rhs.compare(lhs) < 0; }
1008 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
1011 {
return lhs.compare(rhs) > 0; }
1013 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
1016 {
return rhs.compare(lhs) <= 0; }
1018 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
1019 constexpr
typename Storage_type::difference_type
operator-
1022 {
return lhs.distance(rhs); }
1024 template <
typename Storage_type,
typename Storage_ref_type,
typename CoW_container>
1025 constexpr
typename Storage_type::difference_type
operator-
1028 {
return rhs.distance(lhs) * -1; }
1038 template<
class,
class =
void >
1047 struct is_cow_type<T, std::void_t<typename T::cow_container_t>> : std::true_type { };
#define constexpr_cxx20
constexpr qualifier replacement for C++20 constexpr.
constexpr cow_rw_iterator end() const noexcept
Returns a new iterator pointing to the element following the last element, aka end.
constexpr bool empty() const noexcept
Returns true if storage is empty().
constexpr cow_ro_iterator operator++(int) noexcept
Post-increment; Try to avoid: Low performance due to returning copy-ctor.
constexpr cow_ro_iterator(cow_ro_iterator &&o) noexcept
constexpr cow_ro_iterator() noexcept
constexpr cow_ro_iterator & operator=(cow_ro_iterator &&o) noexcept
constexpr cow_ro_iterator & operator--() noexcept
Pre-decrement; Well performing, return *this.
Implementation of a Copy-On-Write (CoW) read-onlu iterator over immutable value_type storage.
constexpr cow_rw_iterator begin() const noexcept
Returns a new iterator pointing to the first element, aka begin.
constexpr bool capacity_reached() const noexcept
Returns true if storage capacity has been reached and the next push_back() will grow the storage and ...
constexpr bool is_begin() const noexcept
Returns true, if this iterator points to begin().
std::ostream & operator<<(std::ostream &out, const cow_darray< Value_type, Alloc_type > &c)
constexpr cow_ro_iterator & operator+=(difference_type i) noexcept
Addition-assignment of 'element_count'; Well performing, return *this.
void write_back() noexcept
Replace the parent's current store with this iterators' instance, unlock the CoW parents' write lock ...
constexpr_cxx20 std::string get_info() const noexcept
constexpr int compare(const cow_rw_iterator< storage_t, storage_ref_t, cow_container_t > &rhs) const noexcept
constexpr cow_ro_iterator(const cow_ro_iterator &o) noexcept
constexpr cow_ro_iterator< Storage_type, Storage_ref_type, CoW_container > immutable() const noexcept
Returns a new const_iterator pointing to the current position.
constexpr storage_t & storage() const noexcept
Returns this instances' underlying shared storage by reference.
void swap(cow_ro_iterator &o) noexcept
sub_traits_t::value_type value_type
constexpr difference_type dist_begin() const noexcept
Returns the distance to_begin() using zero as first index.
sub_traits_t::pointer pointer
constexpr cow_ro_iterator & operator=(const cow_ro_iterator &o) noexcept
sub_traits_t::reference reference
constexpr bool operator!=(const cow_rw_iterator &rhs) const noexcept
storage_t::const_iterator iterator_type
Actual const iterator type of the contained native iterator, probably a simple pointer.
constexpr bool operator!=(const cow_ro_iterator &rhs) const noexcept
template< class T > is_cow_type<T>::value compile-time Type Trait, determining whether the given temp...
constexpr void insert(const value_type &x)
Inserts the element before the current position and moves all elements from there to the right before...
storage_t::difference_type difference_type
PRAGMA_DISABLE_WARNING_POP constexpr_cxx20 std::string to_string(const endian &v) noexcept
Return std::string representation of the given jau::endian.
constexpr void emplace(Args &&... args)
Like std::vector::emplace(), construct a new element in place.
constexpr bool operator<=(const cow_ro_iterator &rhs) const noexcept
storage_t::size_type size_type
constexpr bool is_begin() const noexcept
Returns true, if this iterator points to cbegin().
constexpr storage_t & storage() const noexcept
Returns this instances' underlying shared storage by reference.
constexpr cow_ro_iterator cend() const noexcept
Returns a new const_iterator pointing to the element following the last element, aka end.
constexpr cow_rw_iterator & operator+=(difference_type i) noexcept
Addition-assignment of 'element_count'; Well performing, return *this.
constexpr cow_rw_iterator & operator++() noexcept
Pre-increment; Well performing, return *this.
constexpr difference_type dist_begin() const noexcept
Returns the distance to_begin() using zero as first index.
constexpr cow_rw_iterator & operator-=(difference_type i) noexcept
Subtraction-assignment of 'element_count'; Well performing, return *this.
constexpr bool operator==(const cow_rw_iterator &rhs) const noexcept
constexpr difference_type operator-(const cow_ro_iterator &rhs) const noexcept
Binary 'iterator - iterator -> element_count'; Well performing, return element_count of type differen...
constexpr iterator_type base() const noexcept
Returns a copy of the underlying storage iterator.
constexpr reference emplace_back(Args &&... args)
Like std::vector::emplace_back(), construct a new element in place at the end().
constexpr cow_rw_iterator operator+(difference_type rhs) const noexcept
Binary 'iterator + element_count'; Try to avoid: Low performance due to returning copy-ctor.
constexpr bool operator>=(const cow_ro_iterator &rhs) const noexcept
constexpr_cxx20 std::string get_info() const noexcept
void swap(cow_darray< Value_type, Alloc_type > &rhs, cow_darray< Value_type, Alloc_type > &lhs) noexcept
constexpr difference_type dist_end() const noexcept
Returns the distance to_end() using zero as first index.
constexpr cow_rw_iterator & to_end() noexcept
This iterator is set to the last element, end().
constexpr const reference operator*() const noexcept
constexpr bool operator<=(const cow_rw_iterator &rhs) const noexcept
constexpr size_type size() const noexcept
Return the size of the underlying value_type store.
sub_traits_t::pointer pointer
constexpr difference_type distance(const cow_rw_iterator< storage_t, storage_ref_t, cow_container_t > &rhs) const noexcept
constexpr cow_rw_iterator & operator--() noexcept
Pre-decrement; Well performing, return *this.
constexpr const reference operator[](difference_type i) const noexcept
Subscript of 'element_index', returning immutable Value_type reference.
Storage_ref_type storage_ref_t
constexpr_cxx20 std::string toString() const noexcept
Storage_ref_type storage_ref_t
Implementation of a Copy-On-Write (CoW) using jau::darray as the underlying storage,...
sub_traits_t::iterator_category iterator_category
constexpr bool operator<(const cow_ro_iterator &rhs) const noexcept
void swap(cow_rw_iterator &o) noexcept
C++ named requirements: LegacyIterator: Swappable.
sub_traits_t::iterator_category iterator_category
bool operator<=(const cow_darray< Value_type, Alloc_type > &rhs, const cow_darray< Value_type, Alloc_type > &lhs)
constexpr const pointer operator->() const noexcept
Pointer to member access.
constexpr cow_ro_iterator & operator-=(difference_type i) noexcept
Subtraction-assignment of 'element_count'; Well performing, return *this.
constexpr bool operator<(const cow_rw_iterator &rhs) const noexcept
constexpr bool empty() const noexcept
Returns true if storage is empty().
constexpr bool operator==(const cow_ro_iterator &rhs) const noexcept
constexpr const reference operator[](difference_type i) const noexcept
Subscript of 'element_index', returning immutable Value_type reference.
constexpr bool operator>(const cow_rw_iterator &rhs) const noexcept
constexpr bool is_end() const noexcept
Returns true, if this iterator points to end().
constexpr bool capacity_reached() const noexcept
Returns true if storage capacity has been reached and the next push_back() will grow the storage and ...
bool operator>(const cow_darray< Value_type, Alloc_type > &rhs, const cow_darray< Value_type, Alloc_type > &lhs)
constexpr cow_rw_iterator & to_begin() noexcept
This iterator is set to the first element, begin().
constexpr void push_back(const value_type &x)
Like std::vector::push_back(), copy.
constexpr cow_rw_iterator & operator=(const cow_rw_iterator &o) noexcept
Assigns content of other mutable iterator to this one, if they are not identical.
std::string to_hexstring(value_type const &v) noexcept
Produce a lower-case hexadecimal string representation of the given pointer.
sub_traits_t::reference reference
constexpr const pointer operator->() const noexcept
storage_t::size_type size_type
CoW_container cow_container_t
constexpr iterator_type base() const noexcept
Returns a copy of the underlying storage const_iterator.
constexpr cow_ro_iterator operator--(int) noexcept
Post-decrement; Try to avoid: Low performance due to returning copy-ctor.
constexpr size_type size() const noexcept
Return the size of the underlying value_type store.
bool operator!=(const callocator< T1 > &lhs, const callocator< T2 > &rhs) noexcept
Implementation of a Copy-On-Write (CoW) read-write iterator over mutable value_type storage.
constexpr bool is_end() const noexcept
Returns true, if this iterator points to cend().
constexpr_cxx20 std::string toString() const noexcept
CoW_container cow_container_t
constexpr cow_ro_iterator & to_begin() noexcept
This iterator is set to the first element, cbegin().
constexpr bool operator>=(const cow_rw_iterator &rhs) const noexcept
constexpr bool operator>(const cow_ro_iterator &rhs) const noexcept
constexpr int compare(const cow_ro_iterator &rhs) const noexcept
Returns signum or three-way comparison value.
sub_traits_t::value_type value_type
constexpr cow_rw_iterator operator-(difference_type rhs) const noexcept
Binary 'iterator - element_count'; Try to avoid: Low performance due to returning copy-ctor.
bool operator>=(const cow_darray< Value_type, Alloc_type > &rhs, const cow_darray< Value_type, Alloc_type > &lhs)
constexpr void erase()
Erases the element at the current position.
constexpr cow_ro_iterator & operator++() noexcept
Pre-increment; Well performing, return *this.
Implementation of a Copy-On-Write (CoW) using std::vector as the underlying storage,...
storage_t::difference_type difference_type
constexpr cow_ro_iterator & to_end() noexcept
This iterator is set to the last element, cend().
constexpr cow_ro_iterator cbegin() const noexcept
Returns a new const_iterator pointing to the first element, aka begin.
constexpr cow_ro_iterator operator-(difference_type rhs) const noexcept
Binary 'iterator - element_count'; Try to avoid: Low performance due to returning copy-ctor.
bool operator==(const callocator< T1 > &lhs, const callocator< T2 > &rhs) noexcept
constexpr cow_ro_iterator operator+(difference_type rhs) const noexcept
Binary 'iterator + element_count'; Try to avoid: Low performance due to returning copy-ctor.
constexpr int compare(const cow_rw_iterator &rhs) const noexcept
Returns signum or three-way comparison value.
bool operator<(const cow_darray< Value_type, Alloc_type > &rhs, const cow_darray< Value_type, Alloc_type > &lhs)
constexpr const reference operator*() const noexcept
Dereferencing iterator to value_type reference.
constexpr void pop_back() noexcept
Removes the last element and sets this iterator to end()
constexpr difference_type dist_end() const noexcept
Returns the distance to_end() using zero as first index.
storage_t::iterator iterator_type
Actual iterator type of the contained native iterator, probably a simple pointer.