51#ifdef HAVE_TEUCHOS_BOOST
67#define SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS
68#define SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS_PRINT
75int main(
int argc,
char* argv[] ) {
79 using Teuchos::deallocFunctorDelete;
80 using Teuchos::deallocFunctorHandleDelete;
83 using Teuchos::rcpFromRef;
84 using Teuchos::inOutArg;
86 using Teuchos::rcp_implicit_cast;
87 using Teuchos::rcp_const_cast;
88 using Teuchos::rcp_static_cast;
89 using Teuchos::rcp_dynamic_cast;
90 using Teuchos::set_extra_data;
91 using Teuchos::get_extra_data;
92 using Teuchos::get_nonconst_extra_data;
93 using Teuchos::get_optional_extra_data;
94 using Teuchos::get_optional_nonconst_extra_data;
95 using Teuchos::get_dealloc;
96 using Teuchos::get_nonconst_dealloc;
97 using Teuchos::get_optional_dealloc;
98 using Teuchos::get_optional_nonconst_dealloc;
99 using Teuchos::rcpWithEmbeddedObj;
100 using Teuchos::rcpWithEmbeddedObjPreDestroy;
101 using Teuchos::rcpWithEmbeddedObjPostDestroy;
102 using Teuchos::getEmbeddedObj;
103 using Teuchos::getNonconstEmbeddedObj;
108 bool createCircRefs =
false;
114 std::ostream &out = ( procRank == 0 ? std::cout : blackhole );
119 CommandLineProcessor clp(
false);
120 clp.setOption(
"create-circ-refs",
"no-create-circ-refs", &createCircRefs,
121 "Set if output is printed or not." );
122 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
123 if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
124 out <<
"\nEnd Result: TEST FAILED" << std::endl;
128 blackhole <<
"\nThis should not print anywhere.\n";
132 out <<
"\nTesting basic RCP functionality ...\n";
136 RCP<A> a_ptr1 = rcp(
new C);
137 out <<
"\na_ptr1 = " << a_ptr1 <<
"\n";
154 RCP<D> d_ptr1 = rcp(
new E);
164 const RCP<const A> ca_ptr1 = rcp_const_cast<const A>(a_ptr1);
171 const RCP<const D> cd_ptr1 = rcp_const_cast<const D>(d_ptr1);
176#ifdef SHOW_RUN_TIME_ERROR_1
179 const RCP<A> a_ptr2 = a_ptr1.get();
184 a_ptr1 = rcp_const_cast<A>(ca_ptr1.assert_not_null());
186#ifdef SHOW_COMPILE_TIME_ERRORS
212 const RCP<const B1> cb1_ptr1 = rcp_dynamic_cast<const B1>(ca_ptr1);
233 c_ptr1 = rcp_const_cast<C>(rcp_dynamic_cast<const C>(ca_ptr1));
241 ce_ptr1 = rcp_static_cast<const E>(cd_ptr1);
253#ifdef SHOW_COMPILE_TIME_ERRORS
255 rcp_dynamic_cast<const E>( cd_ptr1 )->E_f();
269 catch(
const std::logic_error &excpt )
277 rcp_dynamic_cast<B1>( rcp(
new B2),
true );
280 catch(
const std::bad_cast &excpt )
286 delete d_ptr1.release().get();
288#ifdef SHOW_RUN_TIME_ERROR_2
292#ifdef SHOW_MEMORY_LEAK_1
305 a_ptr1 = rcp(
new B1);
306 a_ptr1 = rcp(
new B2);
319#ifndef SHOW_RUN_TIME_ERROR_3
326#ifndef SHOW_RUN_TIME_ERROR_4
331#ifdef SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS
335#ifdef SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS_PRINT
336 const void *c_ptr5_base =
dynamic_cast<void*
>(c_ptr5);
337 out <<
"\nSize of C = " <<
sizeof(
C) << std::endl;
338 out <<
"Base address of object of type C = " <<
dynamic_cast<void*
>(c_ptr5) << std::endl;
339 out <<
"Offset to address of object of type C = " << ((
long int)c_ptr5 - (
long int)c_ptr5_base) << std::endl;
340 out <<
"Offset of B1 object in object of type C = " << ((
long int)
static_cast<B1*
>(c_ptr5) - (
long int)c_ptr5_base) << std::endl;
341 out <<
"Offset of B2 object in object of type C = " << ((
long int)
static_cast<B2*
>(c_ptr5) - (
long int)c_ptr5_base) << std::endl;
342 out <<
"Offset of A object in object of type C = " << ((
long int)
static_cast<A*
>(c_ptr5) - (
long int)c_ptr5_base) << std::endl;
345 a_ptr1 = rcp(a_rptr5);
350 a_ptr1 = rcpWithDealloc(
new C, DeallocDelete<C>() );
351 get_dealloc<DeallocDelete<C> >(a_ptr1);
352 get_nonconst_dealloc<DeallocDelete<C> >(a_ptr1);
361 set_extra_data(
int(-5),
"int", inOutArg(a_ptr1) );
364 set_extra_data( rcp(
new B1),
"B1", inOutArg(a_ptr1) );
376 RCP<A> a_ptr = rcpWithEmbeddedObj(
new C,
int(-5));
377 const int intRtn1 = getEmbeddedObj<C,int>(a_ptr);
379 getNonconstEmbeddedObj<C,int>(a_ptr) = -4;
380 const int intRtn2 = getEmbeddedObj<C,int>(a_ptr);
385 RCP<A> a_ptr = rcpWithEmbeddedObjPreDestroy(
new C,
int(-5));
386 const int intRtn1 = getEmbeddedObj<C,int>(a_ptr);
388 getNonconstEmbeddedObj<C,int>(a_ptr) = -4;
389 const int intRtn2 = getEmbeddedObj<C,int>(a_ptr);
394 RCP<A> a_ptr = rcpWithEmbeddedObjPostDestroy(
new C,
int(-5));
395 const int intRtn1 = getEmbeddedObj<C,int>(a_ptr);
397 getNonconstEmbeddedObj<C,int>(a_ptr) = -4;
398 const int intRtn2 = getEmbeddedObj<C,int>(a_ptr);
417 a_ptr1 = rcpWithDealloc(
new C, deallocFunctorDelete<A>(
deallocA) );
421 a_ptr1 = rcpWithDealloc(
new C, deallocFunctorHandleDelete<A>(
deallocHandleA) );
426 if (createCircRefs) {
427 out <<
"\nCreate a circular reference that will cause a memory leak! ...\n";
428# if !defined(HAVE_TEUCHOS_DEBUG_RCP_NODE_TRACING)
430 Teuchos::RCPNodeTracer::setTracingActiveRCPNodes(
true);
432 RCP<A> a = rcp(
new A());
433 RCP<C> c2 = rcp(
new C());
442 out <<
"\nTesting using RCP to wrap an undefined opaque object (no TNT) ...\n";
444 RCP<UndefinedType> op_ptr =
458 out <<
"\nTesting using RCP to wrap an undefined opaque object (with TNT) ...\n";
460 RCP<UndefinedType2> op_ptr = rcpWithDeallocUndef(
createOpaque2(),
468#ifdef HAVE_TEUCHOS_BOOST
470 out <<
"\nTesting basic RCP compatibility with boost::shared_ptr ...\n";
472 boost::shared_ptr<A> a_sptr1(
new C());
473 RCP<A> a_rsptr1 = rcp(a_sptr1);
477 boost::shared_ptr<A> a_sptr2 = shared_pointer(a_rsptr1);
480 RCP<A> a_rsptr2 = rcp(a_sptr2);
483 boost::shared_ptr<A> a_sptr3 = shared_pointer(a_rsptr2);
486 out <<
"\nCompatibility with boost::shared_ptr passed ...\n";
490 out <<
"\nAll tests for RCP seem to check out!\n";
498 if (createCircRefs) {
499 out <<
"\nPrinting the active nodes just to see them!\n";
501#if defined(TEUCHOS_DEBUG) && !defined(HAVE_TEUCHOS_DEBUG_RCP_NODE_TRACING)
509 out <<
"\nEnd Result: TEST PASSED" << std::endl;
511 return ( success ? 0 : 1 );
void destroyOpaque(Opaque_handle *opaque)
Opaque_handle createOpaque()
int getOpaqueValue(Opaque_handle opaque)
int getOpaque2Value(Opaque2_handle opaque)
Opaque2_handle createOpaque2()
void destroyOpaque2(Opaque2_handle *opaque)
void deallocHandleA(A **handle)
const int getOpaque2Value_return
const int getOpaqueValue_return
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.
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
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.
basic_ostream<> subclass that does nothing but discard output.
#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.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
const T & getConst(T &t)
Return a constant reference to an object given a non-const reference.
std::string Teuchos_Version()