ucx/cx/collection.h

branch
ucx-3.1
changeset 816
839fefbdedc7
parent 775
e5909dff0dbf
equal deleted inserted replaced
815:1f40ca07ae1b 816:839fefbdedc7
28 /** 28 /**
29 * \file collection.h 29 * \file collection.h
30 * \brief Common definitions for various collection implementations. 30 * \brief Common definitions for various collection implementations.
31 * \author Mike Becker 31 * \author Mike Becker
32 * \author Olaf Wintermann 32 * \author Olaf Wintermann
33 * \version 3.0
34 * \copyright 2-Clause BSD License 33 * \copyright 2-Clause BSD License
35 */ 34 */
36 35
37 #ifndef UCX_COLLECTION_H 36 #ifndef UCX_COLLECTION_H
38 #define UCX_COLLECTION_H 37 #define UCX_COLLECTION_H
39 38
40 #include "allocator.h" 39 #include "allocator.h"
41 #include "iterator.h" 40 #include "iterator.h"
41 #include "compare.h"
42 42
43 #ifdef __cplusplus 43 #ifdef __cplusplus
44 extern "C" { 44 extern "C" {
45 #endif 45 #endif
46 46
48 * Special constant used for creating collections that are storing pointers. 48 * Special constant used for creating collections that are storing pointers.
49 */ 49 */
50 #define CX_STORE_POINTERS 0 50 #define CX_STORE_POINTERS 0
51 51
52 /** 52 /**
53 * A comparator function comparing two collection elements. 53 * Base attributes of a collection.
54 */ 54 */
55 typedef int(*cx_compare_func)( 55 struct cx_collection_s {
56 void const *left, 56 /**
57 void const *right 57 * The allocator to use.
58 ); 58 */
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 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 };
59 96
60 /** 97 /**
61 * Use this macro to declare common members for a collection structure. 98 * Use this macro to declare common members for a collection structure.
62 */ 99 */
63 #define CX_COLLECTION_MEMBERS \ 100 #define CX_COLLECTION_BASE struct cx_collection_s collection
64 /** \ 101
65 * The allocator to use. \ 102 /**
66 */ \ 103 * Sets a simple destructor function for this collection.
67 CxAllocator const *allocator; \ 104 *
68 /** \ 105 * @param c the collection
69 * The comparator function for the elements. \ 106 * @param destr the destructor function
70 */ \ 107 */
71 cx_compare_func cmpfunc; \ 108 #define cxDefineDestructor(c, destr) \
72 /** \ 109 (c)->collection.simple_destructor = (cx_destructor_func) destr
73 * The size of each element. \ 110
74 */ \ 111 /**
75 size_t item_size; \ 112 * Sets a simple destructor function for this collection.
76 /** \ 113 *
77 * The number of currently stored elements. \ 114 * @param c the collection
78 */ \ 115 * @param destr the destructor function
79 size_t size; \ 116 */
80 /** \ 117 #define cxDefineAdvancedDestructor(c, destr, data) \
81 * An optional simple destructor for the collection's elements. \ 118 (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \
82 * \ 119 (c)->collection.destructor_data = data
83 * @attention Read the documentation of the particular collection implementation \
84 * whether this destructor shall only destroy the contents or also free the memory. \
85 */ \
86 cx_destructor_func simple_destructor; \
87 /** \
88 * An optional advanced destructor for the collection's elements. \
89 * \
90 * @attention Read the documentation of the particular collection implementation \
91 * whether this destructor shall only destroy the contents or also free the memory. \
92 */ \
93 cx_destructor_func2 advanced_destructor; \
94 /** \
95 * The pointer to additional data that is passed to the advanced destructor. \
96 */ \
97 void *destructor_data; \
98 /** \
99 * Indicates if this instance of a collection is supposed to store pointers \
100 * instead of copies of the actual objects. \
101 */ \
102 bool store_pointer;
103 120
104 /** 121 /**
105 * Invokes the simple destructor function for a specific element. 122 * Invokes the simple destructor function for a specific element.
106 * 123 *
107 * Usually only used by collection implementations. There should be no need 124 * Usually only used by collection implementations. There should be no need
109 * 126 *
110 * @param c the collection 127 * @param c the collection
111 * @param e the element 128 * @param e the element
112 */ 129 */
113 #define cx_invoke_simple_destructor(c, e) \ 130 #define cx_invoke_simple_destructor(c, e) \
114 (c)->simple_destructor((c)->store_pointer ? (*((void **) (e))) : (e)) 131 (c)->collection.simple_destructor((c)->collection.store_pointer ? (*((void **) (e))) : (e))
115 132
116 /** 133 /**
117 * Invokes the advanced destructor function for a specific element. 134 * Invokes the advanced destructor function for a specific element.
118 * 135 *
119 * Usually only used by collection implementations. There should be no need 136 * Usually only used by collection implementations. There should be no need
121 * 138 *
122 * @param c the collection 139 * @param c the collection
123 * @param e the element 140 * @param e the element
124 */ 141 */
125 #define cx_invoke_advanced_destructor(c, e) \ 142 #define cx_invoke_advanced_destructor(c, e) \
126 (c)->advanced_destructor((c)->destructor_data, \ 143 (c)->collection.advanced_destructor((c)->collection.destructor_data, \
127 (c)->store_pointer ? (*((void **) (e))) : (e)) 144 (c)->collection.store_pointer ? (*((void **) (e))) : (e))
128 145
129 146
130 /** 147 /**
131 * Invokes all available destructor functions for a specific element. 148 * Invokes all available destructor functions for a specific element.
132 * 149 *
135 * 152 *
136 * @param c the collection 153 * @param c the collection
137 * @param e the element 154 * @param e the element
138 */ 155 */
139 #define cx_invoke_destructor(c, e) \ 156 #define cx_invoke_destructor(c, e) \
140 if ((c)->simple_destructor) cx_invoke_simple_destructor(c,e); \ 157 if ((c)->collection.simple_destructor) cx_invoke_simple_destructor(c,e); \
141 if ((c)->advanced_destructor) cx_invoke_advanced_destructor(c,e) 158 if ((c)->collection.advanced_destructor) cx_invoke_advanced_destructor(c,e)
142 159
143 #ifdef __cplusplus 160 #ifdef __cplusplus
144 } // extern "C" 161 } // extern "C"
145 #endif 162 #endif
146 163

mercurial