Teuchos - Trilinos Tools Package
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
core
src
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
14
#include "
Teuchos_TypeNameTraits.hpp
"
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_)
145
dyn_cast_throw_exception
(
146
TypeNameTraits<T_From>::name
(),
147
typeName
(from),
148
TypeNameTraits<T_To>::name
()
149
);
150
return
*to_;
151
}
152
153
154
}
// namespace Teuchos
155
156
157
#endif
// TEUCHOS_DYN_CAST_HPP
Teuchos_TypeNameTraits.hpp
Defines basic traits returning the name of a type in a portable and readable way.
Teuchos::TypeNameTraits::name
static std::string name()
Definition
Teuchos_TypeNameTraits.hpp:56
Teuchos::typeName
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
Definition
Teuchos_TypeNameTraits.hpp:83
Teuchos::dyn_cast
T_To & dyn_cast(T_From &from)
Dynamic casting utility function meant to replace dynamic_cast<T&> by throwing a better documented er...
Definition
Teuchos_dyn_cast.hpp:141
Teuchos
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
Teuchos::dyn_cast_throw_exception
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)
Definition
Teuchos_dyn_cast.cpp:17
Generated by
1.17.0