]> git.ozlabs.org Git - ccan/blobdiff - ccan/order/order.h
order: Module for comparison callbacks
[ccan] / ccan / order / order.h
diff --git a/ccan/order/order.h b/ccan/order/order.h
new file mode 100644 (file)
index 0000000..f5dac81
--- /dev/null
@@ -0,0 +1,33 @@
+/* CC0 license (public domain) - see LICENSE file for details */
+#ifndef CCAN_ORDER_H
+#define CCAN_ORDER_H
+
+#include <stdint.h>
+#include <assert.h>
+
+#include <ccan/typesafe_cb/typesafe_cb.h>
+
+typedef int (*_total_order_cb)(const void *, const void *, void *);
+typedef int (*total_order_noctx_cb)(const void *, const void *);
+
+#define total_order_cb(_name, _item, _ctx)             \
+       int (*_name)(const __typeof__(_item) *,         \
+                    const __typeof__(_item) *,         \
+                    __typeof__(_ctx))
+
+#define total_order_cast(cmp, item, ctx)                               \
+       typesafe_cb_cast(_total_order_cb, total_order_cb(, item, ctx),  \
+                        (cmp))
+
+struct _total_order {
+       _total_order_cb cb;
+       void *ctx;
+};
+
+#define total_order(_name, _item, _ctx)                        \
+       struct {                                        \
+               total_order_cb(cb, _item, _ctx);        \
+               _ctx ctx;                               \
+       } _name
+
+#endif /* CCAN_ORDER_H */