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[] ) {
108 bool createCircRefs =
false;
114 std::ostream &out = ( procRank == 0 ? std::cout : blackhole );
120 clp.
setOption(
"create-circ-refs",
"no-create-circ-refs", &createCircRefs,
121 "Set if output is printed or not." );
124 out <<
"\nEnd Result: TEST FAILED" << std::endl;
128 blackhole <<
"\nThis should not print anywhere.\n";
132 out <<
"\nTesting basic RCP functionality ...\n";
137 out <<
"\na_ptr1 = " << a_ptr1 <<
"\n";
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
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 )
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);
442 out <<
"\nTesting using RCP to wrap an undefined opaque object (no TNT) ...\n";
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());
477 boost::shared_ptr<A> a_sptr2 = shared_pointer(a_rsptr1);
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.
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Set a boolean option.
EParseCommandLineReturn
Return value for CommandLineProcessor::parse(). Note: These enums are all given non-negative values s...
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
Parse a command line.
Smart reference counting pointer class for automatic garbage collection.
Ptr< T > release()
Release the ownership of the underlying dynamically allocated object.
bool shares_resource(const RCP< T2 > &r_ptr) const
Returns true if the smart pointers share the same underlying reference-counted object.
T * getRawPtr() const
Get the raw C++ pointer to the underlying object.
Ptr< T > ptr() const
Get a safer wrapper raw C++ pointer to the underlying object.
const RCP< T > & assert_not_null() const
Throws NullReferenceError if this->get()==NULL, otherwise returns reference to *this.
T * get() const
Get the raw C++ pointer to the underlying object.
int strong_count() const
Return the number of active RCP<> objects that have a "strong" reference to the underlying reference-...
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.
Embedded & getNonconstEmbeddedObj(const ArrayRCP< T > &p)
Get a const reference to an embedded object that was set by calling arcpWithEmbeddedObjPreDestroy(),...
T1 & get_extra_data(ArrayRCP< T2 > &p, const std::string &name)
Get a non-const reference to extra data associated with a ArrayRCP object.
const Dealloc_T * get_optional_dealloc(const ArrayRCP< T > &p)
Return a pointer to the underlying non-const deallocator object if it exists.
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.
const Dealloc_T & get_dealloc(const ArrayRCP< T > &p)
Return a const reference to the underlying deallocator object.
Dealloc_T * get_optional_nonconst_dealloc(const ArrayRCP< T > &p)
Return a pointer to the underlying const deallocator object if it exists.
Dealloc_T & get_nonconst_dealloc(const ArrayRCP< T > &p)
Return a non-const reference to the underlying deallocator object.
const Embedded & getEmbeddedObj(const ArrayRCP< T > &p)
Get a const reference to an embedded object that was set by calling arcpWithEmbeddedObjPreDestroy(),...
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.
DeallocFunctorDelete< T, DeleteFunctor > deallocFunctorDelete(DeleteFunctor deleteFunctor)
A simple function used to create a functor deallocator object.
DeallocFunctorHandleDelete< T, DeleteHandleFunctor > deallocFunctorHandleDelete(DeleteHandleFunctor deleteHandleFunctor)
A simple function used to create a functor deallocator object.
Initialize, finalize, and query the global MPI session.
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.
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.
RCP< T > rcpWithEmbeddedObjPreDestroy(T *p, const Embedded &embedded, bool owns_mem=true)
Create an RCP with and also put in an embedded 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*.
RCP< T2 > rcp_static_cast(const RCP< T1 > &p1)
Static cast of underlying RCP type from T1* to T2*.
RCP< T > rcpWithEmbeddedObj(T *p, const Embedded &embedded, bool owns_mem=true)
Create an RCP with and also put in an embedded object.
RCP< T2 > rcp_implicit_cast(const RCP< T1 > &p1)
Implicit cast of underlying RCP type from T1* to T2*.
RCP< T2 > rcp_const_cast(const RCP< T1 > &p1)
Constant cast of underlying RCP type from T1* to T2*.
RCP< T > rcpFromRef(T &r)
Return a non-owning weak RCP object from a raw object reference for a defined type.
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< T > rcpWithEmbeddedObjPostDestroy(T *p, const Embedded &embedded, bool owns_mem=true)
Create an RCP with and also put in an embedded object.
T1 & get_nonconst_extra_data(RCP< T2 > &p, const std::string &name)
Get a non-const reference to extra data associated with a RCP object.
#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.
basic_oblackholestream< char, std::char_traits< char > > oblackholestream
std::string Teuchos_Version()
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.