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