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);
176 #ifdef SHOW_RUN_TIME_ERROR_1
179 const RCP<A> a_ptr2 = a_ptr1.get();
186 #ifdef SHOW_COMPILE_TIME_ERRORS
253 #ifdef SHOW_COMPILE_TIME_ERRORS
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 )
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 );
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object...
void destroyOpaque(Opaque_handle *opaque)
void set_extra_data(const T1 &extra_data, const std::string &name, const Ptr< ArrayRCP< T2 > > &p, EPrePostDestruction destroy_when=POST_DESTROY, bool force_unique=true)
Set extra data associated with a ArrayRCP object.
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
Ptr< T > inOutArg(T &arg)
create a non-persisting (required or optional) input/output argument for a function call...
RCP< T2 > rcp_static_cast(const RCP< T1 > &p1)
Static cast of underlying RCP type from T1* to T2*.
boost::shared_ptr< T > shared_pointer(const RCP< T > &rcp)
Conversion function that takes in a Teuchos::RCP object and spits out a boost::shared_ptr object...
T1 & get_extra_data(ArrayRCP< T2 > &p, const std::string &name)
Get a non-const reference to extra data associated with a ArrayRCP object.
Ptr< T1 > get_optional_nonconst_extra_data(RCP< T2 > &p, const std::string &name)
Get a pointer to non-const extra data (if it exists) associated with a RCP object.
RCP< T2 > rcp_dynamic_cast(const RCP< T1 > &p1, bool throw_on_fail=false)
Dynamic cast of underlying RCP type from T1* to T2*.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
basic_ostream<> subclass that does nothing but discard output.
Dealloc_T * get_optional_nonconst_dealloc(const ArrayRCP< T > &p)
Return a pointer to the underlying const deallocator object if it exists.
const T & getConst(T &t)
Return a constant reference to an object given a non-const reference.
const int getOpaqueValue_return
Initialize, finalize, and query the global MPI session.
void destroyOpaque2(Opaque2_handle *opaque)
bool is_null(const ArrayRCP< T > &p)
Returns true if p.get()==NULL.
RCP< T > rcpWithEmbeddedObj(T *p, const Embedded &embedded, bool owns_mem=true)
Create an RCP with and also put in an embedded object.
const int getOpaque2Value_return
Policy class for deallocator that uses delete to delete a pointer which is used by RCP...
T1 * get_optional_extra_data(ArrayRCP< T2 > &p, const std::string &name)
Get a pointer to non-const extra data (if it exists) associated with a ArrayRCP object.
void deallocHandleA(A **handle)
RCP< T > rcpWithEmbeddedObjPostDestroy(T *p, const Embedded &embedded, bool owns_mem=true)
Create an RCP with and also put in an embedded object.
RCP< T > rcpWithEmbeddedObjPreDestroy(T *p, const Embedded &embedded, bool owns_mem=true)
Create an RCP with and also put in an embedded object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
int getOpaqueValue(Opaque_handle opaque)
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
RCP< T > rcpWithDeallocUndef(T *p, Dealloc_T dealloc, bool owns_mem=true)
Initialize from a raw pointer with a deallocation policy for an undefined type.
Opaque2_handle createOpaque2()
const Dealloc_T * get_optional_dealloc(const ArrayRCP< T > &p)
Return a pointer to the underlying non-const deallocator object if it exists.
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[])
int getOpaque2Value(Opaque2_handle opaque)
Opaque_handle createOpaque()
Smart reference counting pointer class for automatic garbage collection.
#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 ...
RCP< T2 > rcp_const_cast(const RCP< T1 > &p1)
Constant cast of underlying RCP type from T1* to T2*.
RCP< T > rcpWithDealloc(T *p, Dealloc_T dealloc, bool owns_mem=true)
Initialize from a raw pointer with a deallocation policy.
static int numActiveRCPNodes()
Print the number of active RCPNode objects currently being tracked.
Reference-counted pointer class and non-member templated function implementations.
Class that helps parse command line input arguments from (argc,argv[]) and set options.
#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...
static void printActiveRCPNodes(std::ostream &out)
Print the list of currently active RCP nodes.