diff options
author | Jacques Lucke <jacques@blender.org> | 2020-05-04 18:12:12 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2020-05-04 18:12:23 +0300 |
commit | df16c23832abe423da5e8fee9bd1cabb643e65de (patch) | |
tree | 41c08f1ae284136d31b0c7db9ae554f44fa847f4 /source/blender | |
parent | ba43c2a040fb92435308c497043f480cb03a3705 (diff) |
Add StringMap.LookupOrAdd and StringMap.LookupOrAddDefault
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenlib/BLI_string_map.hh | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_string_map.hh b/source/blender/blenlib/BLI_string_map.hh index ed23ea3aaa0..caa7e16d1f3 100644 --- a/source/blender/blenlib/BLI_string_map.hh +++ b/source/blender/blenlib/BLI_string_map.hh @@ -342,6 +342,27 @@ template<typename T, typename Allocator = GuardedAllocator> class StringMap { } /** + * Return the value that corresponds to the given key. + * If it does not exist yet, create and insert it first. + */ + template<typename CreateValueF> T &lookup_or_add(StringRef key, const CreateValueF &create_value) + { + return *this->add_or_modify( + key, + [&](T *value) { return new (value) T(create_value()); }, + [](T *value) { return value; }); + } + + /** + * Return the value that corresponds to the given key. + * If it does not exist yet, insert a new default constructed value and return that. + */ + T &lookup_or_add_default(StringRef key) + { + return this->lookup_or_add(key, []() { return T(); }); + } + + /** * Do a linear search over all items to find a key for a value. */ StringRefNull find_key_for_value(const T &value) const |