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 const CxAllocator *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 elem_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 collection |
66 /** \ |
101 |
67 * The allocator to use. \ |
102 /** |
68 */ \ |
103 * Sets a simple destructor function for this collection. |
69 CxAllocator const *allocator; \ |
104 * |
70 /** \ |
105 * @param c the collection |
71 * The comparator function for the elements. \ |
106 * @param destr the destructor function |
72 */ \ |
107 */ |
73 cx_compare_func cmpfunc; \ |
108 #define cxDefineDestructor(c, destr) \ |
74 /** \ |
109 (c)->collection.simple_destructor = (cx_destructor_func) destr |
75 * The size of each element. \ |
110 |
76 */ \ |
111 /** |
77 size_t item_size; \ |
112 * Sets a simple destructor function for this collection. |
78 /** \ |
113 * |
79 * The number of currently stored elements. \ |
114 * @param c the collection |
80 */ \ |
115 * @param destr the destructor function |
81 size_t size; \ |
116 */ |
82 /** \ |
117 #define cxDefineAdvancedDestructor(c, destr, data) \ |
83 * An optional simple destructor for the collection's elements. \ |
118 (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \ |
84 * \ |
119 (c)->collection.destructor_data = data |
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 |
120 |
106 /** |
121 /** |
107 * Invokes the simple destructor function for a specific element. |
122 * Invokes the simple destructor function for a specific element. |
108 * |
123 * |
109 * Usually only used by collection implementations. There should be no need |
124 * Usually only used by collection implementations. There should be no need |