24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 * POSSIBILITY OF SUCH DAMAGE. |
26 * POSSIBILITY OF SUCH DAMAGE. |
27 */ |
27 */ |
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 * \copyright 2-Clause BSD License |
33 * @copyright 2-Clause BSD License |
34 */ |
34 */ |
35 |
35 |
36 #ifndef UCX_COLLECTION_H |
36 #ifndef UCX_COLLECTION_H |
37 #define UCX_COLLECTION_H |
37 #define UCX_COLLECTION_H |
38 |
38 |
94 bool store_pointer; |
94 bool store_pointer; |
95 }; |
95 }; |
96 |
96 |
97 /** |
97 /** |
98 * Use this macro to declare common members for a collection structure. |
98 * Use this macro to declare common members for a collection structure. |
|
99 * |
|
100 * @par Example Use |
|
101 * @code |
|
102 * struct MyCustomSet { |
|
103 * CX_COLLECTION_BASE; |
|
104 * MySetElements *data; |
|
105 * } |
|
106 * @endcode |
99 */ |
107 */ |
100 #define CX_COLLECTION_BASE struct cx_collection_s collection |
108 #define CX_COLLECTION_BASE struct cx_collection_s collection |
101 |
109 |
102 /** |
110 /** |
103 * Sets a simple destructor function for this collection. |
111 * Sets a simple destructor function for this collection. |
104 * |
112 * |
105 * @param c the collection |
113 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
106 * @param destr the destructor function |
114 * @param destr the destructor function |
107 */ |
115 */ |
108 #define cxDefineDestructor(c, destr) \ |
116 #define cxDefineDestructor(c, destr) \ |
109 (c)->collection.simple_destructor = (cx_destructor_func) destr |
117 (c)->collection.simple_destructor = (cx_destructor_func) destr |
110 |
118 |
111 /** |
119 /** |
112 * Sets a simple destructor function for this collection. |
120 * Sets a simple destructor function for this collection. |
113 * |
121 * |
114 * @param c the collection |
122 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
115 * @param destr the destructor function |
123 * @param destr the destructor function |
116 */ |
124 */ |
117 #define cxDefineAdvancedDestructor(c, destr, data) \ |
125 #define cxDefineAdvancedDestructor(c, destr, data) \ |
118 (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \ |
126 (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \ |
119 (c)->collection.destructor_data = data |
127 (c)->collection.destructor_data = data |
122 * Invokes the simple destructor function for a specific element. |
130 * Invokes the simple destructor function for a specific element. |
123 * |
131 * |
124 * Usually only used by collection implementations. There should be no need |
132 * Usually only used by collection implementations. There should be no need |
125 * to invoke this macro manually. |
133 * to invoke this macro manually. |
126 * |
134 * |
127 * @param c the collection |
135 * When the collection stores pointers, those pointers are directly passed |
128 * @param e the element |
136 * to the destructor. Otherwise, a pointer to the element is passed. |
|
137 * |
|
138 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
|
139 * @param e the element (the type is @c void* or @c void** depending on context) |
129 */ |
140 */ |
130 #define cx_invoke_simple_destructor(c, e) \ |
141 #define cx_invoke_simple_destructor(c, e) \ |
131 (c)->collection.simple_destructor((c)->collection.store_pointer ? (*((void **) (e))) : (e)) |
142 (c)->collection.simple_destructor((c)->collection.store_pointer ? (*((void **) (e))) : (e)) |
132 |
143 |
133 /** |
144 /** |
134 * Invokes the advanced destructor function for a specific element. |
145 * Invokes the advanced destructor function for a specific element. |
135 * |
146 * |
136 * Usually only used by collection implementations. There should be no need |
147 * Usually only used by collection implementations. There should be no need |
137 * to invoke this macro manually. |
148 * to invoke this macro manually. |
138 * |
149 * |
139 * @param c the collection |
150 * When the collection stores pointers, those pointers are directly passed |
140 * @param e the element |
151 * to the destructor. Otherwise, a pointer to the element is passed. |
|
152 * |
|
153 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
|
154 * @param e the element (the type is @c void* or @c void** depending on context) |
141 */ |
155 */ |
142 #define cx_invoke_advanced_destructor(c, e) \ |
156 #define cx_invoke_advanced_destructor(c, e) \ |
143 (c)->collection.advanced_destructor((c)->collection.destructor_data, \ |
157 (c)->collection.advanced_destructor((c)->collection.destructor_data, \ |
144 (c)->collection.store_pointer ? (*((void **) (e))) : (e)) |
158 (c)->collection.store_pointer ? (*((void **) (e))) : (e)) |
145 |
159 |
148 * Invokes all available destructor functions for a specific element. |
162 * Invokes all available destructor functions for a specific element. |
149 * |
163 * |
150 * Usually only used by collection implementations. There should be no need |
164 * Usually only used by collection implementations. There should be no need |
151 * to invoke this macro manually. |
165 * to invoke this macro manually. |
152 * |
166 * |
153 * @param c the collection |
167 * When the collection stores pointers, those pointers are directly passed |
154 * @param e the element |
168 * to the destructor. Otherwise, a pointer to the element is passed. |
|
169 * |
|
170 * @param c a pointer to a struct that contains #CX_COLLECTION_BASE |
|
171 * @param e the element (the type is @c void* or @c void** depending on context) |
155 */ |
172 */ |
156 #define cx_invoke_destructor(c, e) \ |
173 #define cx_invoke_destructor(c, e) \ |
157 if ((c)->collection.simple_destructor) cx_invoke_simple_destructor(c,e); \ |
174 if ((c)->collection.simple_destructor) cx_invoke_simple_destructor(c,e); \ |
158 if ((c)->collection.advanced_destructor) cx_invoke_advanced_destructor(c,e) |
175 if ((c)->collection.advanced_destructor) cx_invoke_advanced_destructor(c,e) |
159 |
176 |