1 /* CC0 license (public domain) - see LICENSE file for details */
8 #include <ccan/typesafe_cb/typesafe_cb.h>
9 #include <ccan/ptrint/ptrint.h>
11 typedef int (*_total_order_cb)(const void *, const void *, void *);
12 typedef int (*total_order_noctx_cb)(const void *, const void *);
14 #define total_order_cb(_name, _item, _ctx) \
15 int (*_name)(const __typeof__(_item) *, \
16 const __typeof__(_item) *, \
19 #define total_order_cast(cmp, item, ctx) \
20 typesafe_cb_cast(_total_order_cb, total_order_cb(, item, ctx), \
28 #define total_order(_name, _item, _ctx) \
30 total_order_cb(cb, _item, _ctx); \
34 #define _DECL_ONAME(_oname, _itype) \
35 extern int _order_##_oname(const void *, const void *, void *); \
36 extern int order_##_oname(const _itype *, const _itype *, void *); \
37 extern int order_##_oname##_noctx(const void *, const void *);
39 #define _DECL_ONAME_BIDIR(_oname, _itype) \
40 _DECL_ONAME(_oname, _itype) \
41 _DECL_ONAME(_oname##_reverse, _itype)
43 _DECL_ONAME_BIDIR(s8, int8_t)
44 _DECL_ONAME_BIDIR(s16, int16_t)
45 _DECL_ONAME_BIDIR(s32, int32_t)
46 _DECL_ONAME_BIDIR(s64, int64_t)
48 _DECL_ONAME_BIDIR(u8, uint8_t)
49 _DECL_ONAME_BIDIR(u16, uint16_t)
50 _DECL_ONAME_BIDIR(u32, uint32_t)
51 _DECL_ONAME_BIDIR(u64, uint64_t)
53 _DECL_ONAME_BIDIR(int, int)
54 _DECL_ONAME_BIDIR(uint, unsigned int)
55 _DECL_ONAME_BIDIR(long, long)
56 _DECL_ONAME_BIDIR(ulong, unsigned long)
57 _DECL_ONAME_BIDIR(size, size_t)
58 _DECL_ONAME_BIDIR(ptrdiff, ptrdiff_t)
60 _DECL_ONAME_BIDIR(float, float)
61 _DECL_ONAME_BIDIR(double, double)
64 #undef _DECL_ONAME_BIDIR
66 #define total_order_by_field(_name, _oname, _itype, _field) \
67 total_order(_name, _itype, ptrint_t *) = { \
68 (total_order_cb(, _itype, \
69 ptrint_t *))(_order_##_oname), \
70 int2ptr(offsetof(_itype, _field)), \
73 #endif /* CCAN_ORDER_H */