Line data Source code
1 0 : /*
2 : * Copyright (c) 2021 Nordic Semiconductor ASA
3 : *
4 : * SPDX-License-Identifier: Apache-2.0
5 : */
6 :
7 : #ifndef ZEPHYR_INCLUDE_SYS_CBPRINTF_CXX_H_
8 : #define ZEPHYR_INCLUDE_SYS_CBPRINTF_CXX_H_
9 : #ifdef __cplusplus
10 :
11 : /* C++ version for detecting a pointer to a string. */
12 : static inline int z_cbprintf_cxx_is_pchar(char *, bool const_as_fixed)
13 : {
14 : ARG_UNUSED(const_as_fixed);
15 : return 1;
16 : }
17 :
18 : static inline int z_cbprintf_cxx_is_pchar(const char *, bool const_as_fixed)
19 : {
20 : return const_as_fixed ? 0 : 1;
21 : }
22 :
23 : static inline int z_cbprintf_cxx_is_pchar(volatile char *, bool const_as_fixed)
24 : {
25 : ARG_UNUSED(const_as_fixed);
26 : return 1;
27 : }
28 :
29 : static inline int z_cbprintf_cxx_is_pchar(const volatile char *, bool const_as_fixed)
30 : {
31 : ARG_UNUSED(const_as_fixed);
32 : return 1;
33 : }
34 :
35 : static inline int z_cbprintf_cxx_is_pchar(unsigned char *, bool const_as_fixed)
36 : {
37 : ARG_UNUSED(const_as_fixed);
38 : return 1;
39 : }
40 :
41 : static inline int z_cbprintf_cxx_is_pchar(const unsigned char *, bool const_as_fixed)
42 : {
43 : return const_as_fixed ? 0 : 1;
44 : }
45 :
46 : static inline int z_cbprintf_cxx_is_pchar(volatile unsigned char *, bool const_as_fixed)
47 : {
48 : ARG_UNUSED(const_as_fixed);
49 : return 1;
50 : }
51 :
52 : static inline int z_cbprintf_cxx_is_pchar(const volatile unsigned char *, bool const_as_fixed)
53 : {
54 : ARG_UNUSED(const_as_fixed);
55 : return 1;
56 : }
57 : static inline int z_cbprintf_cxx_is_pchar(wchar_t *, bool const_as_fixed)
58 : {
59 : ARG_UNUSED(const_as_fixed);
60 : return 1;
61 : }
62 :
63 : static inline int z_cbprintf_cxx_is_pchar(const wchar_t *, bool const_as_fixed)
64 : {
65 : return const_as_fixed ? 0 : 1;
66 : }
67 :
68 : static inline int z_cbprintf_cxx_is_pchar(volatile wchar_t *, bool const_as_fixed)
69 : {
70 : ARG_UNUSED(const_as_fixed);
71 : return 1;
72 : }
73 :
74 : static inline int z_cbprintf_cxx_is_pchar(const volatile wchar_t *, bool const_as_fixed)
75 : {
76 : ARG_UNUSED(const_as_fixed);
77 : return 1;
78 : }
79 :
80 : template < typename T >
81 : static inline int z_cbprintf_cxx_is_pchar(T arg, bool const_as_fixed)
82 : {
83 : ARG_UNUSED(arg);
84 : TOOLCHAIN_DISABLE_GCC_WARNING(TOOLCHAIN_WARNING_POINTER_ARITH);
85 : ARG_UNUSED(const_as_fixed);
86 : return 0;
87 : TOOLCHAIN_ENABLE_GCC_WARNING(TOOLCHAIN_WARNING_POINTER_ARITH);
88 : }
89 :
90 : /* C++ version for determining if variable type is numeric and fits in 32 bit word. */
91 : static inline int z_cbprintf_cxx_is_word_num(char)
92 : {
93 : return 1;
94 : }
95 :
96 : static inline int z_cbprintf_cxx_is_word_num(unsigned char)
97 : {
98 : return 1;
99 : }
100 :
101 : static inline int z_cbprintf_cxx_is_word_num(short)
102 : {
103 : return 1;
104 : }
105 :
106 : static inline int z_cbprintf_cxx_is_word_num(unsigned short)
107 : {
108 : return 1;
109 : }
110 :
111 : static inline int z_cbprintf_cxx_is_word_num(int)
112 : {
113 : return 1;
114 : }
115 :
116 : static inline int z_cbprintf_cxx_is_word_num(unsigned int)
117 : {
118 : return 1;
119 : }
120 :
121 : static inline int z_cbprintf_cxx_is_word_num(long)
122 : {
123 : return (sizeof(long) <= sizeof(uint32_t)) ? 1 : 0;
124 : }
125 :
126 : static inline int z_cbprintf_cxx_is_word_num(unsigned long)
127 : {
128 : return (sizeof(long) <= sizeof(uint32_t)) ? 1 : 0;
129 : }
130 :
131 : template < typename T >
132 : static inline int z_cbprintf_cxx_is_word_num(T arg)
133 : {
134 : ARG_UNUSED(arg);
135 : TOOLCHAIN_DISABLE_GCC_WARNING(TOOLCHAIN_WARNING_POINTER_ARITH);
136 : return 0;
137 : TOOLCHAIN_ENABLE_GCC_WARNING(TOOLCHAIN_WARNING_POINTER_ARITH);
138 : }
139 :
140 : /* C++ version for determining if argument is a none character pointer. */
141 : static inline int z_cbprintf_cxx_is_none_char_ptr(char)
142 : {
143 : return 0;
144 : }
145 :
146 : static inline int z_cbprintf_cxx_is_none_char_ptr(unsigned char)
147 : {
148 : return 0;
149 : }
150 :
151 : static inline int z_cbprintf_cxx_is_none_char_ptr(short)
152 : {
153 : return 0;
154 : }
155 :
156 : static inline int z_cbprintf_cxx_is_none_char_ptr(unsigned short)
157 : {
158 : return 0;
159 : }
160 :
161 : static inline int z_cbprintf_cxx_is_none_char_ptr(int)
162 : {
163 : return 0;
164 : }
165 :
166 : static inline int z_cbprintf_cxx_is_none_char_ptr(unsigned int)
167 : {
168 : return 0;
169 : }
170 :
171 : static inline int z_cbprintf_cxx_is_none_char_ptr(long)
172 : {
173 : return 0;
174 : }
175 :
176 : static inline int z_cbprintf_cxx_is_none_char_ptr(unsigned long)
177 : {
178 : return 0;
179 : }
180 :
181 : static inline int z_cbprintf_cxx_is_none_char_ptr(long long)
182 : {
183 : return 0;
184 : }
185 :
186 : static inline int z_cbprintf_cxx_is_none_char_ptr(unsigned long long)
187 : {
188 : return 0;
189 : }
190 :
191 : static inline int z_cbprintf_cxx_is_none_char_ptr(float)
192 : {
193 : return 0;
194 : }
195 :
196 : static inline int z_cbprintf_cxx_is_none_char_ptr(double)
197 : {
198 : return 0;
199 : }
200 :
201 : static inline int z_cbprintf_cxx_is_none_char_ptr(char *)
202 : {
203 : return 0;
204 : }
205 :
206 : static inline int z_cbprintf_cxx_is_none_char_ptr(volatile char *)
207 : {
208 : return 0;
209 : }
210 :
211 : static inline int z_cbprintf_cxx_is_none_char_ptr(const char *)
212 : {
213 : return 0;
214 : }
215 :
216 : static inline int z_cbprintf_cxx_is_none_char_ptr(const volatile char *)
217 : {
218 : return 0;
219 : }
220 :
221 : static inline int z_cbprintf_cxx_is_none_char_ptr(unsigned char *)
222 : {
223 : return 0;
224 : }
225 :
226 : static inline int z_cbprintf_cxx_is_none_char_ptr(volatile unsigned char *)
227 : {
228 : return 0;
229 : }
230 :
231 : static inline int z_cbprintf_cxx_is_none_char_ptr(const unsigned char *)
232 : {
233 : return 0;
234 : }
235 :
236 : static inline int z_cbprintf_cxx_is_none_char_ptr(const volatile unsigned char *)
237 : {
238 : return 0;
239 : }
240 :
241 : template < typename T >
242 : static inline int z_cbprintf_cxx_is_none_char_ptr(T arg)
243 : {
244 : ARG_UNUSED(arg);
245 :
246 : return 1;
247 : }
248 :
249 : /* C++ version for calculating argument size. */
250 : static inline size_t z_cbprintf_cxx_arg_size(float f)
251 : {
252 : ARG_UNUSED(f);
253 :
254 : return sizeof(double);
255 : }
256 :
257 : template < typename T >
258 : static inline size_t z_cbprintf_cxx_arg_size(T arg)
259 : {
260 : ARG_UNUSED(arg);
261 :
262 : return MAX(sizeof(T), sizeof(int));
263 : }
264 :
265 : /* C++ version for storing arguments. */
266 : static inline void z_cbprintf_cxx_store_arg(uint8_t *dst, float arg)
267 : {
268 : double d = (double)arg;
269 : void *p = &d;
270 :
271 : z_cbprintf_wcpy((int *)dst, (int *)p, sizeof(d) / sizeof(int));
272 : }
273 :
274 : static inline void z_cbprintf_cxx_store_arg(uint8_t *dst, void *p)
275 : {
276 : z_cbprintf_wcpy((int *)dst, (int *)&p, sizeof(p) / sizeof(int));
277 : }
278 :
279 : static inline void z_cbprintf_cxx_store_arg(uint8_t *dst, char arg)
280 : {
281 : int tmp = arg + 0;
282 :
283 : z_cbprintf_wcpy((int *)dst, &tmp, 1);
284 : }
285 :
286 : static inline void z_cbprintf_cxx_store_arg(uint8_t *dst, unsigned char arg)
287 : {
288 : int tmp = arg + 0;
289 :
290 : z_cbprintf_wcpy((int *)dst, &tmp, 1);
291 : }
292 :
293 : static inline void z_cbprintf_cxx_store_arg(uint8_t *dst, signed char arg)
294 : {
295 : int tmp = arg + 0;
296 :
297 : z_cbprintf_wcpy((int *)dst, &tmp, 1);
298 : }
299 :
300 : static inline void z_cbprintf_cxx_store_arg(uint8_t *dst, short arg)
301 : {
302 : int tmp = arg + 0;
303 :
304 : z_cbprintf_wcpy((int *)dst, &tmp, 1);
305 : }
306 :
307 : static inline void z_cbprintf_cxx_store_arg(uint8_t *dst, unsigned short arg)
308 : {
309 : int tmp = arg + 0;
310 :
311 : z_cbprintf_wcpy((int *)dst, &tmp, 1);
312 : }
313 :
314 : template < typename T >
315 : static inline void z_cbprintf_cxx_store_arg(uint8_t *dst, T arg)
316 : {
317 : size_t wlen = z_cbprintf_cxx_arg_size(arg) / sizeof(int);
318 : void *p = &arg;
319 :
320 : z_cbprintf_wcpy((int *)dst, (int *)p, wlen);
321 : }
322 :
323 : /* C++ version for long double detection. */
324 : static inline int z_cbprintf_cxx_is_longdouble(long double arg)
325 : {
326 : ARG_UNUSED(arg);
327 : return 1;
328 : }
329 :
330 : template < typename T >
331 : static inline int z_cbprintf_cxx_is_longdouble(T arg)
332 : {
333 : ARG_UNUSED(arg);
334 :
335 : return 0;
336 : }
337 :
338 : /* C++ version for calculating argument alignment. */
339 : static inline size_t z_cbprintf_cxx_alignment(float arg)
340 : {
341 : ARG_UNUSED(arg);
342 :
343 : return VA_STACK_ALIGN(double);
344 : }
345 :
346 : static inline size_t z_cbprintf_cxx_alignment(double arg)
347 : {
348 : ARG_UNUSED(arg);
349 :
350 : return VA_STACK_ALIGN(double);
351 : }
352 :
353 : static inline size_t z_cbprintf_cxx_alignment(long double arg)
354 : {
355 : ARG_UNUSED(arg);
356 :
357 : return VA_STACK_ALIGN(long double);
358 : }
359 :
360 : static inline size_t z_cbprintf_cxx_alignment(long long arg)
361 : {
362 : ARG_UNUSED(arg);
363 :
364 : return VA_STACK_ALIGN(long long);
365 : }
366 :
367 : static inline size_t z_cbprintf_cxx_alignment(unsigned long long arg)
368 : {
369 : ARG_UNUSED(arg);
370 :
371 : return VA_STACK_ALIGN(long long);
372 : }
373 :
374 : template < typename T >
375 : static inline size_t z_cbprintf_cxx_alignment(T arg)
376 : {
377 : return MAX(__alignof__(arg), VA_STACK_MIN_ALIGN);
378 : }
379 :
380 : /* C++ version for checking if two arguments are same type */
381 : template < typename T1, typename T2 >
382 : struct z_cbprintf_cxx_is_same_type {
383 : enum {
384 : value = false
385 : };
386 : };
387 :
388 : template < typename T >
389 : struct z_cbprintf_cxx_is_same_type < T, T > {
390 : enum {
391 : value = true
392 : };
393 : };
394 :
395 : template < typename T >
396 : struct z_cbprintf_cxx_remove_reference {
397 : typedef T type;
398 : };
399 :
400 : template < typename T >
401 : struct z_cbprintf_cxx_remove_reference < T & > {
402 : typedef T type;
403 : };
404 :
405 : #if __cplusplus >= 201103L
406 : template < typename T >
407 : struct z_cbprintf_cxx_remove_reference < T && > {
408 : typedef T type;
409 : };
410 : #endif
411 :
412 : template < typename T >
413 : struct z_cbprintf_cxx_remove_cv {
414 : typedef T type;
415 : };
416 :
417 : template < typename T >
418 : struct z_cbprintf_cxx_remove_cv < const T > {
419 : typedef T type;
420 : };
421 :
422 : template < typename T >
423 : struct z_cbprintf_cxx_remove_cv < volatile T > {
424 : typedef T type;
425 : };
426 :
427 : template < typename T >
428 : struct z_cbprintf_cxx_remove_cv < const volatile T > {
429 : typedef T type;
430 : };
431 :
432 : /* Determine if a type is an array */
433 : template < typename T >
434 : struct z_cbprintf_cxx_is_array {
435 : enum {
436 : value = false
437 : };
438 : };
439 :
440 : template < typename T >
441 : struct z_cbprintf_cxx_is_array < T[] > {
442 : enum {
443 : value = true
444 : };
445 : };
446 :
447 : template < typename T, size_t N >
448 : struct z_cbprintf_cxx_is_array < T[N] > {
449 : enum {
450 : value = true
451 : };
452 : };
453 :
454 : /* Determine the type of elements in an array */
455 : template < typename T >
456 : struct z_cbprintf_cxx_remove_extent {
457 : typedef T type;
458 : };
459 :
460 : template < typename T >
461 : struct z_cbprintf_cxx_remove_extent < T[] > {
462 : typedef T type;
463 : };
464 :
465 : template < typename T, size_t N >
466 : struct z_cbprintf_cxx_remove_extent < T[N] > {
467 : typedef T type;
468 : };
469 :
470 : #endif /* __cplusplus */
471 : #endif /* ZEPHYR_INCLUDE_SYS_CBPRINTF_CXX_H_ */
|