1 #ifndef FLATCC_ACCESSORS
2 #define FLATCC_ACCESSORS
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 
8 #ifndef UINT8_MAX
9 #include <stdint.h>
10 #endif
11 
12 #define __flatcc_basic_scalar_accessors_impl(N, T, W, E)                    \
13 static inline size_t N ## __size(void)                                      \
14 { return sizeof(T); }                                                       \
15 static inline T *N ## __ptr_add(T *p, size_t i)                             \
16 { return p + i; }                                                           \
17 static inline const T *N ## __const_ptr_add(const T *p, size_t i)           \
18 { return p + i; }                                                           \
19 static inline T N ## _read_from_pe(const void *p)                           \
20 { return N ## _cast_from_pe(*(T *)p); }                                     \
21 static inline T N ## _read_to_pe(const void *p)                             \
22 { return N ## _cast_to_pe(*(T *)p); }                                       \
23 static inline T N ## _read(const void *p)                                   \
24 { return *(T *)p; }                                                         \
25 static inline void N ## _write_from_pe(void *p, T v)                        \
26 { *(T *)p = N ## _cast_from_pe(v); }                                        \
27 static inline void N ## _write_to_pe(void *p, T v)                          \
28 { *(T *)p = N ## _cast_to_pe(v); }                                          \
29 static inline void N ## _write(void *p, T v)                                \
30 { *(T *)p = v; }
31 
32 #define __flatcc_define_integer_accessors_impl(N, T, W, E)                  \
33 static inline T N ## _cast_from_pe(T v)                                     \
34 { return (T) E ## W ## toh((uint ## W ## _t)v); }                           \
35 static inline T N ## _cast_to_pe(T v)                                       \
36 { return (T) hto ## E ## W((uint ## W ## _t)v); }                           \
37 static inline T N ## _cast_from_le(T v)                                     \
38 { return (T) le ## W ## toh((uint ## W ## _t)v); }                          \
39 static inline T N ## _cast_to_le(T v)                                       \
40 { return (T) htole ## W((uint ## W ## _t)v); }                              \
41 static inline T N ## _cast_from_be(T v)                                     \
42 { return (T) be ## W ## toh((uint ## W ## _t)v); }                          \
43 static inline T N ## _cast_to_be(T v)                                       \
44 { return (T) htobe ## W((uint ## W ## _t)v); }                              \
45 __flatcc_basic_scalar_accessors_impl(N, T, W, E)
46 
47 #define __flatcc_define_real_accessors_impl(N, T, W, E)                     \
48 union __ ## N ## _cast { T v; uint ## W ## _t u; };                         \
49 static inline T N ## _cast_from_pe(T v)                                     \
50 { union __ ## N ## _cast x;                                                 \
51   x.v = v; x.u = E ## W ## toh(x.u); return x.v; }                          \
52 static inline T N ## _cast_to_pe(T v)                                       \
53 { union __ ## N ## _cast x;                                                 \
54   x.v = v; x.u = hto ## E ## W(x.u); return x.v; }                          \
55 static inline T N ## _cast_from_le(T v)                                     \
56 { union __ ## N ## _cast x;                                                 \
57   x.v = v; x.u = le ## W ## toh(x.u); return x.v; }                         \
58 static inline T N ## _cast_to_le(T v)                                       \
59 { union __ ## N ## _cast x;                                                 \
60   x.v = v; x.u = htole ## W(x.u); return x.v; }                             \
61 static inline T N ## _cast_from_be(T v)                                     \
62 { union __ ## N ## _cast x;                                                 \
63   x.v = v; x.u = be ## W ## toh(x.u); return x.v; }                         \
64 static inline T N ## _cast_to_be(T v)                                       \
65 { union __ ## N ## _cast x;                                                 \
66   x.v = v; x.u = htobe ## W(x.u); return x.v; }                             \
67 __flatcc_basic_scalar_accessors_impl(N, T, W, E)
68 
69 #define __flatcc_define_integer_accessors(N, T, W, E)                       \
70 __flatcc_define_integer_accessors_impl(N, T, W, E)
71 
72 #define __flatcc_define_real_accessors(N, T, W, E)                          \
73 __flatcc_define_real_accessors_impl(N, T, W, E)
74 
75 #define __flatcc_define_basic_integer_accessors(NS, TN, T, W, E)            \
76 __flatcc_define_integer_accessors(NS ## TN, T, W, E)
77 
78 #define __flatcc_define_basic_real_accessors(NS, TN, T, W, E)               \
79 __flatcc_define_real_accessors(NS ## TN, T, W, E)
80 
81 #define __flatcc_define_basic_scalar_accessors(NS, E)                       \
82 __flatcc_define_basic_integer_accessors(NS, char, char, 8, E)               \
83 __flatcc_define_basic_integer_accessors(NS, uint8, uint8_t, 8, E)           \
84 __flatcc_define_basic_integer_accessors(NS, uint16, uint16_t, 16, E)        \
85 __flatcc_define_basic_integer_accessors(NS, uint32, uint32_t, 32, E)        \
86 __flatcc_define_basic_integer_accessors(NS, uint64, uint64_t, 64, E)        \
87 __flatcc_define_basic_integer_accessors(NS, int8, int8_t, 8, E)             \
88 __flatcc_define_basic_integer_accessors(NS, int16, int16_t, 16, E)          \
89 __flatcc_define_basic_integer_accessors(NS, int32, int32_t, 32, E)          \
90 __flatcc_define_basic_integer_accessors(NS, int64, int64_t, 64, E)          \
91 __flatcc_define_basic_real_accessors(NS, float, float, 32, E)               \
92 __flatcc_define_basic_real_accessors(NS, double, double, 64, E)
93 
94 #ifdef __cplusplus
95 }
96 #endif
97 
98 #endif /* FLATCC_ACCESSORS */
99