summaryrefslogtreecommitdiff
path: root/runtime/safe_map.h
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/safe_map.h')
-rw-r--r--runtime/safe_map.h13
1 files changed, 13 insertions, 0 deletions
diff --git a/runtime/safe_map.h b/runtime/safe_map.h
index 0e5b503e26..49f80f31a8 100644
--- a/runtime/safe_map.h
+++ b/runtime/safe_map.h
@@ -19,6 +19,7 @@
#include <map>
#include <memory>
+#include <type_traits>
#include "base/allocator.h"
#include "base/logging.h"
@@ -124,6 +125,18 @@ class SafeMap {
return result.first;
}
+ template <typename CreateFn>
+ V GetOrCreate(const K& k, CreateFn create) {
+ static_assert(std::is_same<V, typename std::result_of<CreateFn()>::type>::value,
+ "Argument `create` should return a value of type V.");
+ auto lb = lower_bound(k);
+ if (lb != end() && !key_comp()(k, lb->first)) {
+ return lb->second;
+ }
+ auto it = PutBefore(lb, k, create());
+ return it->second;
+ }
+
bool Equals(const Self& rhs) const {
return map_ == rhs.map_;
}