damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

flatcc_endian.h (3991B)


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