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 type traits definitions. 32 */ 33 34 #ifndef OT_TYPE_TRAITS_HPP_ 35 #define OT_TYPE_TRAITS_HPP_ 36 37 namespace ot { 38 namespace TypeTraits { 39 40 /** 41 * Represents a true value (contains a `true` static `kValue` member variable). 42 * 43 */ 44 struct TrueValue 45 { 46 constexpr static bool kValue = true; ///< true value. 47 }; 48 49 /** 50 * Represents a false value (contains a `false` static `kValue` member variable). 51 * 52 */ 53 struct FalseValue 54 { 55 constexpr static bool kValue = false; ///< false value. 56 }; 57 58 /** 59 * Indicates whether or not a given template `Type` is a pointer type. 60 * 61 * The `constexpr` expression `IsPointer<Type>::kValue` would be `true` when the `Type` is a pointer, otherwise it 62 * would be `false`. 63 * 64 * @tparam Type A type to check if is a pointer. 65 * 66 */ 67 template <typename Type> struct IsPointer : public FalseValue 68 { 69 }; 70 71 // Partial template specializations of the `IsPointer<Type>` 72 73 template <typename Type> struct IsPointer<Type *> : public TrueValue 74 { 75 }; 76 77 template <typename Type> struct IsPointer<const Type *> : public TrueValue 78 { 79 }; 80 81 template <typename Type> struct IsPointer<volatile Type *> : public TrueValue 82 { 83 }; 84 85 template <typename Type> struct IsPointer<const volatile Type *> : TrueValue 86 { 87 }; 88 89 /** 90 * Indicates whether or not a given template `FirstType is the same as `SecondType`. 91 * 92 * The `constexpr` expression `IsSame<FirstType, SecondType>::kValue` would be `true` when the two types are the same, 93 * otherwise it would be `false`. 94 * 95 * @tparam FirstType The first type. 96 * @tparam SecondType The second type. 97 * 98 */ 99 template <typename FirstType, typename SecondType> struct IsSame : public FalseValue 100 { 101 }; 102 103 template <typename Type> struct IsSame<Type, Type> : public TrueValue 104 { 105 }; 106 107 /** 108 * Selects between two given types based on a boolean condition at compile time. 109 * 110 * It provides member type named `Type` which is defined as `TypeOnTrue` if `kCondition` is `true` at compile time, or 111 * as `TypeOnFalse` if `kCondition` is `false`. 112 * 113 * @tparam kCondition The boolean condition which is used to select between the two types. 114 * @tparam TypeOnTrue The type to select when `kCondition` is `true`. 115 * @tparam TypeOnFalse The type to select when `kCondition` is `false`. 116 * 117 */ 118 template <bool kCondition, typename TypeOnTrue, typename TypeOnFalse> struct Conditional 119 { 120 typedef TypeOnFalse Type; ///< The selected type based on `kCondition`. 121 }; 122 123 template <typename TypeOnTrue, typename TypeOnFalse> struct Conditional<true, TypeOnTrue, TypeOnFalse> 124 { 125 typedef TypeOnTrue Type; 126 }; 127 128 /** 129 * Determines the return type of a given function pointer type. 130 * 131 * It provides member type named `Type` which gives the return type of `HandlerType` function pointer. 132 * 133 * For example, `ReturnTypeOf<Error (*)(void *aContext)>::Type` would be `Error`. 134 * 135 * @tparam HandlerType The function pointer type. 136 * 137 */ 138 template <typename HandlerType> struct ReturnTypeOf; 139 140 template <typename RetType, typename... Args> struct ReturnTypeOf<RetType (*)(Args...)> 141 { 142 typedef RetType Type; ///< The return type. 143 }; 144 145 /** 146 * Determines the type of the first argument of a given function pointer type. 147 * 148 * It provides member type named `Type` which gives the first argument type of `HandlerType` function pointer. 149 * 150 * For example, `ReturnTypeOf<Error (*)(void *aContext)>::Type` would be `void *`. 151 * 152 * @tparam HandlerType The function pointer type. 153 * 154 */ 155 template <typename HandlerType> struct FirstArgTypeOf; 156 157 template <typename RetType, typename FirstArgType, typename... Args> 158 struct FirstArgTypeOf<RetType (*)(FirstArgType, Args...)> 159 { 160 typedef FirstArgType Type; ///< The first argument type. 161 }; 162 163 } // namespace TypeTraits 164 } // namespace ot 165 166 #endif // OT_TYPE_TRAITS_HPP_ 167