src/cx/map.h

changeset 658
56c62780582e
parent 630
ac5e7f789048
child 659
4a06fd63909a
     1.1 --- a/src/cx/map.h	Mon Feb 20 19:55:42 2023 +0100
     1.2 +++ b/src/cx/map.h	Thu Feb 23 18:58:15 2023 +0100
     1.3 @@ -63,7 +63,15 @@
     1.4      CxAllocator *allocator;
     1.5      /** The number of elements currently stored. */
     1.6      size_t size;
     1.7 -    // TODO: elemsize + a flag if values shall be copied to the map
     1.8 +    /**
     1.9 +     * The size of an element.
    1.10 +     */
    1.11 +    size_t itemsize;
    1.12 +    /**
    1.13 +     * True, if this map shall store pointers instead
    1.14 +     * of copies of objects.
    1.15 +     */
    1.16 +    bool store_pointers;
    1.17  };
    1.18  
    1.19  /**
    1.20 @@ -161,6 +169,38 @@
    1.21      void *value;
    1.22  };
    1.23  
    1.24 +/**
    1.25 + * Advises the map to store copies of the objects (default mode of operation).
    1.26 + *
    1.27 + * Retrieving objects from this map will yield pointers to the copies stored
    1.28 + * within this list.
    1.29 + *
    1.30 + * @param map the map
    1.31 + * @see cxMapStorePointers()
    1.32 + */
    1.33 +__attribute__((__nonnull__))
    1.34 +static inline void cxMapStoreObjects(CxMap *map) {
    1.35 +    map->store_pointers = false;
    1.36 +}
    1.37 +
    1.38 +/**
    1.39 + * Advises the map to only store pointers to the objects.
    1.40 + *
    1.41 + * Retrieving objects from this list will yield the original pointers stored.
    1.42 + *
    1.43 + * @note This function forcibly sets the element size to the size of a pointer.
    1.44 + * Invoking this function on a non-empty map that already stores copies of
    1.45 + * objects is undefined.
    1.46 + *
    1.47 + * @param map the map
    1.48 + * @see cxMapStoreObjects()
    1.49 + */
    1.50 +__attribute__((__nonnull__))
    1.51 +static inline void cxMapStorePointers(CxMap *map) {
    1.52 +    map->store_pointers = true;
    1.53 +    map->itemsize = sizeof(void *);
    1.54 +}
    1.55 +
    1.56  
    1.57  /**
    1.58   * Deallocates the memory of the specified map.
    1.59 @@ -221,7 +261,8 @@
    1.60   *
    1.61   * @param map the map
    1.62   * @param key the key
    1.63 - * @return the removed value
    1.64 + * @return if this map is storing pointers, the removed value, \c NULL otherwise
    1.65 + * @see cxMapStorePointers()
    1.66   */
    1.67  __attribute__((__nonnull__, __warn_unused_result__))
    1.68  static inline void *cxMapRemove(

mercurial