Thu, 23 Feb 2023 21:42:46 +0100
split cxMapRemove() to cxMapRemoveAndGet()
src/cx/map.h | file | annotate | diff | comparison | revisions | |
tests/test_map.cpp | file | annotate | diff | comparison | revisions |
--- a/src/cx/map.h Thu Feb 23 18:58:15 2023 +0100 +++ b/src/cx/map.h Thu Feb 23 21:42:46 2023 +0100 @@ -112,7 +112,7 @@ /** * Removes an element. */ - __attribute__((__nonnull__, __warn_unused_result__)) + __attribute__((__nonnull__)) void *(*remove)( CxMap *map, CxHashKey key @@ -259,13 +259,38 @@ /** * Removes a key/value-pair from the map by using the key. * + * If this map is storing pointers, you should make sure that the map + * is not the last location where this pointer is stored. + * Otherwise, use cxMapRemoveAndGet() to retrieve the pointer while + * removing it from the map. + * * @param map the map * @param key the key - * @return if this map is storing pointers, the removed value, \c NULL otherwise + * @see cxMapRemoveAndGet() + */ +__attribute__((__nonnull__)) +static inline void cxMapRemove( + CxMap *map, + CxHashKey key +) { + (void) map->cl->remove(map, key); +} + +/** + * Removes a key/value-pair from the map by using the key. + * + * This function should only be used when the map is storing pointers, + * in order to retrieve the pointer you are about to remove. + * In any other case, cxMapRemove() is sufficient. + * + * @param map the map + * @param key the key + * @return the stored pointer or \c NULL if either the key is not present + * in the map or the map is not storing pointers * @see cxMapStorePointers() */ __attribute__((__nonnull__, __warn_unused_result__)) -static inline void *cxMapRemove( +static inline void *cxMapRemoveAndGet( CxMap *map, CxHashKey key ) {
--- a/tests/test_map.cpp Thu Feb 23 18:58:15 2023 +0100 +++ b/tests/test_map.cpp Thu Feb 23 21:42:46 2023 +0100 @@ -163,7 +163,7 @@ } else { // execute a remove and verify that the removed element was returned (or nullptr) auto found = refmap.find(op.key); - auto removed = cxMapRemove(map, key); + auto removed = cxMapRemoveAndGet(map, key); if (found == refmap.end()) { EXPECT_EQ(removed, nullptr); } else { @@ -307,8 +307,7 @@ EXPECT_NE(s3p, &s3); // remove a string - auto r = cxMapRemove(map, cx_hash_key_str("s2")); - EXPECT_EQ(r, nullptr); + cxMapRemove(map, cx_hash_key_str("s2")); // iterate auto ref = std::vector{s5.ptr, s3.ptr, s4.ptr};