Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : /**
19 : * @file
20 : * This file provides functions for safely type casting some basic
21 : * datatypes (e.g. uint8_t * <-> unsigned char *)
22 : *
23 : *
24 : */
25 :
26 : #pragma once
27 :
28 : #include <limits.h>
29 : #include <stdint.h>
30 :
31 : namespace chip {
32 :
33 : namespace Uint8 {
34 :
35 : /**
36 : * Safely typecast a pointer to unsigned char to uint8_t
37 : *
38 : * @param[in] in pointer to unsigned char
39 : *
40 : * @return The typecast value
41 : */
42 : inline uint8_t * from_uchar(unsigned char * in)
43 : {
44 : static_assert(CHAR_BIT == 8, "Can't type cast unsigned char array as uint8_t array");
45 : #ifdef __cplusplus
46 : return reinterpret_cast<uint8_t *>(in);
47 : #else
48 : return (uint8_t *) in;
49 : #endif
50 : }
51 :
52 : /**
53 : * Safely typecast a pointer to const unsigned char to const uint8_t
54 : *
55 : * @param[in] in pointer to const unsigned char
56 : *
57 : * @return The typecast value
58 : */
59 : inline const uint8_t * from_const_uchar(const unsigned char * in)
60 : {
61 : static_assert(CHAR_BIT == 8, "Can't type cast unsigned char array as uint8_t array");
62 : #ifdef __cplusplus
63 : return reinterpret_cast<const uint8_t *>(in);
64 : #else
65 : return (const uint8_t *) in;
66 : #endif
67 : }
68 :
69 : /**
70 : * Safely typecast a pointer to char to uint8_t
71 : *
72 : * @param[in] in pointer to char
73 : *
74 : * @return The typecast value
75 : */
76 2 : inline uint8_t * from_char(char * in)
77 : {
78 : static_assert(CHAR_BIT == 8, "Can't type cast char array as uint8_t array");
79 : #ifdef __cplusplus
80 2 : return reinterpret_cast<uint8_t *>(in);
81 : #else
82 : return (uint8_t *) in;
83 : #endif
84 : }
85 :
86 : /**
87 : * Safely typecast a pointer to const char to const uint8_t
88 : *
89 : * @param[in] in pointer to const char
90 : *
91 : * @return The typecast value
92 : */
93 766 : inline const uint8_t * from_const_char(const char * in)
94 : {
95 : static_assert(CHAR_BIT == 8, "Can't type cast char array as uint8_t array");
96 : #ifdef __cplusplus
97 766 : return reinterpret_cast<const uint8_t *>(in);
98 : #else
99 : return (const uint8_t *) in;
100 : #endif
101 : }
102 :
103 : /**
104 : * Safely typecast a pointer to uint8_t to unsigned char
105 : *
106 : * @param[in] in pointer to uint8_t
107 : *
108 : * @return The typecast value
109 : */
110 184414 : inline unsigned char * to_uchar(uint8_t * in)
111 : {
112 : static_assert(CHAR_BIT == 8, "Can't type cast uint8_t array to unsigned char array");
113 : #ifdef __cplusplus
114 184414 : return reinterpret_cast<unsigned char *>(in);
115 : #else
116 : return (unsigned char *) in;
117 : #endif
118 : }
119 :
120 : /**
121 : * Safely typecast a pointer to const uint8_t to const unsigned char
122 : *
123 : * @param[in] in pointer to const uint8_t
124 : *
125 : * @return The typecast value
126 : */
127 77190 : inline const unsigned char * to_const_uchar(const uint8_t * in)
128 : {
129 : static_assert(CHAR_BIT == 8, "Can't type cast uint8_t array to unsigned char array");
130 : #ifdef __cplusplus
131 77190 : return reinterpret_cast<const unsigned char *>(in);
132 : #else
133 : return (const unsigned char *) in;
134 : #endif
135 : }
136 :
137 : /**
138 : * Safely typecast a pointer to uint8_t to char
139 : *
140 : * @param[in] in pointer to uint8_t
141 : *
142 : * @return The typecast value
143 : */
144 : inline char * to_char(uint8_t * in)
145 : {
146 : static_assert(CHAR_BIT == 8, "Can't type cast uint8_t array to char array");
147 : #ifdef __cplusplus
148 : return reinterpret_cast<char *>(in);
149 : #else
150 : return (char *) in;
151 : #endif
152 : }
153 :
154 : /**
155 : * Safely typecast a pointer to const uint8_t to char
156 : *
157 : * @param[in] in pointer to const uint8_t
158 : *
159 : * @return The typecast value
160 : */
161 415 : inline const char * to_const_char(const uint8_t * in)
162 : {
163 : static_assert(CHAR_BIT == 8, "Can't type cast uint8_t array to char array");
164 : #ifdef __cplusplus
165 415 : return reinterpret_cast<const char *>(in);
166 : #else
167 : return (const char *) in;
168 : #endif
169 : }
170 :
171 : } // namespace Uint8
172 : } // namespace chip
|