Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_dyn_cast.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TEUCHOS_DYN_CAST_HPP
11 #define TEUCHOS_DYN_CAST_HPP
12 
13 
15 
16 
17 namespace Teuchos {
18 
19 
28 class m_bad_cast : public std::bad_cast {
29  std::string msg;
30 public:
31  explicit m_bad_cast(const std::string& what_arg ) : msg(what_arg) {}
32  virtual ~m_bad_cast() throw() {}
33  virtual const char* what() const throw() { return msg.data(); }
34 };
35 
36 
37 // Throw <tt>m_bad_cast</tt> for below function
38 TEUCHOSCORE_LIB_DLL_EXPORT void dyn_cast_throw_exception(
39  const std::string &T_from,
40  const std::string &T_from_concr,
41  const std::string &T_to
42  );
43 
44 
139 template <class T_To, class T_From>
140 inline
141 T_To& dyn_cast(T_From &from)
142 {
143  T_To *to_ = dynamic_cast<T_To*>(&from);
144  if(!to_)
147  typeName(from),
149  );
150  return *to_;
151 }
152 
153 
154 } // namespace Teuchos
155 
156 
157 #endif // TEUCHOS_DYN_CAST_HPP
T_To & dyn_cast(T_From &from)
Dynamic casting utility function meant to replace dynamic_cast&lt;T&amp;&gt; by throwing a better documented er...
Exception class for bad cast.
Default traits class that just returns typeid(T).name().
Defines basic traits returning the name of a type in a portable and readable way. ...
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
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)