Teuchos Package Browser (Single Doxygen Collection)
Version of the Day
core
test
dyn_cast
core/test/dyn_cast/cxx_main.cpp
Go to the documentation of this file.
1
// @HEADER
2
// ***********************************************************************
3
//
4
// Teuchos: Common Tools Package
5
// Copyright (2004) Sandia Corporation
6
//
7
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8
// license for use of this work by or on behalf of the U.S. Government.
9
//
10
// Redistribution and use in source and binary forms, with or without
11
// modification, are permitted provided that the following conditions are
12
// met:
13
//
14
// 1. Redistributions of source code must retain the above copyright
15
// notice, this list of conditions and the following disclaimer.
16
//
17
// 2. Redistributions in binary form must reproduce the above copyright
18
// notice, this list of conditions and the following disclaimer in the
19
// documentation and/or other materials provided with the distribution.
20
//
21
// 3. Neither the name of the Corporation nor the names of the
22
// contributors may be used to endorse or promote products derived from
23
// this software without specific prior written permission.
24
//
25
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
//
37
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38
//
39
// ***********************************************************************
40
// @HEADER
41
42
#include "
Teuchos_dyn_cast.hpp
"
43
#include "
Teuchos_CommandLineProcessor.hpp
"
44
#include "
Teuchos_GlobalMPISession.hpp
"
45
#include "
Teuchos_Version.hpp
"
46
47
class
A
{
public
:
virtual
~A
(){} };
48
class
B
:
public
A
{
public
:
void
f
(
bool
verbose) {
if
(verbose) std::cout <<
"\nB::f() called!\n"
; } };
49
class
C
:
public
A
{};
50
51
int
main
(
int
argc,
char
* argv[] )
52
{
53
54
using
Teuchos::CommandLineProcessor
;
55
56
bool
verbose =
true
;
57
58
Teuchos::GlobalMPISession
mpiSession(&argc, &argv);
59
60
try
{
61
62
// Read options from the commandline
63
CommandLineProcessor clp(
false
);
// Don't throw exceptions
64
clp.setOption(
"verbose"
,
"quiet"
, &verbose,
"Set if output is printed or not."
);
65
CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
66
if
( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL )
return
parse_return;
67
68
if
(verbose) std::cout <<
Teuchos::Teuchos_Version
() << std::endl << std::endl;
69
70
if
(verbose) std::cout
71
<<
"\n*******************************************"
72
<<
"\n*** Basic test of Teuchos::dyn_cast<>() ***"
73
<<
"\n*******************************************\n"
;
74
B
b;
75
A
&a = b;
76
try
{
77
if
(verbose) std::cout <<
"\nTrying: dynamic_cast<C&>(a); [Should throw a std::bad_cast std::exception with very bad error message]\n"
;
78
C
& c_from_an_a = dynamic_cast<C&>(a);
79
(void) c_from_an_a;
// forestall "unused variable" compiler warnings
80
}
81
catch
(
const
std::bad_cast &e ) {
82
if
(verbose) std::cout <<
"\nCaught std::bad_cast std::exception e where e.what() = \""
<< e.what() <<
"\"\n"
;
83
}
84
try
{
85
if
(verbose) std::cout <<
"\nTrying: Teuchos::dyn_cast<C>(a); [Should throw a std::bad_cast std::exception with a very good error message]\n"
;
86
Teuchos::dyn_cast<C>(a);
87
}
88
catch
(
const
std::bad_cast &e ) {
89
if
(verbose) std::cout <<
"\nCaught std::bad_cast std::exception e where e.what() = \""
<< e.what() <<
"\"\n"
;
90
}
91
if
(verbose) std::cout <<
"\nTrying: Teuchos::dyn_cast<B>(a).f(); [Should succeed and print \"B::f() called\"]\n"
;
92
Teuchos::dyn_cast<B>(a).f(verbose);
93
if
(verbose) std::cout <<
"\nAll tests check out!\n"
;
94
}
95
catch
(
const
std::exception &excpt ) {
96
if
(verbose)
97
std::cerr <<
"*** Caught standard std::exception : "
<< excpt.what() << std::endl;
98
return
1;
99
}
100
catch
( ... ) {
101
if
(verbose)
102
std::cerr <<
"*** Caught an unknown std::exception\n"
;
103
return
1;
104
}
105
return
0;
106
}
C
Definition:
PackageC.cpp:3
Teuchos::GlobalMPISession
Initialize, finalize, and query the global MPI session.
Definition:
Teuchos_GlobalMPISession.hpp:113
main
int main(int argc, char *argv[])
Definition:
core/test/dyn_cast/cxx_main.cpp:51
B
Definition:
PackageB.cpp:3
Teuchos_Version.hpp
Teuchos_dyn_cast.hpp
Teuchos_CommandLineProcessor.hpp
Basic command line parser for input from (argc,argv[])
Teuchos_GlobalMPISession.hpp
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
A
Definition:
PackageA.cpp:3
Teuchos::Teuchos_Version
std::string Teuchos_Version()
Definition:
Teuchos_Version.hpp:54
A::~A
virtual ~A()
Definition:
core/test/dyn_cast/cxx_main.cpp:47
Teuchos::CommandLineProcessor
Class that helps parse command line input arguments from (argc,argv[]) and set options.
Definition:
Teuchos_CommandLineProcessor.hpp:76
B::f
void f(bool verbose)
Definition:
core/test/dyn_cast/cxx_main.cpp:48
Generated by
1.8.16