// @HEADER
// *****************************************************************************
// Teuchos: Common Tools Package
//
// Copyright 2004 NTESS and the Teuchos contributors.
// SPDX-License-Identifier: BSD-3-Clause
// *****************************************************************************
// @HEADER
#ifndef TEUCHOS_DYN_CAST_HPP
#define TEUCHOS_DYN_CAST_HPP
#include "Teuchos_TypeNameTraits.hpp"
namespace Teuchos {
/** \brief Exception class for bad cast.
\ingroup teuchos_language_support_grp
We create this class so that we may throw a bad_cast when appropriate and
still use the TEUCHOS_TEST_FOR_EXCEPTION macro. We recommend users try to catch a
bad_cast.
*/
class m_bad_cast : public std::bad_cast {
std::string msg;
public:
explicit m_bad_cast(const std::string& what_arg ) : msg(what_arg) {}
virtual ~m_bad_cast() throw() {}
virtual const char* what() const throw() { return msg.data(); }
};
// Throw m_bad_cast for below function
TEUCHOSCORE_LIB_DLL_EXPORT void dyn_cast_throw_exception(
const std::string &T_from,
const std::string &T_from_concr,
const std::string &T_to
);
/** \brief Dynamic casting utility function meant to replace
* dynamic_cast by throwing a better documented error
* message.
*
* \ingroup teuchos_language_support_grp
*
* Existing uses of the built-in dynamic_cast() operator
* such as:
\code
C &c = dynamic_cast(a);
\endcode
* are easily replaced as:
\code
C &c = dyn_cast(a);
\endcode
* and that is it. One could write a perl script to do this
* automatically.
*
* This utility function is designed to cast an object reference of
* type T_From to type T_To and if the cast fails at
* runtime then an std::exception (derived from std::bad_cast) is
* thrown that contains a very good error message.
*
* Consider the following class hierarchy:
\code
class A {};
class B : public A {};
class C : public A {};
\endcode
*
* Now consider the following program:
\code
int main( int argc, char* argv[] ) {
B b;
A &a = b;
try {
std::cout << "\nTrying: dynamic_cast(a);\n";
dynamic_cast(a);
}
catch( const std::bad_cast &e ) {
std::cout << "\nCaught std::bad_cast std::exception e where e.what() = \"" << e.what() << "\"\n";
}
try {
std::cout << "\nTrying: Teuchos::dyn_cast(a);\n";
Teuchos::dyn_cast(a);
}
catch( const std::bad_cast &e ) {
std::cout << "\nCaught std::bad_cast std::exception e where e.what() = \"" << e.what() << "\"\n";
}
return 0;
}
\endcode
* The above program will print something that looks like (compiled
* with g++ for example):
\verbatim
Trying: dynamic_cast(a);
Caught std::bad_cast std::exception e where e.what() = "St8bad_cast"
Trying: Teuchos::dyn_cast(a);
Caught std::bad_cast std::exception e where e.what() = "../../../../packages/teuchos/src/Teuchos_dyn_cast.cpp:46: true:
dyn_cast<1C>(1A) : Error, the object with the concrete type '1B' (passed in through the interface type '1A') does
not support the interface '1C' and the dynamic cast failed!"
\endverbatim
* The above program shows that the standard implementation of
* dynamic_cast() does not return any useful debugging
* information at all but the templated function
* Teuchos::dyn_cast() returns all kinds of useful
* information. The generated error message gives the type of the
* interface that the object was passed in as (i.e. A), what
* the actual concrete type of the object is (i.e. B) and
* what type is trying to be dynamically casted to (i.e. C).
* This type of information is extremely valuable when trying to track
* down these type of runtime dynamic casting errors. In some cases
* (such as with gdb), debuggers do not even give the type of
* concrete object so this function is very important on these
* platforms. In many cases, a debugger does not even need to be
* opened to diagnose what the problem is and how to fix it.
*
* Note that this function is inlined and does not incur any
* significant runtime performance penalty over the raw
* dynamic_cast() operator.
*/
template
inline
T_To& dyn_cast(T_From &from)
{
T_To *to_ = dynamic_cast(&from);
if(!to_)
dyn_cast_throw_exception(
TypeNameTraits::name(),
typeName(from),
TypeNameTraits::name()
);
return *to_;
}
} // namespace Teuchos
#endif // TEUCHOS_DYN_CAST_HPP