src/cx/iterator.h

Sat, 22 Jan 2022 19:10:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 22 Jan 2022 19:10:04 +0100
changeset 497
b182a8b8a1af
parent 496
1a07e24801a9
child 500
eb9e7bd40a8e
permissions
-rw-r--r--

pointer type must be explicit in cx_foreach macro

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    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
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    28 /**
    29  * \file iterator.h
    30  * \brief Interface for iterator implementations.
    31  * \author Mike Becker
    32  * \author Olaf Wintermann
    33  * \version 3.0
    34  * \copyright 2-Clause BSD License
    35  */
    37 #ifndef UCX_ITERATOR_H
    38 #define UCX_ITERATOR_H
    40 #include "common.h"
    42 /**
    43  * Iterator value type.
    44  * An iterator points to a certain element in an (possibly unbounded) chain of elements.
    45  * Iterators that are based on collections (which have a defined "first" element), are supposed
    46  * to be "position-aware", which means that they keep track of the current index within the collection.
    47  *
    48  * @note Objects that are pointed to by an iterator are mutable through that iterator. However, if the
    49  * iterator is based on a collection and the underlying collection is mutated (elements added or removed),
    50  * the iterator becomes invalid (regardless of what cxIteratorValid() returns) and MUST be re-obtained
    51  * from the collection.
    52  */
    53 typedef struct cx_iterator_s CxIterator;
    55 /**
    56  * Internal iterator struct - use CxIterator.
    57  */
    58 struct cx_iterator_s {
    59     /**
    60      * True iff the iterator points to valid data.
    61      */
    62     bool (*valid)(CxIterator const *) __attribute__ ((__nonnull__));
    64     /**
    65      * Returns a pointer to the current element.
    66      */
    67     void *(*current)(CxIterator const *) __attribute__ ((__nonnull__));
    69     /**
    70      * Advances the iterator.
    71      */
    72     void (*next)(CxIterator *) __attribute__ ((__nonnull__));
    74     /**
    75      * Handle for the current element, if required.
    76      */
    77     void *elem_handle;
    79     /**
    80      * Handle for the source collection, if any.
    81      */
    82     void *src_handle;
    84     /**
    85      * If the iterator is position-aware, contains the index of the element in the underlying collection.
    86      * Otherwise, this field is usually uninitialized.
    87      */
    88     size_t index;
    90     /**
    91      * Users may set this to true, if the current element shall be removed from the underlying collection
    92      * when the iterator advances.
    93      * Has no effect on iterators that are not based on a collection.
    94      */
    95     bool remove;
    96 };
    98 /**
    99  * Checks if the iterator points to valid data.
   100  *
   101  * This is especially false for past-the-end iterators.
   102  *
   103  * @param iter a pointer to the iterator
   104  * @return true iff the iterator points to valid data
   105  */
   106 __attribute__ ((__nonnull__))
   107 static inline bool cxIteratorValid(CxIterator const *iter) {
   108     return iter->valid(iter);
   109 }
   111 /**
   112  * Returns a pointer to the current element.
   113  *
   114  * The behavior is undefined if this iterator is invalid.
   115  *
   116  * @param iter a pointer to the iterator
   117  * @return a pointer to the current element
   118  */
   119 __attribute__ ((__nonnull__))
   120 static inline void *cxIteratorCurrent(CxIterator const *iter) {
   121     return iter->current(iter);
   122 }
   124 /**
   125  * Advances the iterator to the next element.
   126  *
   127  * @param iter a pointer to the iterator
   128  */
   129 __attribute__ ((__nonnull__))
   130 static inline void cxIteratorNext(CxIterator *iter) {
   131     iter->next(iter);
   132 }
   134 /**
   135  * Loops over an iterator.
   136  * @param type the type of the elements
   137  * @param elem the name of the iteration variable
   138  * @param iter the iterator
   139  */
   140 #define cx_foreach(type, elem, iter) \
   141 for (type elem; cxIteratorValid(&iter) && (elem = cxIteratorCurrent(&iter)) != NULL ; cxIteratorNext(&iter)) // NOLINT(bugprone-macro-parentheses)
   143 #endif /* UCX_ITERATOR_H */

mercurial