damus

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

flatcc_accessors.h (6007B)


      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 static inline T N ## _read_from_le(const void *p)                           \
     32 { return N ## _cast_from_le(*(T *)p); }                                     \
     33 typedef struct { int is_null; T value; } N ## _option_t;
     34 
     35 #define __flatcc_define_integer_accessors_impl(N, T, W, E)                  \
     36 static inline T N ## _cast_from_pe(T v)                                     \
     37 { return (T) E ## W ## toh((uint ## W ## _t)v); }                           \
     38 static inline T N ## _cast_to_pe(T v)                                       \
     39 { return (T) hto ## E ## W((uint ## W ## _t)v); }                           \
     40 static inline T N ## _cast_from_le(T v)                                     \
     41 { return (T) le ## W ## toh((uint ## W ## _t)v); }                          \
     42 static inline T N ## _cast_to_le(T v)                                       \
     43 { return (T) htole ## W((uint ## W ## _t)v); }                              \
     44 static inline T N ## _cast_from_be(T v)                                     \
     45 { return (T) be ## W ## toh((uint ## W ## _t)v); }                          \
     46 static inline T N ## _cast_to_be(T v)                                       \
     47 { return (T) htobe ## W((uint ## W ## _t)v); }                              \
     48 __flatcc_basic_scalar_accessors_impl(N, T, W, E)
     49 
     50 #define __flatcc_define_real_accessors_impl(N, T, W, E)                     \
     51 union __ ## N ## _cast { T v; uint ## W ## _t u; };                         \
     52 static inline T N ## _cast_from_pe(T v)                                     \
     53 { union __ ## N ## _cast x;                                                 \
     54   x.v = v; x.u = E ## W ## toh(x.u); return x.v; }                          \
     55 static inline T N ## _cast_to_pe(T v)                                       \
     56 { union __ ## N ## _cast x;                                                 \
     57   x.v = v; x.u = hto ## E ## W(x.u); return x.v; }                          \
     58 static inline T N ## _cast_from_le(T v)                                     \
     59 { union __ ## N ## _cast x;                                                 \
     60   x.v = v; x.u = le ## W ## toh(x.u); return x.v; }                         \
     61 static inline T N ## _cast_to_le(T v)                                       \
     62 { union __ ## N ## _cast x;                                                 \
     63   x.v = v; x.u = htole ## W(x.u); return x.v; }                             \
     64 static inline T N ## _cast_from_be(T v)                                     \
     65 { union __ ## N ## _cast x;                                                 \
     66   x.v = v; x.u = be ## W ## toh(x.u); return x.v; }                         \
     67 static inline T N ## _cast_to_be(T v)                                       \
     68 { union __ ## N ## _cast x;                                                 \
     69   x.v = v; x.u = htobe ## W(x.u); return x.v; }                             \
     70 __flatcc_basic_scalar_accessors_impl(N, T, W, E)
     71 
     72 #define __flatcc_define_integer_accessors(N, T, W, E)                       \
     73 __flatcc_define_integer_accessors_impl(N, T, W, E)
     74 
     75 #define __flatcc_define_real_accessors(N, T, W, E)                          \
     76 __flatcc_define_real_accessors_impl(N, T, W, E)
     77 
     78 #define __flatcc_define_basic_integer_accessors(NS, TN, T, W, E)            \
     79 __flatcc_define_integer_accessors(NS ## TN, T, W, E)
     80 
     81 #define __flatcc_define_basic_real_accessors(NS, TN, T, W, E)               \
     82 __flatcc_define_real_accessors(NS ## TN, T, W, E)
     83 
     84 #define __flatcc_define_basic_scalar_accessors(NS, E)                       \
     85 __flatcc_define_basic_integer_accessors(NS, char, char, 8, E)               \
     86 __flatcc_define_basic_integer_accessors(NS, uint8, uint8_t, 8, E)           \
     87 __flatcc_define_basic_integer_accessors(NS, uint16, uint16_t, 16, E)        \
     88 __flatcc_define_basic_integer_accessors(NS, uint32, uint32_t, 32, E)        \
     89 __flatcc_define_basic_integer_accessors(NS, uint64, uint64_t, 64, E)        \
     90 __flatcc_define_basic_integer_accessors(NS, int8, int8_t, 8, E)             \
     91 __flatcc_define_basic_integer_accessors(NS, int16, int16_t, 16, E)          \
     92 __flatcc_define_basic_integer_accessors(NS, int32, int32_t, 32, E)          \
     93 __flatcc_define_basic_integer_accessors(NS, int64, int64_t, 64, E)          \
     94 __flatcc_define_basic_real_accessors(NS, float, float, 32, E)               \
     95 __flatcc_define_basic_real_accessors(NS, double, double, 64, E)
     96 
     97 #ifdef __cplusplus
     98 }
     99 #endif
    100 
    101 #endif /* FLATCC_ACCESSORS */