aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Biener <rguenther@suse.de>2023-04-21 13:38:53 +0200
committerRichard Biener <rguenther@suse.de>2023-04-21 15:53:21 +0200
commit2e047c00e983b6d9037906feac3a215ef829c82f (patch)
tree379027430395b2c9f50a4921510a22283b1cc96a
parentc39cdd9e654540f74cd2478019c40f1611554a44 (diff)
downloadgcc-2e047c00e983b6d9037906feac3a215ef829c82f.zip
gcc-2e047c00e983b6d9037906feac3a215ef829c82f.tar.gz
gcc-2e047c00e983b6d9037906feac3a215ef829c82f.tar.bz2
Add safe_is_a
The following adds safe_is_a, an is_a check handling nullptr gracefully. * is-a.h (safe_is_a): New.
-rw-r--r--gcc/is-a.h13
1 files changed, 13 insertions, 0 deletions
diff --git a/gcc/is-a.h b/gcc/is-a.h
index b535524..0a697cf 100644
--- a/gcc/is-a.h
+++ b/gcc/is-a.h
@@ -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. */