1 | /* |
2 | * Copyright © 2020 Benjamin Otte |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Lesser General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2.1 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
16 | */ |
17 | |
18 | #ifndef __GTK_SORT_KEYS_PRIVATE_H__ |
19 | #define __GTK_SORT_KEYS_PRIVATE_H__ |
20 | |
21 | #include <gdk/gdk.h> |
22 | #include <gtk/gtkenums.h> |
23 | #include <gtk/gtksorter.h> |
24 | |
25 | typedef struct _GtkSortKeys GtkSortKeys; |
26 | typedef struct _GtkSortKeysClass GtkSortKeysClass; |
27 | |
28 | struct _GtkSortKeys |
29 | { |
30 | const GtkSortKeysClass *klass; |
31 | int ref_count; |
32 | |
33 | gsize key_size; |
34 | gsize key_align; /* must be power of 2 */ |
35 | }; |
36 | |
37 | struct _GtkSortKeysClass |
38 | { |
39 | void (* free) (GtkSortKeys *self); |
40 | |
41 | GCompareDataFunc key_compare; |
42 | |
43 | gboolean (* is_compatible) (GtkSortKeys *self, |
44 | GtkSortKeys *other); |
45 | |
46 | void (* init_key) (GtkSortKeys *self, |
47 | gpointer item, |
48 | gpointer key_memory); |
49 | void (* clear_key) (GtkSortKeys *self, |
50 | gpointer key_memory); |
51 | }; |
52 | |
53 | GtkSortKeys * gtk_sort_keys_alloc (const GtkSortKeysClass *klass, |
54 | gsize size, |
55 | gsize key_size, |
56 | gsize key_align); |
57 | #define gtk_sort_keys_new(_name, _klass, _key_size, _key_align) \ |
58 | ((_name *) gtk_sort_keys_alloc ((_klass), sizeof (_name), (_key_size), (_key_align))) |
59 | GtkSortKeys * gtk_sort_keys_ref (GtkSortKeys *self); |
60 | void gtk_sort_keys_unref (GtkSortKeys *self); |
61 | |
62 | GtkSortKeys * gtk_sort_keys_new_equal (void); |
63 | |
64 | gsize gtk_sort_keys_get_key_size (GtkSortKeys *self); |
65 | gsize gtk_sort_keys_get_key_align (GtkSortKeys *self); |
66 | GCompareDataFunc gtk_sort_keys_get_key_compare_func (GtkSortKeys *self); |
67 | gboolean gtk_sort_keys_is_compatible (GtkSortKeys *self, |
68 | GtkSortKeys *other); |
69 | gboolean gtk_sort_keys_needs_clear_key (GtkSortKeys *self); |
70 | |
71 | #define GTK_SORT_KEYS_ALIGN(_size,_align) (((_size) + (_align) - 1) & ~((_align) - 1)) |
72 | static inline int |
73 | gtk_sort_keys_compare (GtkSortKeys *self, |
74 | gconstpointer a, |
75 | gconstpointer b) |
76 | { |
77 | return self->klass->key_compare (a, b, self); |
78 | } |
79 | |
80 | static inline void |
81 | gtk_sort_keys_init_key (GtkSortKeys *self, |
82 | gpointer item, |
83 | gpointer key_memory) |
84 | { |
85 | self->klass->init_key (self, item, key_memory); |
86 | } |
87 | |
88 | static inline void |
89 | gtk_sort_keys_clear_key (GtkSortKeys *self, |
90 | gpointer key_memory) |
91 | { |
92 | if (self->klass->clear_key) |
93 | self->klass->clear_key (self, key_memory); |
94 | } |
95 | |
96 | #endif /* __GTK_SORT_KEYS_PRIVATE_H__ */ |
97 | |
98 | |