|
Teuchos Package Browser (Single Doxygen Collection) Version of the Day
|
00001 // @HEADER 00002 // *********************************************************************** 00003 // 00004 // Teuchos: Common Tools Package 00005 // Copyright (2004) Sandia Corporation 00006 // 00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00008 // license for use of this work by or on behalf of the U.S. Government. 00009 // 00010 // This library is free software; you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as 00012 // published by the Free Software Foundation; either version 2.1 of the 00013 // License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 // USA 00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00025 // 00026 // *********************************************************************** 00027 // @HEADER 00028 00029 #include "Teuchos_dyn_cast.hpp" 00030 #include "Teuchos_CommandLineProcessor.hpp" 00031 #include "Teuchos_GlobalMPISession.hpp" 00032 #include "Teuchos_Version.hpp" 00033 00034 class A { public: virtual ~A(){} }; 00035 class B : public A { public: void f(bool verbose) { if(verbose) std::cout << "\nB::f() called!\n"; } }; 00036 class C : public A {}; 00037 00038 int main( int argc, char* argv[] ) 00039 { 00040 00041 using Teuchos::CommandLineProcessor; 00042 00043 bool verbose = true; 00044 00045 Teuchos::GlobalMPISession mpiSession(&argc, &argv); 00046 00047 try { 00048 00049 // Read options from the commandline 00050 CommandLineProcessor clp(false); // Don't throw exceptions 00051 clp.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." ); 00052 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv); 00053 if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return; 00054 00055 if(verbose) std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl; 00056 00057 if(verbose) std::cout 00058 << "\n*******************************************" 00059 << "\n*** Basic test of Teuchos::dyn_cast<>() ***" 00060 << "\n*******************************************\n"; 00061 B b; 00062 A &a = b; 00063 try { 00064 if(verbose) std::cout << "\nTrying: dynamic_cast<C&>(a); [Should throw a std::bad_cast std::exception with very bad error message]\n"; 00065 dynamic_cast<C&>(a); 00066 } 00067 catch( const std::bad_cast &e ) { 00068 if(verbose) std::cout << "\nCaught std::bad_cast std::exception e where e.what() = \"" << e.what() << "\"\n"; 00069 } 00070 try { 00071 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"; 00072 Teuchos::dyn_cast<C>(a); 00073 } 00074 catch( const std::bad_cast &e ) { 00075 if(verbose) std::cout << "\nCaught std::bad_cast std::exception e where e.what() = \"" << e.what() << "\"\n"; 00076 } 00077 if(verbose) std::cout << "\nTrying: Teuchos::dyn_cast<B>(a).f(); [Should succeed and print \"B::f() called\"]\n"; 00078 Teuchos::dyn_cast<B>(a).f(verbose); 00079 if(verbose) std::cout << "\nAll tests check out!\n"; 00080 } 00081 catch( const std::exception &excpt ) { 00082 if(verbose) 00083 std::cerr << "*** Caught standard std::exception : " << excpt.what() << std::endl; 00084 return 1; 00085 } 00086 catch( ... ) { 00087 if(verbose) 00088 std::cerr << "*** Caught an unknown std::exception\n"; 00089 return 1; 00090 } 00091 return 0; 00092 }
1.7.4