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