Sat, 08 Jun 2024 20:08:09 +0200
add cxIteratorRef() macro
universe@677 | 1 | /* |
universe@677 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@677 | 3 | * |
universe@677 | 4 | * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. |
universe@677 | 5 | * |
universe@677 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@677 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@677 | 8 | * |
universe@677 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@677 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@677 | 11 | * |
universe@677 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@677 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@677 | 14 | * documentation and/or other materials provided with the distribution. |
universe@677 | 15 | * |
universe@677 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@677 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@677 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@677 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@677 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@677 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@677 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@677 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@677 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@677 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@677 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@677 | 27 | */ |
universe@677 | 28 | /** |
universe@677 | 29 | * \file collection.h |
universe@677 | 30 | * \brief Common definitions for various collection implementations. |
universe@677 | 31 | * \author Mike Becker |
universe@677 | 32 | * \author Olaf Wintermann |
universe@677 | 33 | * \copyright 2-Clause BSD License |
universe@677 | 34 | */ |
universe@677 | 35 | |
universe@677 | 36 | #ifndef UCX_COLLECTION_H |
universe@677 | 37 | #define UCX_COLLECTION_H |
universe@677 | 38 | |
universe@677 | 39 | #include "allocator.h" |
universe@677 | 40 | #include "iterator.h" |
universe@854 | 41 | #include "compare.h" |
universe@677 | 42 | |
universe@677 | 43 | #ifdef __cplusplus |
universe@677 | 44 | extern "C" { |
universe@677 | 45 | #endif |
universe@677 | 46 | |
universe@677 | 47 | /** |
universe@677 | 48 | * Special constant used for creating collections that are storing pointers. |
universe@677 | 49 | */ |
universe@677 | 50 | #define CX_STORE_POINTERS 0 |
universe@677 | 51 | |
universe@677 | 52 | /** |
universe@854 | 53 | * Base attributes of a collection. |
universe@677 | 54 | */ |
universe@854 | 55 | struct cx_collection_s { |
universe@854 | 56 | /** |
universe@854 | 57 | * The allocator to use. |
universe@854 | 58 | */ |
universe@854 | 59 | CxAllocator const *allocator; |
universe@854 | 60 | /** |
universe@854 | 61 | * The comparator function for the elements. |
universe@854 | 62 | */ |
universe@854 | 63 | cx_compare_func cmpfunc; |
universe@854 | 64 | /** |
universe@854 | 65 | * The size of each element. |
universe@854 | 66 | */ |
universe@855 | 67 | size_t elem_size; |
universe@854 | 68 | /** |
universe@854 | 69 | * The number of currently stored elements. |
universe@854 | 70 | */ |
universe@854 | 71 | size_t size; |
universe@854 | 72 | /** |
universe@854 | 73 | * An optional simple destructor for the collection's elements. |
universe@854 | 74 | * |
universe@854 | 75 | * @attention Read the documentation of the particular collection implementation |
universe@854 | 76 | * whether this destructor shall only destroy the contents or also free the memory. |
universe@854 | 77 | */ |
universe@854 | 78 | cx_destructor_func simple_destructor; |
universe@854 | 79 | /** |
universe@854 | 80 | * An optional advanced destructor for the collection's elements. |
universe@854 | 81 | * |
universe@854 | 82 | * @attention Read the documentation of the particular collection implementation |
universe@854 | 83 | * whether this destructor shall only destroy the contents or also free the memory. |
universe@854 | 84 | */ |
universe@854 | 85 | cx_destructor_func2 advanced_destructor; |
universe@854 | 86 | /** |
universe@854 | 87 | * The pointer to additional data that is passed to the advanced destructor. |
universe@854 | 88 | */ |
universe@854 | 89 | void *destructor_data; |
universe@854 | 90 | /** |
universe@854 | 91 | * Indicates if this list is supposed to store pointers |
universe@854 | 92 | * instead of copies of the actual objects. |
universe@854 | 93 | */ |
universe@854 | 94 | bool store_pointer; |
universe@854 | 95 | }; |
universe@677 | 96 | |
universe@677 | 97 | /** |
universe@677 | 98 | * Use this macro to declare common members for a collection structure. |
universe@677 | 99 | */ |
universe@856 | 100 | #define CX_COLLECTION_BASE struct cx_collection_s collection |
universe@677 | 101 | |
universe@677 | 102 | /** |
universe@857 | 103 | * Sets a simple destructor function for this collection. |
universe@857 | 104 | * |
universe@857 | 105 | * @param c the collection |
universe@857 | 106 | * @param destr the destructor function |
universe@857 | 107 | */ |
universe@857 | 108 | #define cxDefineDestructor(c, destr) \ |
universe@857 | 109 | (c)->collection.simple_destructor = (cx_destructor_func) destr |
universe@857 | 110 | |
universe@857 | 111 | /** |
universe@857 | 112 | * Sets a simple destructor function for this collection. |
universe@857 | 113 | * |
universe@857 | 114 | * @param c the collection |
universe@857 | 115 | * @param destr the destructor function |
universe@857 | 116 | */ |
universe@857 | 117 | #define cxDefineAdvancedDestructor(c, destr, data) \ |
universe@857 | 118 | (c)->collection.advanced_destructor = (cx_destructor_func2) destr; \ |
universe@857 | 119 | (c)->collection.destructor_data = data |
universe@857 | 120 | |
universe@857 | 121 | /** |
universe@677 | 122 | * Invokes the simple destructor function for a specific element. |
universe@677 | 123 | * |
universe@677 | 124 | * Usually only used by collection implementations. There should be no need |
universe@677 | 125 | * to invoke this macro manually. |
universe@677 | 126 | * |
universe@677 | 127 | * @param c the collection |
universe@677 | 128 | * @param e the element |
universe@677 | 129 | */ |
universe@677 | 130 | #define cx_invoke_simple_destructor(c, e) \ |
universe@856 | 131 | (c)->collection.simple_destructor((c)->collection.store_pointer ? (*((void **) (e))) : (e)) |
universe@677 | 132 | |
universe@677 | 133 | /** |
universe@677 | 134 | * Invokes the advanced destructor function for a specific element. |
universe@677 | 135 | * |
universe@677 | 136 | * Usually only used by collection implementations. There should be no need |
universe@677 | 137 | * to invoke this macro manually. |
universe@677 | 138 | * |
universe@677 | 139 | * @param c the collection |
universe@677 | 140 | * @param e the element |
universe@677 | 141 | */ |
universe@677 | 142 | #define cx_invoke_advanced_destructor(c, e) \ |
universe@856 | 143 | (c)->collection.advanced_destructor((c)->collection.destructor_data, \ |
universe@856 | 144 | (c)->collection.store_pointer ? (*((void **) (e))) : (e)) |
universe@677 | 145 | |
universe@677 | 146 | |
universe@712 | 147 | /** |
universe@712 | 148 | * Invokes all available destructor functions for a specific element. |
universe@712 | 149 | * |
universe@712 | 150 | * Usually only used by collection implementations. There should be no need |
universe@712 | 151 | * to invoke this macro manually. |
universe@712 | 152 | * |
universe@712 | 153 | * @param c the collection |
universe@712 | 154 | * @param e the element |
universe@712 | 155 | */ |
universe@677 | 156 | #define cx_invoke_destructor(c, e) \ |
universe@856 | 157 | if ((c)->collection.simple_destructor) cx_invoke_simple_destructor(c,e); \ |
universe@856 | 158 | if ((c)->collection.advanced_destructor) cx_invoke_advanced_destructor(c,e) |
universe@677 | 159 | |
universe@677 | 160 | #ifdef __cplusplus |
universe@677 | 161 | } // extern "C" |
universe@677 | 162 | #endif |
universe@677 | 163 | |
universe@677 | 164 | #endif // UCX_COLLECTION_H |