#include "Teuchos_oblackholestream.hpp"
#include "Teuchos_StandardCatchMacros.hpp"
#include "Teuchos_Assert.hpp"
#include "Teuchos_getConst.hpp"
#include "Teuchos_Version.hpp"
#ifdef HAVE_TEUCHOS_BOOST
# include "Teuchos_RCPBoostSharedPtrConversions.hpp"
#endif
#include "TestClasses.hpp"
#define SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS
#define SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS_PRINT
int main( int argc, char* argv[] ) {
using Teuchos::deallocFunctorDelete;
using Teuchos::deallocFunctorHandleDelete;
using Teuchos::null;
using Teuchos::rcpFromRef;
using Teuchos::inOutArg;
using Teuchos::rcp_implicit_cast;
using Teuchos::rcp_const_cast;
using Teuchos::rcp_static_cast;
using Teuchos::rcp_dynamic_cast;
using Teuchos::set_extra_data;
using Teuchos::get_extra_data;
using Teuchos::get_nonconst_extra_data;
using Teuchos::get_optional_extra_data;
using Teuchos::get_optional_nonconst_extra_data;
using Teuchos::get_dealloc;
using Teuchos::get_nonconst_dealloc;
using Teuchos::get_optional_dealloc;
using Teuchos::get_optional_nonconst_dealloc;
using Teuchos::rcpWithEmbeddedObj;
using Teuchos::rcpWithEmbeddedObjPreDestroy;
using Teuchos::rcpWithEmbeddedObjPostDestroy;
using Teuchos::getEmbeddedObj;
using Teuchos::getNonconstEmbeddedObj;
bool success = true;
bool createCircRefs = false;
std::ostream &out = ( procRank == 0 ? std::cout : blackhole );
try {
CommandLineProcessor clp(false);
clp.setOption( "create-circ-refs", "no-create-circ-refs", &createCircRefs,
"Set if output is printed or not." );
CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
out << "\nEnd Result: TEST FAILED" << std::endl;
return parse_return;
}
blackhole << "\nThis should not print anywhere.\n";
out << std::endl << Teuchos::Teuchos_Version() << std::endl;
out << "\nTesting basic RCP functionality ...\n";
RCP<A> a_ptr1 = rcp(new C);
out << "\na_ptr1 = " << a_ptr1 << "\n";
RCP<D> d_ptr1 = rcp(new E);
{
const RCP<const A> ca_ptr1 = rcp_const_cast<const A>(a_ptr1);
const RCP<const D> cd_ptr1 = rcp_const_cast<const D>(d_ptr1);
#ifdef SHOW_RUN_TIME_ERROR_1
const RCP<A> a_ptr2 = a_ptr1.get();
#endif
a_ptr1 = rcp_const_cast<A>(ca_ptr1.assert_not_null());
#ifdef SHOW_COMPILE_TIME_ERRORS
ca_ptr1 = ca_ptr1;
ca_ptr1->A_g();
#endif
const RCP<const B1> cb1_ptr1 = rcp_dynamic_cast<const B1>(ca_ptr1);
const RCP<C>
c_ptr1 = rcp_const_cast<C>(rcp_dynamic_cast<const C>(ca_ptr1));
const RCP<const E>
ce_ptr1 = rcp_static_cast<const E>(cd_ptr1);
#ifdef SHOW_COMPILE_TIME_ERRORS
rcp_dynamic_cast<const E>( cd_ptr1 )->E_f();
#endif
#ifndef _INTEL
#ifdef TEUCHOS_DEBUG
try {
return -1;
}
catch( const std::logic_error &excpt )
{}
#endif
try {
rcp_dynamic_cast<B1>( rcp(new B2), true );
return -1;
}
catch( const std::bad_cast &excpt )
{}
#endif
delete d_ptr1.release().get();
#ifdef SHOW_RUN_TIME_ERROR_2
#endif
#ifdef SHOW_MEMORY_LEAK_1
a_ptr1.release();
#endif
}
a_ptr1 = rcp(new A);
a_ptr1 = rcp(new B1);
a_ptr1 = rcp(new B2);
a_ptr1 = rcp(new C);
d_ptr1 = rcp(new D);
d_ptr1 = rcp(new E);
C c;
a_ptr1 = rcp(&c);
#ifndef SHOW_RUN_TIME_ERROR_3
a_ptr1.release();
#endif
E e;
d_ptr1 = rcp(&e);
#ifndef SHOW_RUN_TIME_ERROR_4
d_ptr1.release();
#endif
#ifdef SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS
C *c_ptr5 = new C;
#ifdef SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS_PRINT
const void *c_ptr5_base = dynamic_cast<void*>(c_ptr5);
out << "\nSize of C = " << sizeof(C) << std::endl;
out << "Base address of object of type C = " << dynamic_cast<void*>(c_ptr5) << std::endl;
out << "Offset to address of object of type C = " << ((long int)c_ptr5 - (long int)c_ptr5_base) << std::endl;
out << "Offset of B1 object in object of type C = " << ((long int)static_cast<B1*>(c_ptr5) - (long int)c_ptr5_base) << std::endl;
out << "Offset of B2 object in object of type C = " << ((long int)static_cast<B2*>(c_ptr5) - (long int)c_ptr5_base) << std::endl;
out << "Offset of A object in object of type C = " << ((long int)static_cast<A*>(c_ptr5) - (long int)c_ptr5_base) << std::endl;
#endif
A *a_rptr5 = c_ptr5;
a_ptr1 = rcp(a_rptr5);
a_ptr1 = null;
#endif
a_ptr1 = rcpWithDealloc( new C, DeallocDelete<C>() );
get_dealloc<DeallocDelete<C> >(a_ptr1);
get_nonconst_dealloc<DeallocDelete<C> >(a_ptr1);
set_extra_data( int(-5), "int", inOutArg(a_ptr1) );
set_extra_data( rcp(new B1), "B1", inOutArg(a_ptr1) );
{
RCP<A> a_ptr = rcpWithEmbeddedObj(new C,int(-5));
const int intRtn1 = getEmbeddedObj<C,int>(a_ptr);
getNonconstEmbeddedObj<C,int>(a_ptr) = -4;
const int intRtn2 = getEmbeddedObj<C,int>(a_ptr);
}
{
RCP<A> a_ptr = rcpWithEmbeddedObjPreDestroy(new C,int(-5));
const int intRtn1 = getEmbeddedObj<C,int>(a_ptr);
getNonconstEmbeddedObj<C,int>(a_ptr) = -4;
const int intRtn2 = getEmbeddedObj<C,int>(a_ptr);
}
{
RCP<A> a_ptr = rcpWithEmbeddedObjPostDestroy(new C,int(-5));
const int intRtn1 = getEmbeddedObj<C,int>(a_ptr);
getNonconstEmbeddedObj<C,int>(a_ptr) = -4;
const int intRtn2 = getEmbeddedObj<C,int>(a_ptr);
}
int a_f_return = -2;
set_extra_data( rcp(new Get_A_f_return(&*a_ptr1,&a_f_return)),
"a_f_return", inOutArg(a_ptr1), Teuchos::PRE_DESTROY );
a_ptr1 = null;
d_ptr1 = null;
a_ptr1 = rcpWithDealloc( new C, deallocFunctorDelete<A>(deallocA) );
a_ptr1 = null;
a_ptr1 = rcpWithDealloc( new C, deallocFunctorHandleDelete<A>(deallocHandleA) );
a_ptr1 = null;
#ifdef TEUCHOS_DEBUG
if (createCircRefs) {
out << "\nCreate a circular reference that will cause a memory leak! ...\n";
# if !defined(HAVE_TEUCHOS_DEBUG_RCP_NODE_TRACING)
Teuchos::RCPNodeTracer::setTracingActiveRCPNodes(true);
# endif
RCP<A> a = rcp(new A());
RCP<C> c2 = rcp(new C());
a->set_C(c2);
c2->set_A(a);
}
#endif
#ifndef TEUCHOS_DEBUG
out << "\nTesting using RCP to wrap an undefined opaque object (no TNT) ...\n";
{
RCP<UndefinedType> op_ptr =
rcpWithDeallocUndef (createOpaque (),
deallocFunctorHandleDelete<UndefinedType> (destroyOpaque),
true);
}
#endif
out << "\nTesting using RCP to wrap an undefined opaque object (with TNT) ...\n";
{
RCP<UndefinedType2> op_ptr = rcpWithDeallocUndef( createOpaque2(),
deallocFunctorHandleDelete<UndefinedType2>(destroyOpaque2) );
}
#ifdef HAVE_TEUCHOS_BOOST
out << "\nTesting basic RCP compatibility with boost::shared_ptr ...\n";
boost::shared_ptr<A> a_sptr1(new C());
RCP<A> a_rsptr1 = rcp(a_sptr1);
boost::shared_ptr<A> a_sptr2 = shared_pointer(a_rsptr1);
RCP<A> a_rsptr2 = rcp(a_sptr2);
boost::shared_ptr<A> a_sptr3 = shared_pointer(a_rsptr2);
out << "\nCompatibility with boost::shared_ptr passed ...\n";
#endif
out << "\nAll tests for RCP seem to check out!\n";
}
try {
if (createCircRefs) {
out << "\nPrinting the active nodes just to see them!\n";
#if defined(TEUCHOS_DEBUG) && !defined(HAVE_TEUCHOS_DEBUG_RCP_NODE_TRACING)
#endif
}
}
if(success)
out << "\nEnd Result: TEST PASSED" << std::endl;
return ( success ? 0 : 1 );
}
Basic command line parser for input from (argc,argv[])
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Reference-counted pointer class and non-member templated function implementations.
Class that helps parse command line input arguments from (argc,argv[]) and set options.
Policy class for deallocator that uses delete to delete a pointer which is used by RCP.
Initialize, finalize, and query the global MPI session.
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
static int numActiveRCPNodes()
Print the number of active RCPNode objects currently being tracked.
static void printActiveRCPNodes(std::ostream &out)
Print the list of currently active RCP nodes.
Smart reference counting pointer class for automatic garbage collection.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call.
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
This macro is checks that to numbers are equal and if not then throws an exception with a good error ...
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
const T & getConst(T &t)
Return a constant reference to an object given a non-const reference.
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.