36 #ifndef UCX_COLLECTION_H |
36 #ifndef UCX_COLLECTION_H |
37 #define UCX_COLLECTION_H |
37 #define UCX_COLLECTION_H |
38 |
38 |
39 #include "allocator.h" |
39 #include "allocator.h" |
40 #include "iterator.h" |
40 #include "iterator.h" |
|
41 #include "compare.h" |
41 |
42 |
42 #ifdef __cplusplus |
43 #ifdef __cplusplus |
43 extern "C" { |
44 extern "C" { |
44 #endif |
45 #endif |
45 |
46 |
46 /** |
47 /** |
47 * Special constant used for creating collections that are storing pointers. |
48 * Special constant used for creating collections that are storing pointers. |
48 */ |
49 */ |
49 #define CX_STORE_POINTERS 0 |
50 #define CX_STORE_POINTERS 0 |
50 |
51 |
51 #ifndef CX_COMPARE_FUNC_DEFINED |
|
52 #define CX_COMPARE_FUNC_DEFINED |
|
53 /** |
52 /** |
54 * A comparator function comparing two collection elements. |
53 * Base attributes of a collection. |
55 */ |
54 */ |
56 typedef int(*cx_compare_func)( |
55 struct cx_collection_s { |
57 void const *left, |
56 /** |
58 void const *right |
57 * The allocator to use. |
59 ); |
58 */ |
60 #endif // CX_COMPARE_FUNC_DEFINED |
59 CxAllocator const *allocator; |
|
60 /** |
|
61 * The comparator function for the elements. |
|
62 */ |
|
63 cx_compare_func cmpfunc; |
|
64 /** |
|
65 * The size of each element. |
|
66 */ |
|
67 size_t item_size; |
|
68 /** |
|
69 * The number of currently stored elements. |
|
70 */ |
|
71 size_t size; |
|
72 /** |
|
73 * An optional simple destructor for the collection's elements. |
|
74 * |
|
75 * @attention Read the documentation of the particular collection implementation |
|
76 * whether this destructor shall only destroy the contents or also free the memory. |
|
77 */ |
|
78 cx_destructor_func simple_destructor; |
|
79 /** |
|
80 * An optional advanced destructor for the collection's elements. |
|
81 * |
|
82 * @attention Read the documentation of the particular collection implementation |
|
83 * whether this destructor shall only destroy the contents or also free the memory. |
|
84 */ |
|
85 cx_destructor_func2 advanced_destructor; |
|
86 /** |
|
87 * The pointer to additional data that is passed to the advanced destructor. |
|
88 */ |
|
89 void *destructor_data; |
|
90 /** |
|
91 * Indicates if this list is supposed to store pointers |
|
92 * instead of copies of the actual objects. |
|
93 */ |
|
94 bool store_pointer; |
|
95 }; |
61 |
96 |
62 /** |
97 /** |
63 * Use this macro to declare common members for a collection structure. |
98 * Use this macro to declare common members for a collection structure. |
64 */ |
99 */ |
65 #define CX_COLLECTION_MEMBERS \ |
100 #define CX_COLLECTION_BASE struct cx_collection_s base |
66 /** \ |
|
67 * The allocator to use. \ |
|
68 */ \ |
|
69 CxAllocator const *allocator; \ |
|
70 /** \ |
|
71 * The comparator function for the elements. \ |
|
72 */ \ |
|
73 cx_compare_func cmpfunc; \ |
|
74 /** \ |
|
75 * The size of each element. \ |
|
76 */ \ |
|
77 size_t item_size; \ |
|
78 /** \ |
|
79 * The number of currently stored elements. \ |
|
80 */ \ |
|
81 size_t size; \ |
|
82 /** \ |
|
83 * An optional simple destructor for the collection's elements. \ |
|
84 * \ |
|
85 * @attention Read the documentation of the particular collection implementation \ |
|
86 * whether this destructor shall only destroy the contents or also free the memory. \ |
|
87 */ \ |
|
88 cx_destructor_func simple_destructor; \ |
|
89 /** \ |
|
90 * An optional advanced destructor for the collection's elements. \ |
|
91 * \ |
|
92 * @attention Read the documentation of the particular collection implementation \ |
|
93 * whether this destructor shall only destroy the contents or also free the memory. \ |
|
94 */ \ |
|
95 cx_destructor_func2 advanced_destructor; \ |
|
96 /** \ |
|
97 * The pointer to additional data that is passed to the advanced destructor. \ |
|
98 */ \ |
|
99 void *destructor_data; \ |
|
100 /** \ |
|
101 * Indicates if this instance of a collection is supposed to store pointers \ |
|
102 * instead of copies of the actual objects. \ |
|
103 */ \ |
|
104 bool store_pointer; |
|
105 |
101 |
106 /** |
102 /** |
107 * Invokes the simple destructor function for a specific element. |
103 * Invokes the simple destructor function for a specific element. |
108 * |
104 * |
109 * Usually only used by collection implementations. There should be no need |
105 * Usually only used by collection implementations. There should be no need |
111 * |
107 * |
112 * @param c the collection |
108 * @param c the collection |
113 * @param e the element |
109 * @param e the element |
114 */ |
110 */ |
115 #define cx_invoke_simple_destructor(c, e) \ |
111 #define cx_invoke_simple_destructor(c, e) \ |
116 (c)->simple_destructor((c)->store_pointer ? (*((void **) (e))) : (e)) |
112 (c)->base.simple_destructor((c)->base.store_pointer ? (*((void **) (e))) : (e)) |
117 |
113 |
118 /** |
114 /** |
119 * Invokes the advanced destructor function for a specific element. |
115 * Invokes the advanced destructor function for a specific element. |
120 * |
116 * |
121 * Usually only used by collection implementations. There should be no need |
117 * Usually only used by collection implementations. There should be no need |
123 * |
119 * |
124 * @param c the collection |
120 * @param c the collection |
125 * @param e the element |
121 * @param e the element |
126 */ |
122 */ |
127 #define cx_invoke_advanced_destructor(c, e) \ |
123 #define cx_invoke_advanced_destructor(c, e) \ |
128 (c)->advanced_destructor((c)->destructor_data, \ |
124 (c)->base.advanced_destructor((c)->base.destructor_data, \ |
129 (c)->store_pointer ? (*((void **) (e))) : (e)) |
125 (c)->base.store_pointer ? (*((void **) (e))) : (e)) |
130 |
126 |
131 |
127 |
132 /** |
128 /** |
133 * Invokes all available destructor functions for a specific element. |
129 * Invokes all available destructor functions for a specific element. |
134 * |
130 * |