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 helper functions to convert between public OT C structs and corresponding core C++ classes.
32 */
33
34 #ifndef AS_CORE_TYPE_HPP_
35 #define AS_CORE_TYPE_HPP_
36
37 #include "openthread-core-config.h"
38
39 namespace ot {
40
41 /**
42 * This structure relates a given public OT type to its corresponding core C++ class/type.
43 *
44 * @tparam FromType The public OT type.
45 *
46 * Specializations of this template struct are provided for different `FromType` which include a member type definition
47 * named `Type` to provide the corresponding core class/type related to `FromType.
48 *
49 * For example, `CoreType<otIp6Address>::Type` is defined as `Ip6::Address`.
50 *
51 */
52 template <typename FromType> struct CoreType;
53
54 /**
55 * This function converts a pointer to public OT type (C struct) to the corresponding core C++ type reference.
56 *
57 * @tparam Type The public OT type to convert.
58 *
59 * @param[in] aObject A pointer to the object to convert.
60 *
61 * @returns A reference of the corresponding C++ type matching @p aObject.
62 *
63 */
AsCoreType(Type * aObject)64 template <typename Type> typename CoreType<Type>::Type &AsCoreType(Type *aObject)
65 {
66 return *static_cast<typename CoreType<Type>::Type *>(aObject);
67 }
68
69 /**
70 * This function converts a const pointer to public OT type (C struct) to the corresponding core C++ type reference.
71 *
72 * @tparam Type The public OT type to convert.
73 *
74 * @param[in] aObject A const pointer to the object to convert.
75 *
76 * @returns A const reference of the corresponding C++ type matching @p aObject.
77 *
78 */
AsCoreType(const Type * aObject)79 template <typename Type> const typename CoreType<Type>::Type &AsCoreType(const Type *aObject)
80 {
81 return *static_cast<const typename CoreType<Type>::Type *>(aObject);
82 }
83
84 /**
85 * This function converts a pointer to public OT type (C struct) to the corresponding core C++ type pointer.
86 *
87 * @tparam Type The public OT type to convert.
88 *
89 * @param[in] aObject A pointer to the object to convert.
90 *
91 * @returns A pointer of the corresponding C++ type matching @p aObject.
92 *
93 */
AsCoreTypePtr(Type * aObject)94 template <typename Type> typename CoreType<Type>::Type *AsCoreTypePtr(Type *aObject)
95 {
96 return static_cast<typename CoreType<Type>::Type *>(aObject);
97 }
98
99 /**
100 * This function converts a const pointer to public OT type (C struct) to the corresponding core C++ type pointer.
101 *
102 * @tparam Type The public OT type to convert.
103 *
104 * @param[in] aObject A pointer to the object to convert.
105 *
106 * @returns A const pointer of the corresponding C++ type matching @p aObject.
107 *
108 */
AsCoreTypePtr(const Type * aObject)109 template <typename Type> const typename CoreType<Type>::Type *AsCoreTypePtr(const Type *aObject)
110 {
111 return static_cast<const typename CoreType<Type>::Type *>(aObject);
112 }
113
114 /**
115 * This structure maps two enumeration types.
116 *
117 * @tparam FromEnumType The enum type.
118 *
119 * Specializations of this template struct are provided which include a member type definition named `Type` to provide
120 * the related `enum` type mapped with `FromEnumType`.
121 *
122 * For example, `MappedEnum<otMacFilterAddressMode>::Type` is defined as `Mac::Filter::Mode`.
123 *
124 */
125 template <typename FromEnumType> struct MappedEnum;
126
127 /**
128 * This function convert an enumeration type value to a related enumeration type value.
129 *
130 * @param[in] aValue The enumeration value to convert
131 *
132 * @returns The matching enumeration value.
133 *
134 */
MapEnum(EnumType aValue)135 template <typename EnumType> const typename MappedEnum<EnumType>::Type MapEnum(EnumType aValue)
136 {
137 return static_cast<typename MappedEnum<EnumType>::Type>(aValue);
138 }
139
140 // Helper macro to define specialization of `CoreType` struct relating `BaseType` with `SubType`.
141 #define DefineCoreType(BaseType, SubType) \
142 template <> struct CoreType<BaseType> \
143 { \
144 using Type = SubType; \
145 }
146
147 // Helper macro to map two related enumeration types.
148 #define DefineMapEnum(FirstEnumType, SecondEnumType) \
149 template <> struct MappedEnum<FirstEnumType> \
150 { \
151 using Type = SecondEnumType; \
152 }; \
153 \
154 template <> struct MappedEnum<SecondEnumType> \
155 { \
156 using Type = FirstEnumType; \
157 }
158
159 } // namespace ot
160
161 #endif // AS_CORE_TYPE_HPP_
162