51 #ifdef HAVE_TEUCHOS_BOOST
67 #define SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS
68 #define SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS_PRINT
75 int 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();
258 #ifndef _INTEL // Intel compiler does not seem to be doing dynamic cast correctly?
259 #ifdef TEUCHOS_DEBUG // operator->() only throws std::exception when TEUCHOS_DEBUG is defined
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;
343 #endif // SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS_PRINT
345 a_ptr1 =
rcp(a_rptr5);
347 #endif // SHOW_RUN_TIME_ERROR_VIRTUAL_BASE_CLASS
351 get_dealloc<DeallocDelete<C> >(a_ptr1);
352 get_nonconst_dealloc<DeallocDelete<C> >(a_ptr1);
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);
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);
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);
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());
438 #endif // TEUCHOS_DEBUG
440 #ifndef TEUCHOS_DEBUG
442 out <<
"\nTesting using RCP to wrap an undefined opaque object (no TNT) ...\n";
444 RCP<UndefinedType> op_ptr =
456 #endif // not TEUCHOS_DEBUG
458 out <<
"\nTesting using RCP to wrap an undefined opaque object (with TNT) ...\n";
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);
480 RCP<A> a_rsptr2 =
rcp(a_sptr2);
486 out <<
"\nCompatibility with boost::shared_ptr passed ...\n";
488 #endif // HAVE_TEUCHOS_BOOST
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 );