| 42 | }; |
| 43 | |
| 44 | struct ArrayProxy { |
| 45 | using value_type = std::weak_ptr<A>; |
| 46 | |
| 47 | struct iterator { |
| 48 | using iterator_category = std::random_access_iterator_tag; |
| 49 | using difference_type = std::ptrdiff_t; |
| 50 | using value_type = std::weak_ptr<A>; |
| 51 | using pointer = std::weak_ptr<A>*; // or also value_type* |
| 52 | using reference = std::weak_ptr<A>&; // or also value_type& |
| 53 | |
| 54 | const ArrayProxy& a; |
| 55 | size_t index; |
| 56 | |
| 57 | iterator(const ArrayProxy& a_, size_t index_) : a(a_), index(index_) { |
| 58 | } |
| 59 | |
| 60 | value_type operator*() const { |
| 61 | size_t size = a.mpParent.children.size(); |
| 62 | if (index >= 0 && index < size) { |
| 63 | return a.mpParent.children[index]; |
| 64 | } |
| 65 | return std::weak_ptr<A>(); |
| 66 | } |
| 67 | |
| 68 | // Operators : arithmetic |
| 69 | inline iterator& operator++() { |
| 70 | ++index; |
| 71 | return *this; |
| 72 | } |
| 73 | inline iterator& operator--() { |
| 74 | --index; |
| 75 | return *this; |
| 76 | } |
| 77 | inline iterator& operator+=(const size_t& rhs) { |
| 78 | index += rhs; |
| 79 | return *this; |
| 80 | } |
| 81 | inline iterator& operator-=(const size_t& rhs) { |
| 82 | index -= rhs; |
| 83 | return *this; |
| 84 | } |
| 85 | |
| 86 | // Operators : comparison |
| 87 | inline bool operator==(const iterator& rhs) { |
| 88 | return index == rhs.index; |
| 89 | } |
| 90 | inline bool operator!=(const iterator& rhs) { |
| 91 | return index != rhs.index; |
| 92 | } |
| 93 | inline bool operator>(const iterator& rhs) { |
| 94 | return index > rhs.index; |
| 95 | } |
| 96 | inline bool operator<(const iterator& rhs) { |
| 97 | return index < rhs.index; |
| 98 | } |
| 99 | inline bool operator>=(const iterator& rhs) { |
| 100 | return index >= rhs.index; |
| 101 | } |