Drake
is_dynamic_castable.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <string>
5 
6 #include <gtest/gtest.h>
7 
9 
10 namespace drake {
11 
34 template <typename ToType, typename FromType>
35 ::testing::AssertionResult is_dynamic_castable(const FromType* ptr) {
36  if (ptr == nullptr) {
37  const std::string from_name{NiceTypeName::Get<FromType>()};
38  const std::string to_name{NiceTypeName::Get<ToType>()};
39  return ::testing::AssertionFailure()
40  << "is_dynamic_castable<" << to_name << ">(" << from_name << "* ptr)"
41  << " failed because ptr was already nullptr.";
42  }
43  if (dynamic_cast<const ToType* const>(ptr) == nullptr) {
44  const std::string from_name{NiceTypeName::Get<FromType>()};
45  const std::string to_name{NiceTypeName::Get<ToType>()};
46  const std::string dynamic_name{NiceTypeName::Get(*ptr)};
47  return ::testing::AssertionFailure()
48  << "is_dynamic_castable<" << to_name << ">(" << from_name << "* ptr)"
49  << " failed because ptr is of dynamic type " << dynamic_name << ".";
50  }
51  return ::testing::AssertionSuccess();
52 }
53 
58 template <typename ToType, typename FromType>
59 ::testing::AssertionResult is_dynamic_castable(std::shared_ptr<FromType> ptr) {
60  return is_dynamic_castable<ToType, FromType>(ptr.get());
61 }
62 
67 template <typename ToType, typename FromType>
68 ::testing::AssertionResult is_dynamic_castable(
69  const std::unique_ptr<FromType>& ptr) {
70  return is_dynamic_castable<ToType, FromType>(ptr.get());
71 }
72 
73 } // namespace drake
Definition: automotive_demo.cc:88
::testing::AssertionResult is_dynamic_castable(const FromType *ptr)
Checks if ptr, a pointer to FromType class, can be safely converted to a pointer to ToType class...
Definition: is_dynamic_castable.h:35
static const std::string & Get()
Attempts to return a nicely demangled and canonicalized type name that is the same on all platforms...
Definition: nice_type_name.h:53