1 /* 2 * Copyright (c) 2020, The OpenThread Authors. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. Neither the name of the copyright holder nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /** 30 * @file 31 * This file includes definitions for a generic item-pointer iterator class. 32 */ 33 34 #ifndef ITERATOR_UTILS_HPP_ 35 #define ITERATOR_UTILS_HPP_ 36 37 namespace ot { 38 39 /** 40 * @addtogroup core-iterator-utils 41 * 42 * @brief 43 * This module includes definitions for OpenThread generic item-pointer iterator class. 44 * 45 * @{ 46 * 47 */ 48 49 /** 50 * This template class is used as a base class for those item-pointer iterators. 51 * 52 * These iterators have common methods and operators like `Advance()` and `++` and hold a pointer to the 53 * object. 54 * 55 * Users of this class should follow CRTP-style inheritance, i.e., `IteratorType` class itself should publicly inherit 56 * from `ItemPtrIterator<ItemType, IteratorType>`. 57 * 58 * @tparam ItemType The type of the object that the iterator points to. 59 * @tparam IteratorType The Iterator class that inherits this class. The class MUST have a method `Advance()` which 60 * moves the pointer to the next. `Advance()` SHALL NOT be called when `IsDone()` is `true` and 61 * would set the pointer to `nullptr` when there's no more elements. 62 * 63 */ 64 template <typename ItemType, typename IteratorType> class ItemPtrIterator 65 { 66 public: 67 /** 68 * This method indicates whether there are no more items to be accessed (iterator has reached the end). 69 * 70 * @retval TRUE There are no more items to be accessed (iterator has reached the end). 71 * @retval FALSE The current item is valid. 72 * 73 */ IsDone(void) const74 bool IsDone(void) const { return mItem == nullptr; } 75 76 /** 77 * This method overloads `++` operator (pre-increment) to advance the iterator. 78 * 79 * The iterator is moved to point to the next item using IteratorType's `Advance` method. 80 * If there are no more items, the iterator becomes empty (i.e., `operator*` returns `nullptr` and `IsDone()` 81 * returns `true`). 82 * 83 */ operator ++(void)84 void operator++(void) { static_cast<IteratorType *>(this)->Advance(); } 85 86 /** 87 * This method overloads `++` operator (post-increment) to advance the iterator. 88 * 89 * The iterator is moved to point to the next item using IteratorType's `Advance` method. 90 * If there are no more items, the iterator becomes empty (i.e., `operator*` returns `nullptr` and `IsDone()` 91 * returns `true`). 92 * 93 */ operator ++(int)94 void operator++(int) { static_cast<IteratorType *>(this)->Advance(); } 95 96 /** 97 * This method overloads the `*` dereference operator and gets a reference to then item to which the iterator is 98 * currently pointing. 99 * 100 * This method MUST be used when the iterator is not empty/finished (i.e., `IsDone()` returns `false`). 101 * 102 * @returns A reference to the item currently pointed by the iterator. 103 * 104 */ operator *(void)105 ItemType &operator*(void) { return *mItem; } 106 107 /** 108 * This method overloads the `->` dereference operator and gets a pointer to the item to which the iterator is 109 * currently pointing. 110 * 111 * @returns A pointer to the item associated with the iterator, or `nullptr` if iterator is empty/done. 112 * 113 */ operator ->(void)114 ItemType *operator->(void) { return mItem; } 115 116 /** 117 * This method overloads operator `==` to evaluate whether or not two `Iterator` instances point to the same 118 * item. 119 * 120 * @param[in] aOther The other `Iterator` to compare with. 121 * 122 * @retval TRUE If the two `Iterator` objects point to the same item or both are done. 123 * @retval FALSE If the two `Iterator` objects do not point to the same item. 124 * 125 */ operator ==(const IteratorType & aOther) const126 bool operator==(const IteratorType &aOther) const { return mItem == aOther.mItem; } 127 128 /** 129 * This method overloads operator `!=` to evaluate whether or not two `Iterator` instances point to the same 130 * child entry. 131 * 132 * @param[in] aOther The other `Iterator` to compare with. 133 * 134 * @retval TRUE If the two `Iterator` objects do not point to the same item. 135 * @retval FALSE If the two `Iterator` objects point to the same item or both are done. 136 * 137 */ operator !=(const IteratorType & aOther) const138 bool operator!=(const IteratorType &aOther) const { return mItem != aOther.mItem; } 139 140 protected: 141 /** 142 * Default constructor 143 * 144 */ ItemPtrIterator(void)145 ItemPtrIterator(void) 146 : mItem(nullptr) 147 { 148 } 149 150 /** 151 * Constructor with an Item pointer. 152 * 153 */ ItemPtrIterator(ItemType * item)154 explicit ItemPtrIterator(ItemType *item) 155 : mItem(item) 156 { 157 } 158 159 ItemType *mItem; 160 }; 161 162 /** 163 * @} 164 * 165 */ 166 167 } // namespace ot 168 169 #endif // ITERATOR_UTILS_HPP_ 170