src/cx/collection.h

Mon, 18 Dec 2023 14:14:47 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Dec 2023 14:14:47 +0100
changeset 759
475335643af4
parent 712
cff828e156a9
child 786
b0ebb3d88407
permissions
-rw-r--r--

increase version number to 3.1

remove per-file version information
from Doxygen output

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

mercurial