1 /* 2 * Copyright (c) 2021, 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 pointer wrapper. 32 */ 33 34 #ifndef PTR_WRAPPER_HPP_ 35 #define PTR_WRAPPER_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #include <stdbool.h> 40 #include <stdint.h> 41 42 namespace ot { 43 44 /** 45 * Represents a wrapper over a pointer. 46 * 47 * This is intended as base class of `OwnedPtr` or `RetainPtr` providing common simple methods. 48 * 49 * @tparam Type The pointer type. 50 */ 51 template <class Type> class Ptr 52 { 53 public: 54 /** 55 * This is the default constructor for `Ptr` initializing it as null. 56 */ Ptr(void)57 Ptr(void) 58 : mPointer(nullptr) 59 { 60 } 61 62 /** 63 * Initializes the `Ptr` with a given pointer. 64 * 65 * @param[in] aPointer A pointer to initialize with. 66 */ Ptr(Type * aPointer)67 explicit Ptr(Type *aPointer) 68 : mPointer(aPointer) 69 { 70 } 71 72 /** 73 * Indicates whether the `Ptr` is null or not. 74 * 75 * @retval TRUE The `Ptr` is null. 76 * @retval FALSE The `Ptr` is not null. 77 */ IsNull(void) const78 bool IsNull(void) const { return (mPointer == nullptr); } 79 80 /** 81 * Gets the wrapped pointer. 82 * 83 * @returns The wrapped pointer. 84 */ Get(void)85 Type *Get(void) { return mPointer; } 86 87 /** 88 * Gets the wrapped pointer. 89 * 90 * @returns The wrapped pointer. 91 */ Get(void) const92 const Type *Get(void) const { return mPointer; } 93 94 /** 95 * Overloads the `->` dereference operator and returns the pointer. 96 * 97 * @returns The wrapped pointer. 98 */ operator ->(void)99 Type *operator->(void) { return mPointer; } 100 101 /** 102 * Overloads the `->` dereference operator and returns the pointer. 103 * 104 * @returns The wrapped pointer. 105 */ operator ->(void) const106 const Type *operator->(void) const { return mPointer; } 107 108 /** 109 * Overloads the `*` dereference operator and returns a reference to the pointed object. 110 * 111 * The behavior is undefined if `IsNull() == true`. 112 * 113 * @returns A reference to the pointed object. 114 */ operator *(void)115 Type &operator*(void) { return *mPointer; } 116 117 /** 118 * Overloads the `*` dereference operator and returns a reference to the pointed object. 119 * 120 * The behavior is undefined if `IsNull() == true`. 121 * 122 * @returns A reference to the pointed object. 123 */ operator *(void) const124 const Type &operator*(void) const { return *mPointer; } 125 126 /** 127 * Overloads the operator `==` to compare the `Ptr` with a given pointer. 128 * 129 * @param[in] aPointer The pointer to compare with. 130 * 131 * @retval TRUE If `Ptr` is equal to @p aPointer. 132 * @retval FALSE If `Ptr` is not equal to @p aPointer. 133 */ operator ==(const Type * aPointer) const134 bool operator==(const Type *aPointer) const { return (mPointer == aPointer); } 135 136 /** 137 * Overloads the operator `!=` to compare the `Ptr` with a given pointer. 138 * 139 * @param[in] aPointer The pointer to compare with. 140 * 141 * @retval TRUE If `Ptr` is not equal to @p aPointer. 142 * @retval FALSE If `Ptr` is equal to @p aPointer. 143 */ operator !=(const Type * aPointer) const144 bool operator!=(const Type *aPointer) const { return (mPointer != aPointer); } 145 146 /** 147 * Overloads the operator `==` to compare the `Ptr` with another `Ptr`. 148 * 149 * @param[in] aOther The other `Ptr` to compare with. 150 * 151 * @retval TRUE If `Ptr` is equal to @p aOther. 152 * @retval FALSE If `Ptr` is not equal to @p aOther. 153 */ operator ==(const Ptr & aOther) const154 bool operator==(const Ptr &aOther) const { return (mPointer == aOther.mPointer); } 155 156 /** 157 * Overloads the operator `!=` to compare the `Ptr` with another `Ptr`. 158 * 159 * @param[in] aOther The other `Ptr` to compare with. 160 * 161 * @retval TRUE If `Ptr` is not equal to @p aOther. 162 * @retval FALSE If `Ptr` is equal to @p aOther. 163 */ operator !=(const Ptr & aOther) const164 bool operator!=(const Ptr &aOther) const { return (mPointer != aOther.mPointer); } 165 166 protected: 167 Type *mPointer; 168 }; 169 170 } // namespace ot 171 172 #endif // PTR_WRAPPER_HPP_ 173