diff -r 55cc3b373c5e -r d402fead3386 src/cx/list.h --- a/src/cx/list.h Wed Jan 25 19:19:29 2023 +0100 +++ b/src/cx/list.h Thu Jan 26 20:59:36 2023 +0100 @@ -65,7 +65,11 @@ /** * The list class definition. */ - cx_list_class *cl; + cx_list_class const *cl; + /** + * The actual implementation in case the list class is delegating. + */ + cx_list_class const *climpl; /** * The allocator to use. */ @@ -122,6 +126,16 @@ void (*destructor)(struct cx_list_s *list); /** + * Member function for inserting a single elements. + * Implementors SHOULD see to performant implementations for corner cases. + */ + int (*insert_element)( + struct cx_list_s *list, + size_t index, + void const *data + ); + + /** * Member function for inserting multiple elements. * Implementors SHOULD see to performant implementations for corner cases. */ @@ -198,6 +212,43 @@ typedef struct cx_list_s CxList; /** + * Advises the list to store copies of the objects (default mode of operation). + * + * Retrieving objects from this list will yield pointers to the copies stored + * within this list. + * + * @param list the list + * @see cxListStorePointers() + */ +__attribute__((__nonnull__)) +void cxListStoreObjects(CxList *list); + +/** + * Advises the list to only store pointers to the objects. + * + * Retrieving objects from this list will yield the original pointers stored. + * + * @note This function forcibly sets the element size to the size of a pointer. + * Invoking this function on a non-empty list that already stores copies of + * objects is undefined. + * + * @param list the list + * @see cxListStoreObjects() + */ +__attribute__((__nonnull__)) +void cxListStorePointers(CxList *list); + +/** + * Returns true, if this list is storing pointers instead of the actual data. + * + * @param list + * @return + * @see cxListStorePointers() + */ +__attribute__((__nonnull__)) +bool cxListIsStoringPointers(CxList *list); + +/** * Adds an item to the end of the list. * * @param list the list @@ -210,7 +261,7 @@ CxList *list, void const *elem ) { - return list->cl->insert_array(list, list->size, elem, 1) != 1; + return list->cl->insert_element(list, list->size, elem); } /** @@ -221,6 +272,9 @@ * If there is not enough memory to add all elements, the returned value is * less than \p n. * + * If this list is storing pointers instead of objects \p array is expected to + * be an array of pointers. + * * @param list the list * @param array a pointer to the elements to add * @param n the number of elements to add @@ -254,7 +308,7 @@ size_t index, void const *elem ) { - return list->cl->insert_array(list, index, elem, 1) != 1; + return list->cl->insert_element(list, index, elem); } /** @@ -267,6 +321,9 @@ * If there is not enough memory to add all elements, the returned value is * less than \p n. * + * If this list is storing pointers instead of objects \p array is expected to + * be an array of pointers. + * * @param list the list * @param index the index where to add the elements * @param array a pointer to the elements to add