Introduce safe_as_a
gcc/ 2014-08-18 David Malcolm <dmalcolm@redhat.com> * is-a.h (template<T, U> safe_as_a <U *p>) New function. From-SVN: r214117
This commit is contained in:
parent
c41b94cef2
commit
26b3538ba2
2 changed files with 28 additions and 0 deletions
|
@ -1,3 +1,7 @@
|
|||
2014-08-18 David Malcolm <dmalcolm@redhat.com>
|
||||
|
||||
* is-a.h (template<T, U> safe_as_a <U *p>) New function.
|
||||
|
||||
2014-08-18 Jan Hubicka <hubicka@ucw.cz>
|
||||
|
||||
* ipa-visibility.c (update_visibility_by_resolution_info): Do no turn UNDEF
|
||||
|
|
24
gcc/is-a.h
24
gcc/is-a.h
|
@ -46,6 +46,14 @@ TYPE as_a <TYPE> (pointer)
|
|||
|
||||
do_something_with (as_a <cgraph_node *> *ptr);
|
||||
|
||||
TYPE safe_as_a <TYPE> (pointer)
|
||||
|
||||
Like as_a <TYPE> (pointer), but where pointer could be NULL. This
|
||||
adds a check against NULL where the regular is_a_helper hook for TYPE
|
||||
assumes non-NULL.
|
||||
|
||||
do_something_with (safe_as_a <cgraph_node *> *ptr);
|
||||
|
||||
TYPE dyn_cast <TYPE> (pointer)
|
||||
|
||||
Converts pointer to TYPE if and only if "is_a <TYPE> pointer". Otherwise,
|
||||
|
@ -185,6 +193,22 @@ as_a (U *p)
|
|||
return is_a_helper <T>::cast (p);
|
||||
}
|
||||
|
||||
/* Similar to as_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 T
|
||||
safe_as_a (U *p)
|
||||
{
|
||||
if (p)
|
||||
{
|
||||
gcc_checking_assert (is_a <T> (p));
|
||||
return is_a_helper <T>::cast (p);
|
||||
}
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* A generic checked conversion from a base type U to a derived type T. See
|
||||
the discussion above for when to use this function. */
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue