add single instance mode
[uwplayer.git] / ucx / cx / list.h
index 95dec26..25e2ad5 100644 (file)
 #define UCX_LIST_H
 
 #include "common.h"
-#include "allocator.h"
-#include "iterator.h"
+#include "collection.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-#ifndef CX_STORE_POINTERS
-/**
- * Special constant used for creating collections that are storing pointers.
- */
-#define CX_STORE_POINTERS 0
-#endif
-
-/**
- * A comparator function comparing two list elements.
- */
-typedef int(*CxListComparator)(
-        void const *left,
-        void const *right
-);
-
 /**
  * List class type.
  */
@@ -69,6 +53,7 @@ typedef struct cx_list_class_s cx_list_class;
  * Structure for holding the base data of a list.
  */
 struct cx_list_s {
+    CX_COLLECTION_MEMBERS
     /**
      * The list class definition.
      */
@@ -77,50 +62,6 @@ struct cx_list_s {
      * The actual implementation in case the list class is delegating.
      */
     cx_list_class const *climpl;
-    /**
-     * The allocator to use.
-     */
-    CxAllocator const *allocator;
-    /**
-     * The comparator function for the elements.
-     */
-    CxListComparator cmpfunc;
-    /**
-     * The size of each element (payload only).
-     */
-    size_t itemsize;
-    /**
-     * The size of the list (number of currently stored elements).
-     */
-    size_t size;
-    /**
-     * The capacity of the list (maximum number of elements).
-     */
-    size_t capacity;
-    union {
-        /**
-         * An optional simple destructor for the list contents that admits the free() interface.
-         *
-         * @remark Set content_destructor_type to #CX_DESTRUCTOR_SIMPLE.
-         *
-         * @attention Read the documentation of the particular list implementation
-         * whether this destructor shall only destroy the contents or also free the memory.
-         */
-        cx_destructor_func simple_destructor;
-        /**
-         * An optional advanced destructor for the list contents providing additional data.
-         *
-         * @remark Set content_destructor_type to #CX_DESTRUCTOR_ADVANCED.
-         *
-         * @attention Read the documentation of the particular list implementation
-         * whether this destructor shall only destroy the contents or also free the memory.
-         */
-        cx_advanced_destructor advanced_destructor;
-    };
-    /**
-     * The type of destructor to use.
-     */
-    enum cx_destructor_type content_destructor_type;
 };
 
 /**
@@ -129,11 +70,14 @@ struct cx_list_s {
 struct cx_list_class_s {
     /**
      * Destructor function.
+     *
+     * Implementations SHALL invoke the content destructor functions if provided
+     * and SHALL deallocate the list memory, if an allocator is provided.
      */
     void (*destructor)(struct cx_list_s *list);
 
     /**
-     * Member function for inserting a single elements.
+     * Member function for inserting a single element.
      * Implementors SHOULD see to performant implementations for corner cases.
      */
     int (*insert_element)(
@@ -195,13 +139,13 @@ struct cx_list_class_s {
     /**
      * Member function for finding an element.
      */
-    size_t (*find)(
+    ssize_t (*find)(
             struct cx_list_s const *list,
             void const *elem
     );
 
     /**
-     * Member function for sorting the list in place.
+     * Member function for sorting the list in-place.
      */
     void (*sort)(struct cx_list_s *list);
 
@@ -234,51 +178,6 @@ struct cx_list_class_s {
 typedef struct cx_list_s CxList;
 
 /**
- * Invokes the configured destructor function for a specific element.
- *
- * Usually only used by list implementations. There should be no need
- * to invoke this function manually.
- *
- * @param list the list
- * @param elem the element
- */
-__attribute__((__nonnull__))
-void cx_list_invoke_destructor(
-        struct cx_list_s const *list,
-        void *elem
-);
-
-/**
- * Invokes the simple destructor function for a specific element.
- *
- * Usually only used by list implementations. There should be no need
- * to invoke this function manually.
- *
- * @param list the list
- * @param elem the element
- */
-__attribute__((__nonnull__))
-void cx_list_invoke_simple_destructor(
-        struct cx_list_s const *list,
-        void *elem
-);
-
-/**
- * Invokes the advanced destructor function for a specific element.
- *
- * Usually only used by list implementations. There should be no need
- * to invoke this function manually.
- *
- * @param list the list
- * @param elem the element
- */
-__attribute__((__nonnull__))
-void cx_list_invoke_advanced_destructor(
-        struct cx_list_s const *list,
-        void *elem
-);
-
-/**
  * 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
@@ -309,11 +208,24 @@ void cxListStorePointers(CxList *list);
  * Returns true, if this list is storing pointers instead of the actual data.
  *
  * @param list
- * @return
+ * @return true, if this list is storing pointers
  * @see cxListStorePointers()
  */
 __attribute__((__nonnull__))
-bool cxListIsStoringPointers(CxList const *list);
+static inline bool cxListIsStoringPointers(CxList const *list) {
+    return list->store_pointer;
+}
+
+/**
+ * Returns the number of elements currently stored in the list.
+ *
+ * @param list the list
+ * @return the number of currently stored elements
+ */
+__attribute__((__nonnull__))
+static inline size_t cxListSize(CxList const *list) {
+    return list->size;
+}
 
 /**
  * Adds an item to the end of the list.
@@ -660,10 +572,11 @@ static inline CxMutIterator cxListMutBackwardsIterator(CxList *list) {
  *
  * @param list the list
  * @param elem the element to find
- * @return the index of the element or \c (size+1) if the element is not found
+ * @return the index of the element or a negative
+ * value when the element is not found
  */
 __attribute__((__nonnull__))
-static inline size_t cxListFind(
+static inline ssize_t cxListFind(
         CxList const *list,
         void const *elem
 ) {
@@ -671,7 +584,7 @@ static inline size_t cxListFind(
 }
 
 /**
- * Sorts the list in place.
+ * Sorts the list in-place.
  *
  * \remark The underlying sort algorithm is implementation defined.
  *
@@ -722,6 +635,14 @@ int cxListCompare(
 __attribute__((__nonnull__))
 void cxListDestroy(CxList *list);
 
+/**
+ * A shared instance of an empty list.
+ *
+ * Writing to that list is undefined.
+ */
+extern CxList * const cxEmptyList;
+
+
 #ifdef __cplusplus
 } // extern "C"
 #endif