Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
core/test/dyn_cast/cxx_main.cpp
Go to the documentation of this file.
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 #include "Teuchos_dyn_cast.hpp"
13 #include "Teuchos_Version.hpp"
14 
15 class A { public: virtual ~A(){} };
16 class B : public A { public: void f(bool verbose) { if(verbose) std::cout << "\nB::f() called!\n"; } };
17 class C : public A {};
18 
19 int main( int argc, char* argv[] )
20 {
21 
23 
24  bool verbose = true;
25 
26  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
27 
28  try {
29 
30  // Read options from the commandline
31  CommandLineProcessor clp(false); // Don't throw exceptions
32  clp.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
33  CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
34  if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return;
35 
36  if(verbose) std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
37 
38  if(verbose) std::cout
39  << "\n*******************************************"
40  << "\n*** Basic test of Teuchos::dyn_cast<>() ***"
41  << "\n*******************************************\n";
42  B b;
43  A &a = b;
44  try {
45  if(verbose) std::cout << "\nTrying: dynamic_cast<C&>(a); [Should throw a std::bad_cast std::exception with very bad error message]\n";
46  C& c_from_an_a = dynamic_cast<C&>(a);
47  (void) c_from_an_a; // forestall "unused variable" compiler warnings
48  }
49  catch( const std::bad_cast &e ) {
50  if(verbose) std::cout << "\nCaught std::bad_cast std::exception e where e.what() = \"" << e.what() << "\"\n";
51  }
52  try {
53  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";
54  Teuchos::dyn_cast<C>(a);
55  }
56  catch( const std::bad_cast &e ) {
57  if(verbose) std::cout << "\nCaught std::bad_cast std::exception e where e.what() = \"" << e.what() << "\"\n";
58  }
59  if(verbose) std::cout << "\nTrying: Teuchos::dyn_cast<B>(a).f(); [Should succeed and print \"B::f() called\"]\n";
60  Teuchos::dyn_cast<B>(a).f(verbose);
61  if(verbose) std::cout << "\nAll tests check out!\n";
62  }
63  catch( const std::exception &excpt ) {
64  if(verbose)
65  std::cerr << "*** Caught standard std::exception : " << excpt.what() << std::endl;
66  return 1;
67  }
68  catch( ... ) {
69  if(verbose)
70  std::cerr << "*** Caught an unknown std::exception\n";
71  return 1;
72  }
73  return 0;
74 }
void f(bool verbose)
Initialize, finalize, and query the global MPI session.
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...
std::string Teuchos_Version()
int main(int argc, char *argv[])
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Basic command line parser for input from (argc,argv[])
Class that helps parse command line input arguments from (argc,argv[]) and set options.