Browse Source

detect dangling references in MapFindPtr and utility helpers

swarmer 1 year ago
parent
commit
b009ee7c48
2 changed files with 15 additions and 11 deletions
  1. 8 6
      util/generic/mapfindptr.h
  2. 7 5
      util/generic/utility.h

+ 8 - 6
util/generic/mapfindptr.h

@@ -1,5 +1,7 @@
 #pragma once
 
+#include <util/system/compiler.h>
+
 #include <type_traits>
 #include <utility>
 
@@ -12,14 +14,14 @@ if (T* value = MapFindPtr(myMap, someKey) {
 */
 
 template <class Map, class K>
-inline auto MapFindPtr(Map& map, const K& key) {
+inline auto MapFindPtr(Map& map Y_LIFETIME_BOUND, const K& key) {
     auto i = map.find(key);
 
     return (i == map.end() ? nullptr : &i->second);
 }
 
 template <class Map, class K>
-inline auto MapFindPtr(const Map& map, const K& key) {
+inline auto MapFindPtr(const Map& map Y_LIFETIME_BOUND, const K& key) {
     auto i = map.find(key);
 
     return (i == map.end() ? nullptr : &i->second);
@@ -29,12 +31,12 @@ inline auto MapFindPtr(const Map& map, const K& key) {
 template <class Derived>
 struct TMapOps {
     template <class K>
-    inline auto FindPtr(const K& key) {
+    inline auto FindPtr(const K& key) Y_LIFETIME_BOUND {
         return MapFindPtr(static_cast<Derived&>(*this), key);
     }
 
     template <class K>
-    inline auto FindPtr(const K& key) const {
+    inline auto FindPtr(const K& key) const Y_LIFETIME_BOUND {
         return MapFindPtr(static_cast<const Derived&>(*this), key);
     }
 
@@ -45,7 +47,7 @@ struct TMapOps {
     }
 
     template <class K, class V>
-    inline const V& ValueRef(const K& key, V& defaultValue) const {
+    inline const V& ValueRef(const K& key, V& defaultValue Y_LIFETIME_BOUND) const Y_LIFETIME_BOUND {
         static_assert(std::is_same<std::remove_const_t<V>, typename Derived::mapped_type>::value, "Passed default value must have the same type as the underlying map's mapped_type.");
 
         if (auto found = FindPtr(key)) {
@@ -55,5 +57,5 @@ struct TMapOps {
     }
 
     template <class K, class V>
-    inline const V& ValueRef(const K& key, V&& defaultValue) const = delete;
+    inline const V& ValueRef(const K& key, V&& defaultValue Y_LIFETIME_BOUND) const Y_LIFETIME_BOUND = delete;
 };

+ 7 - 5
util/generic/utility.h

@@ -2,31 +2,33 @@
 
 #include "typetraits.h"
 
+#include <util/system/compiler.h>
+
 #include <cstring>
 
 template <class T>
-static constexpr const T& Min(const T& l, const T& r) {
+static constexpr const T& Min(const T& l Y_LIFETIME_BOUND, const T& r Y_LIFETIME_BOUND) {
     return r < l ? r : l;
 }
 
 template <typename T, typename... Args>
-static constexpr const T& Min(const T& a, const T& b, const Args&... args) {
+static constexpr const T& Min(const T& a Y_LIFETIME_BOUND, const T& b Y_LIFETIME_BOUND, const Args&... args Y_LIFETIME_BOUND) {
     return Min(a, Min(b, args...));
 }
 
 template <class T>
-static constexpr const T& Max(const T& l, const T& r) {
+static constexpr const T& Max(const T& l Y_LIFETIME_BOUND, const T& r Y_LIFETIME_BOUND) {
     return l < r ? r : l;
 }
 
 template <typename T, typename... Args>
-static constexpr const T& Max(const T& a, const T& b, const Args&... args) {
+static constexpr const T& Max(const T& a Y_LIFETIME_BOUND, const T& b Y_LIFETIME_BOUND, const Args&... args Y_LIFETIME_BOUND) {
     return Max(a, Max(b, args...));
 }
 
 // replace with http://en.cppreference.com/w/cpp/algorithm/clamp in c++17
 template <class T>
-constexpr const T& ClampVal(const T& val, const T& min, const T& max) {
+constexpr const T& ClampVal(const T& val Y_LIFETIME_BOUND, const T& min Y_LIFETIME_BOUND, const T& max Y_LIFETIME_BOUND) {
     return val < min ? min : (max < val ? max : val);
 }