Branch data Line data Source code
1 : : #ifndef FLATCC_ENDIAN_H
2 : : #define FLATCC_ENDIAN_H
3 : :
4 : : /*
5 : : * This file provides helper macros to define type-specific macros and
6 : : * inline functions that convert between stored data and native data
7 : : * indedpently of both native (host) endianness and protocol endianness
8 : : * (i.e. the serialized endian format).
9 : : *
10 : : * To detect endianness correctly ensure one of the following is defined.
11 : : *
12 : : * __LITTLE_ENDIAN__
13 : : * __BIG_ENDIAN__
14 : : * FLATBUFFERS_LITTLEENDIAN=1
15 : : * FLATBUFFERS_LITTLEENDIAN=0
16 : : *
17 : : * Note: the Clang compiler likely already does this, but other
18 : : * compilers may have their own way, if at all.
19 : : *
20 : : * It is also necessary to include <endian.h> or a compatible
21 : : * implementation in order to provide:
22 : : *
23 : : * le16toh, le32to, le64toh, be16toh, be32toh, be64toh,
24 : : * htole16, htole32, htole64, htobe16, htobe32, htobe64.
25 : : *
26 : : * A simple way to ensure all of the above for most platforms is
27 : : * to include the portable endian support file:
28 : : *
29 : : * #include "flatcc/portable/pendian.h"
30 : : *
31 : : * It is also necessary to include
32 : : *
33 : : * #include "flatcc/flatcc_types.h"
34 : : *
35 : : * or an equivalent file. This makes it possible to change the
36 : : * endianness of the serialized data and the sizes of flatbuffer
37 : : * specific types such as `uoffset_t`.
38 : : *
39 : : * Note: the mentioned include files are likely already included
40 : : * by the file including this file, at least for the default
41 : : * configuration.
42 : : */
43 : :
44 : : #ifndef UINT8_t
45 : : #include <stdint.h>
46 : : #endif
47 : :
48 : : /* These are needed to simplify accessor macros and are not found in <endian.h>. */
49 : : #ifndef le8toh
50 : : #define le8toh(n) (n)
51 : : #endif
52 : :
53 : : #ifndef be8toh
54 : : #define be8toh(n) (n)
55 : : #endif
56 : :
57 : : #ifndef htole8
58 : : #define htole8(n) (n)
59 : : #endif
60 : :
61 : : #ifndef htobe8
62 : : #define htobe8(n) (n)
63 : : #endif
64 : :
65 : : #include "flatcc/flatcc_accessors.h"
66 : :
67 : : /* This is the binary encoding endianness, usually LE for flatbuffers. */
68 : : #if FLATBUFFERS_PROTOCOL_IS_LE
69 : : #define flatbuffers_endian le
70 : : #elif FLATBUFFERS_PROTOCOL_IS_BE
71 : : #define flatbuffers_endian be
72 : : #else
73 : : #error "flatbuffers has no defined endiannesss"
74 : : #endif
75 : :
76 : 54 : __flatcc_define_basic_scalar_accessors(flatbuffers_, flatbuffers_endian)
77 : :
78 : 0 : __flatcc_define_integer_accessors(flatbuffers_bool, flatbuffers_bool_t,
79 : : FLATBUFFERS_BOOL_WIDTH, flatbuffers_endian)
80 : :
81 : 813 : __flatcc_define_integer_accessors(__flatbuffers_uoffset, flatbuffers_uoffset_t,
82 : : FLATBUFFERS_UOFFSET_WIDTH, flatbuffers_endian)
83 : 805 : __flatcc_define_integer_accessors(__flatbuffers_soffset, flatbuffers_soffset_t,
84 : : FLATBUFFERS_SOFFSET_WIDTH, flatbuffers_endian)
85 : 2021 : __flatcc_define_integer_accessors(__flatbuffers_voffset, flatbuffers_voffset_t,
86 : : FLATBUFFERS_VOFFSET_WIDTH, flatbuffers_endian)
87 : 5 : __flatcc_define_integer_accessors(__flatbuffers_utype, flatbuffers_utype_t,
88 : : FLATBUFFERS_UTYPE_WIDTH, flatbuffers_endian)
89 : 108 : __flatcc_define_integer_accessors(__flatbuffers_thash, flatbuffers_thash_t,
90 : : FLATBUFFERS_THASH_WIDTH, flatbuffers_endian)
91 : :
92 : : /* flatcc/portable/pendian.h sets LITTLE/BIG flags if possible, and always defines le16toh. */
93 : : #ifndef flatbuffers_is_native_pe
94 : : #if defined(__LITTLE_ENDIAN__) || FLATBUFFERS_LITTLEENDIAN
95 : : #undef FLATBUFFERS_LITTLEENDIAN
96 : : #define FLATBUFFERS_LITTLEENDIAN 1
97 : : #define flatbuffers_is_native_pe() (FLATBUFFERS_PROTOCOL_IS_LE)
98 : : #elif defined(__BIG_ENDIAN__) || (defined(FLATBUFFERS_LITTLEENDIAN) && !FLATBUFFERS_LITTLEENDIAN)
99 : : #undef FLATBUFFERS_LITTLEENDIAN
100 : : #define FLATBUFFERS_LITTLEENDIAN 0
101 : : #define flatbuffers_is_native_pe() (FLATBUFFERS_PROTOCOL_IS_BE)
102 : : #else
103 : : #define flatbuffers_is_native_pe() (__FLATBUFFERS_CONCAT(flatbuffers_endian, 16toh)(1) == 1)
104 : : #endif
105 : : #endif
106 : :
107 : : #ifndef flatbuffers_is_native_le
108 : : #define flatbuffers_is_native_le() flatbuffers_is_native_pe()
109 : : #endif
110 : :
111 : : #ifndef flatbuffers_is_native_be
112 : : #define flatbuffers_is_native_be() (!flatbuffers_is_native_pe())
113 : : #endif
114 : :
115 : : #endif /* FLATCC_ENDIAN_H */
|