]> git.ozlabs.org Git - ccan/blob - ccan/order/order.h
edceab5551e2e68dd14bee39bf7ac4a12663841b
[ccan] / ccan / order / order.h
1 /* CC0 license (public domain) - see LICENSE file for details */
2 #ifndef CCAN_ORDER_H
3 #define CCAN_ORDER_H
4
5 #include <stdint.h>
6 #include <assert.h>
7
8 #include <ccan/typesafe_cb/typesafe_cb.h>
9 #include <ccan/ptrint/ptrint.h>
10
11 typedef int (*_total_order_cb)(const void *, const void *, void *);
12 typedef int (*total_order_noctx_cb)(const void *, const void *);
13
14 #define total_order_cb(_name, _item, _ctx)              \
15         int (*_name)(const __typeof__(_item) *,         \
16                      const __typeof__(_item) *,         \
17                      __typeof__(_ctx))
18
19 #define total_order_cast(cmp, item, ctx)                                \
20         typesafe_cb_cast(_total_order_cb, total_order_cb(, item, ctx),  \
21                          (cmp))
22
23 struct _total_order {
24         _total_order_cb cb;
25         void *ctx;
26 };
27
28 #define total_order(_name, _item, _ctx)                 \
29         struct {                                        \
30                 total_order_cb(cb, _item, _ctx);        \
31                 _ctx ctx;                               \
32         } _name
33
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 *);
38
39 #define _DECL_ONAME_BIDIR(_oname, _itype)                               \
40         _DECL_ONAME(_oname, _itype)                                     \
41         _DECL_ONAME(_oname##_reverse, _itype)
42
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)
47
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)
52
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)
59
60 _DECL_ONAME_BIDIR(float, float)
61 _DECL_ONAME_BIDIR(double, double)
62
63 #undef _DECL_ONAME
64 #undef _DECL_ONAME_BIDIR
65
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)),                      \
71         }
72
73 #endif /* CCAN_ORDER_H */