src/cx/list.h

changeset 641
d402fead3386
parent 640
55cc3b373c5e
child 647
2e6e9d9f2159
     1.1 --- a/src/cx/list.h	Wed Jan 25 19:19:29 2023 +0100
     1.2 +++ b/src/cx/list.h	Thu Jan 26 20:59:36 2023 +0100
     1.3 @@ -65,7 +65,11 @@
     1.4      /**
     1.5       * The list class definition.
     1.6       */
     1.7 -    cx_list_class *cl;
     1.8 +    cx_list_class const *cl;
     1.9 +    /**
    1.10 +     * The actual implementation in case the list class is delegating.
    1.11 +     */
    1.12 +    cx_list_class const *climpl;
    1.13      /**
    1.14       * The allocator to use.
    1.15       */
    1.16 @@ -122,6 +126,16 @@
    1.17      void (*destructor)(struct cx_list_s *list);
    1.18  
    1.19      /**
    1.20 +     * Member function for inserting a single elements.
    1.21 +     * Implementors SHOULD see to performant implementations for corner cases.
    1.22 +     */
    1.23 +    int (*insert_element)(
    1.24 +            struct cx_list_s *list,
    1.25 +            size_t index,
    1.26 +            void const *data
    1.27 +    );
    1.28 +
    1.29 +    /**
    1.30       * Member function for inserting multiple elements.
    1.31       * Implementors SHOULD see to performant implementations for corner cases.
    1.32       */
    1.33 @@ -198,6 +212,43 @@
    1.34  typedef struct cx_list_s CxList;
    1.35  
    1.36  /**
    1.37 + * Advises the list to store copies of the objects (default mode of operation).
    1.38 + *
    1.39 + * Retrieving objects from this list will yield pointers to the copies stored
    1.40 + * within this list.
    1.41 + *
    1.42 + * @param list the list
    1.43 + * @see cxListStorePointers()
    1.44 + */
    1.45 +__attribute__((__nonnull__))
    1.46 +void cxListStoreObjects(CxList *list);
    1.47 +
    1.48 +/**
    1.49 + * Advises the list to only store pointers to the objects.
    1.50 + *
    1.51 + * Retrieving objects from this list will yield the original pointers stored.
    1.52 + *
    1.53 + * @note This function forcibly sets the element size to the size of a pointer.
    1.54 + * Invoking this function on a non-empty list that already stores copies of
    1.55 + * objects is undefined.
    1.56 + *
    1.57 + * @param list the list
    1.58 + * @see cxListStoreObjects()
    1.59 + */
    1.60 +__attribute__((__nonnull__))
    1.61 +void cxListStorePointers(CxList *list);
    1.62 +
    1.63 +/**
    1.64 + * Returns true, if this list is storing pointers instead of the actual data.
    1.65 + *
    1.66 + * @param list
    1.67 + * @return
    1.68 + * @see cxListStorePointers()
    1.69 + */
    1.70 +__attribute__((__nonnull__))
    1.71 +bool cxListIsStoringPointers(CxList *list);
    1.72 +
    1.73 +/**
    1.74   * Adds an item to the end of the list.
    1.75   *
    1.76   * @param list the list
    1.77 @@ -210,7 +261,7 @@
    1.78          CxList *list,
    1.79          void const *elem
    1.80  ) {
    1.81 -    return list->cl->insert_array(list, list->size, elem, 1) != 1;
    1.82 +    return list->cl->insert_element(list, list->size, elem);
    1.83  }
    1.84  
    1.85  /**
    1.86 @@ -221,6 +272,9 @@
    1.87   * If there is not enough memory to add all elements, the returned value is
    1.88   * less than \p n.
    1.89   *
    1.90 + * If this list is storing pointers instead of objects \p array is expected to
    1.91 + * be an array of pointers.
    1.92 + *
    1.93   * @param list the list
    1.94   * @param array a pointer to the elements to add
    1.95   * @param n the number of elements to add
    1.96 @@ -254,7 +308,7 @@
    1.97          size_t index,
    1.98          void const *elem
    1.99  ) {
   1.100 -    return list->cl->insert_array(list, index, elem, 1) != 1;
   1.101 +    return list->cl->insert_element(list, index, elem);
   1.102  }
   1.103  
   1.104  /**
   1.105 @@ -267,6 +321,9 @@
   1.106   * If there is not enough memory to add all elements, the returned value is
   1.107   * less than \p n.
   1.108   *
   1.109 + * If this list is storing pointers instead of objects \p array is expected to
   1.110 + * be an array of pointers.
   1.111 + *
   1.112   * @param list the list
   1.113   * @param index the index where to add the elements
   1.114   * @param array a pointer to the elements to add

mercurial