Line data Source code
1 : /** 2 : Copyright (c) 2017-2022 Roman Katuntsev <sbkarr@stappler.org> 3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev> 4 : 5 : Permission is hereby granted, free of charge, to any person obtaining a copy 6 : of this software and associated documentation files (the "Software"), to deal 7 : in the Software without restriction, including without limitation the rights 8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 : copies of the Software, and to permit persons to whom the Software is 10 : furnished to do so, subject to the following conditions: 11 : 12 : The above copyright notice and this permission notice shall be included in 13 : all copies or substantial portions of the Software. 14 : 15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 : THE SOFTWARE. 22 : **/ 23 : 24 : #ifndef STAPPLER_CORE_MEMORY_SPMEMPOINTERITERATOR_H_ 25 : #define STAPPLER_CORE_MEMORY_SPMEMPOINTERITERATOR_H_ 26 : 27 : #include "SPCore.h" 28 : 29 : namespace STAPPLER_VERSIONIZED stappler::memory { 30 : 31 : template<class Type, class Pointer, class Reference> 32 : class pointer_iterator { 33 : public: 34 : using iterator_category = std::random_access_iterator_tag; 35 : using size_type = size_t; 36 : using pointer = Pointer; 37 : using reference = Reference; 38 : using iterator = pointer_iterator<Type, Pointer, Reference>; 39 : using difference_type = std::ptrdiff_t; 40 : using value_type = typename std::remove_cv<Type>::type; 41 : 42 5274 : pointer_iterator() noexcept : current(nullptr) {} 43 540093951 : pointer_iterator(const iterator & other) noexcept : current(other.current) {} 44 136809297 : explicit pointer_iterator(pointer p) noexcept : current(p) {} 45 : 46 501055 : iterator& operator=(const iterator &other) { current = other.current; return *this; } 47 392991 : bool operator==(const iterator &other) const { return current == other.current; } 48 1230310921 : bool operator!=(const iterator &other) const { return current != other.current; } 49 4825 : bool operator<(const iterator &other) const { return current < other.current; } 50 : bool operator>(const iterator &other) const { return current > other.current; } 51 : bool operator<=(const iterator &other) const { return current <= other.current; } 52 : bool operator>=(const iterator &other) const { return current >= other.current; } 53 : 54 679325816 : iterator& operator++() { ++current; return *this; } 55 499001300 : iterator operator++(int) { auto tmp = *this; ++ current; return tmp; } 56 275273 : iterator& operator--() { --current; return *this; } 57 125 : iterator operator--(int) { auto tmp = *this; --current; return tmp; } 58 964337 : iterator& operator+= (size_type n) { current += n; return *this; } 59 33521 : iterator& operator-=(size_type n) { current -= n; return *this; } 60 9003714 : difference_type operator-(const iterator &other) const { return current - other.current; } 61 : 62 1180089522 : reference operator*() const { return *current; } 63 110634 : pointer operator->() const { return current; } 64 : reference operator[](size_type n) const { return *(current + n); } 65 : 66 174555 : size_type operator-(pointer p) const { return current - p; } 67 : 68 : // const_iterator cast 69 174706 : operator pointer_iterator<value_type, const value_type *, const value_type &> () const { 70 174706 : return pointer_iterator<value_type, const value_type *, const value_type &>(current); 71 : } 72 : 73 : //operator pointer () const { return current; } 74 : 75 : friend auto operator+(size_type n, const iterator &it) -> iterator { 76 : return iterator(it.current + n); 77 : } 78 : 79 8412 : friend auto operator+(const iterator &it, size_type n) -> iterator { 80 8412 : return iterator(it.current + n); 81 : } 82 : 83 650 : friend auto operator-(const iterator &it, size_type n) -> iterator { 84 650 : return iterator(it.current - n); 85 : } 86 : 87 : protected: 88 : pointer current; 89 : }; 90 : 91 : } 92 : 93 : #endif /* STAPPLER_CORE_MEMORY_SPMEMPOINTERITERATOR_H_ */