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 |
1.1 --- a/src/cx/map.h Thu Feb 23 18:58:15 2023 +0100 1.2 +++ b/src/cx/map.h Thu Feb 23 21:42:46 2023 +0100 1.3 @@ -112,7 +112,7 @@ 1.4 /** 1.5 * Removes an element. 1.6 */ 1.7 - __attribute__((__nonnull__, __warn_unused_result__)) 1.8 + __attribute__((__nonnull__)) 1.9 void *(*remove)( 1.10 CxMap *map, 1.11 CxHashKey key 1.12 @@ -259,13 +259,38 @@ 1.13 /** 1.14 * Removes a key/value-pair from the map by using the key. 1.15 * 1.16 + * If this map is storing pointers, you should make sure that the map 1.17 + * is not the last location where this pointer is stored. 1.18 + * Otherwise, use cxMapRemoveAndGet() to retrieve the pointer while 1.19 + * removing it from the map. 1.20 + * 1.21 * @param map the map 1.22 * @param key the key 1.23 - * @return if this map is storing pointers, the removed value, \c NULL otherwise 1.24 + * @see cxMapRemoveAndGet() 1.25 + */ 1.26 +__attribute__((__nonnull__)) 1.27 +static inline void cxMapRemove( 1.28 + CxMap *map, 1.29 + CxHashKey key 1.30 +) { 1.31 + (void) map->cl->remove(map, key); 1.32 +} 1.33 + 1.34 +/** 1.35 + * Removes a key/value-pair from the map by using the key. 1.36 + * 1.37 + * This function should only be used when the map is storing pointers, 1.38 + * in order to retrieve the pointer you are about to remove. 1.39 + * In any other case, cxMapRemove() is sufficient. 1.40 + * 1.41 + * @param map the map 1.42 + * @param key the key 1.43 + * @return the stored pointer or \c NULL if either the key is not present 1.44 + * in the map or the map is not storing pointers 1.45 * @see cxMapStorePointers() 1.46 */ 1.47 __attribute__((__nonnull__, __warn_unused_result__)) 1.48 -static inline void *cxMapRemove( 1.49 +static inline void *cxMapRemoveAndGet( 1.50 CxMap *map, 1.51 CxHashKey key 1.52 ) {
2.1 --- a/tests/test_map.cpp Thu Feb 23 18:58:15 2023 +0100 2.2 +++ b/tests/test_map.cpp Thu Feb 23 21:42:46 2023 +0100 2.3 @@ -163,7 +163,7 @@ 2.4 } else { 2.5 // execute a remove and verify that the removed element was returned (or nullptr) 2.6 auto found = refmap.find(op.key); 2.7 - auto removed = cxMapRemove(map, key); 2.8 + auto removed = cxMapRemoveAndGet(map, key); 2.9 if (found == refmap.end()) { 2.10 EXPECT_EQ(removed, nullptr); 2.11 } else { 2.12 @@ -307,8 +307,7 @@ 2.13 EXPECT_NE(s3p, &s3); 2.14 2.15 // remove a string 2.16 - auto r = cxMapRemove(map, cx_hash_key_str("s2")); 2.17 - EXPECT_EQ(r, nullptr); 2.18 + cxMapRemove(map, cx_hash_key_str("s2")); 2.19 2.20 // iterate 2.21 auto ref = std::vector{s5.ptr, s3.ptr, s4.ptr};