]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Add safe_is_a
authorRichard Biener <rguenther@suse.de>
Fri, 21 Apr 2023 11:38:53 +0000 (13:38 +0200)
committerRichard Biener <rguenther@suse.de>
Fri, 21 Apr 2023 13:53:21 +0000 (15:53 +0200)
The following adds safe_is_a, an is_a check handling nullptr
gracefully.

* is-a.h (safe_is_a): New.

gcc/is-a.h

index b5355242655a35fb5cf3465abb36c7677e75f5a7..0a697cf002a228197cfbe6ec83d8fea7fc1825c4 100644 (file)
@@ -232,6 +232,19 @@ is_a (U *p)
   return is_a_helper<T>::test (p);
 }
 
+/* Similar to is_a<>, but where the pointer can be NULL, even if
+   is_a_helper<T> doesn't check for NULL.  */
+
+template <typename T, typename U>
+inline bool
+safe_is_a (U *p)
+{
+  if (p)
+    return is_a_helper <T>::test (p);
+  else
+    return false;
+}
+
 /* A generic conversion from a base type U to a derived type T.  See the
    discussion above for when to use this function.  */