diff options
author | Simon Marchi <simon.marchi@efficios.com> | 2023-05-18 15:05:56 -0400 |
---|---|---|
committer | Simon Marchi <simon.marchi@efficios.com> | 2023-05-24 14:53:53 -0400 |
commit | 175ee55a229d9a02ae702f8818e2e022faae3132 (patch) | |
tree | bd8c484fc43fbaff56dbf971530fc86cf866962c /gdbsupport/gdb-checked-static-cast.h | |
parent | cbd9efbbc0414b71d47020958b5fbeff25504f15 (diff) | |
download | binutils-175ee55a229d9a02ae702f8818e2e022faae3132.zip binutils-175ee55a229d9a02ae702f8818e2e022faae3132.tar.gz binutils-175ee55a229d9a02ae702f8818e2e022faae3132.tar.bz2 |
gdbsupport: add support for references to checked_static_cast
Add a checked_static_cast overload that works with references. A bad
dynamic cast with references throws std::bad_cast, it would be possible
to implement the new overload based on that, but it seemed simpler to
just piggy back off the existing function.
I found some potential uses of this new overload in amd-dbgapi-target.c,
update them to illustrate the use of the new overload. To build
amd-dbgapi-target.c, on needs the amd-dbgapi library, which I don't
expect many people to have. But I have it, and it builds fine here. I
did test the new overload by making a purposely bad cast and it did
catch it.
Change-Id: Id6b6a7db09fe3b4aa43cddb60575ff5f46761e96
Reviewed-By: Lancelot SIX <lsix@lancelotsix.com>
Reviewed-By: Andrew Burgess <aburgess@redhat.com>
Diffstat (limited to 'gdbsupport/gdb-checked-static-cast.h')
-rw-r--r-- | gdbsupport/gdb-checked-static-cast.h | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/gdbsupport/gdb-checked-static-cast.h b/gdbsupport/gdb-checked-static-cast.h index bc75244..e9127b9 100644 --- a/gdbsupport/gdb-checked-static-cast.h +++ b/gdbsupport/gdb-checked-static-cast.h @@ -66,6 +66,22 @@ checked_static_cast (V *v) return result; } +/* Same as the above, but to cast from a reference type to another. */ + +template<typename T, typename V, typename = gdb::Requires<std::is_reference<T>>> +T +checked_static_cast (V &v) +{ + static_assert (std::is_reference<T>::value, "target must be a reference type"); + + using T_no_R = typename std::remove_reference<T>::type; + using T_P = typename std::add_pointer<T_no_R>::type; + + using V_no_R = typename std::remove_reference<V>::type; + + return *checked_static_cast<T_P, V_no_R> (&v); +} + } #endif /* COMMON_GDB_CHECKED_DYNAMIC_CAST_H */ |