48 #ifdef HAVE_TEUCHOS_BOOST
51 #ifdef HAVE_TEUCHOSCORE_CXX11
68 using Teuchos::rcpFromRef;
69 using Teuchos::rcpFromUndefRef;
70 using Teuchos::outArg;
71 using Teuchos::rcpWithEmbeddedObj;
72 using Teuchos::getEmbeddedObj;
73 using Teuchos::getOptionalEmbeddedObj;
74 using Teuchos::getOptionalNonconstEmbeddedObj;
75 using Teuchos::set_extra_data;
76 using Teuchos::get_optional_nonconst_extra_data;
156 RCP<A> b_rcp(std::move(a_rcp));
183 A *a_raw_ptr = a_rcp.getRawPtr();
212 b_rcp = std::move(a_rcp);
266 RCP<const A> ca_rcp = a_rcp.getConst();
287 RCP<A> a_rcp(0,
null);
295 RCP<A> a_rcp(a,
null);
312 RCP<A> a_rcp1 = rcp<A>(
new A);
315 if (RCPNodeTracer::isTracingActiveRCPNodes())
353 ECHO(RCP<int> rcp1 =
rcp(
new int(0)));
360 ECHO(RCP<int> rcp3 = rcp2);
365 ECHO(RCP <int> rcp4 = rcp1);
405 ECHO(
A *a_ptr = c_ptr);
406 ECHO(RCP<C> c_rcp =
rcp(c_ptr));
407 #if defined(TEUCHOS_DEBUG) && defined(HAS_TEUCHOS_GET_BASE_OBJ_VOID_PTR)
409 TEST_THROW(RCP<A> a_rcp =
rcp(a_ptr), DuplicateOwningRCPError);
412 ECHO(RCP<A> a_rcp =
rcp(a_ptr));
414 ECHO(a_rcp.release());
427 ECHO(
A *a_ptr2 = a_ptr1);
428 ECHO(RCP<A> a_rcp1 =
rcp(a_ptr1));
429 #if defined(TEUCHOS_DEBUG)
431 TEST_THROW(RCP<A> a_rcp2 =
rcp(a_ptr2), DuplicateOwningRCPError);
434 ECHO(RCP<A> a_rcp2 =
rcp(a_ptr2));
436 ECHO(a_rcp2.release());
449 ECHO(
E *d_ptr = e_ptr);
450 ECHO(RCP<E> e_rcp =
rcp(e_ptr));
451 #if defined(TEUCHOS_DEBUG)
454 TEST_THROW(RCP<D> d_rcp =
rcp(d_ptr), DuplicateOwningRCPError);
457 ECHO(RCP<D> d_rcp =
rcp(d_ptr));
459 ECHO(d_rcp.release());
478 ECHO(RCP<C> c_rcp(
new C));
479 ECHO(
A &a_ref = *c_rcp);
480 ECHO(RCP<A> a_rcp =
rcp(&a_ref,
false));
482 #if defined(TEUCHOS_DEBUG) && defined(HAS_TEUCHOS_GET_BASE_OBJ_VOID_PTR)
483 TEST_THROW(a_rcp->A_g(), DanglingReferenceError);
493 ECHO(RCP<A> a_rcp1(
new C));
494 ECHO(
A &a_ref = *a_rcp1);
495 ECHO(RCP<A> a_rcp2 =
rcp(&a_ref,
false));
497 #if defined(TEUCHOS_DEBUG)
498 TEST_THROW(a_rcp2->A_g(), DanglingReferenceError);
508 ECHO(RCP<E> e_rcp(
new E));
509 ECHO(
D &d_ref = *e_rcp);
510 ECHO(RCP<D> d_rcp =
rcp(&d_ref,
false));
512 #if defined(TEUCHOS_DEBUG)
513 TEST_THROW(d_rcp->D_g(), DanglingReferenceError);
523 TEUCHOS_UNIT_TEST( RCP, rcpFromRef_duplicate_rcp_nonowning_polymorphic_different_addr )
526 ECHO(RCP<C> c_rcp(
new C));
527 ECHO(
A &a_ref = *c_rcp);
530 #if defined(TEUCHOS_DEBUG) && defined(HAS_TEUCHOS_GET_BASE_OBJ_VOID_PTR)
531 TEST_THROW(a_rcp->A_g(), DanglingReferenceError);
538 TEUCHOS_UNIT_TEST( RCP, rcpFromRef_duplicate_rcp_nonowning_polymorphic_same_addr )
541 ECHO(RCP<A> a_rcp1(
new C));
542 ECHO(
A &a_ref = *a_rcp1);
545 #if defined(TEUCHOS_DEBUG)
546 TEST_THROW(a_rcp2->A_g(), DanglingReferenceError);
556 ECHO(RCP<E> e_rcp(
new E));
557 ECHO(
D &d_ref = *e_rcp);
560 #if defined(TEUCHOS_DEBUG)
561 TEST_THROW(d_rcp->D_g(), DanglingReferenceError);
571 TEUCHOS_UNIT_TEST( RCP, rcpFromUndefRef_duplicate_rcp_nonowning_polymorphic_same_addr )
574 ECHO(RCP<A> a_rcp1(
new C));
575 ECHO(
A &a_ref = *a_rcp1);
589 RCP<A> a_rcp =
rcp(
new A);
598 const Ptr<const RCP<C> > c_ptr_rcp_1 =
599 getOptionalEmbeddedObj<A, RCP<C> >(a_rcp);
601 const Ptr<RCP<C> > c_ptr_rcp_2 =
602 getOptionalNonconstEmbeddedObj<A, RCP<C> >(a_rcp);
613 Ptr<const RCP<C> > c_ptr_rcp_1 =
614 getOptionalEmbeddedObj<A, RCP<C> >(a_rcp);
616 TEST_EQUALITY( (*c_ptr_rcp_1).getRawPtr(), c_rcp.getRawPtr() );
619 Ptr<RCP<C> > c_ptr_rcp_2 =
620 getOptionalNonconstEmbeddedObj<A, RCP<C> >(a_rcp);
622 TEST_EQUALITY( (*c_ptr_rcp_2).getRawPtr(), c_rcp.getRawPtr() );
630 RCP<A> a_rcp =
rcp(
new A);
638 RCP<A> a_rcp =
rcp(
new A);
660 ECHO(RCP<T> rcp_strong =
rcp(
new T));
668 ECHO(RCP<T> rcp_weak1 = rcp_strong.create_weak());
685 TEST_EQUALITY( rcp_weak1.getRawPtr(), rcp_strong.getRawPtr() );
687 ECHO(RCP<T> rcp_weak2 = rcp_weak1);
705 TEST_EQUALITY( rcp_weak2.getRawPtr(), rcp_strong.getRawPtr() );
736 ECHO(rcp_weak1.assert_not_null());
737 ECHO(rcp_weak2.assert_not_null());
739 TEST_THROW( rcp_weak1.assert_valid_ptr(), DanglingReferenceError );
741 TEST_THROW( rcp_weak1.operator->(), DanglingReferenceError );
742 TEST_THROW( *rcp_weak1, DanglingReferenceError );
743 TEST_THROW( rcp_weak1.create_weak(), DanglingReferenceError );
744 TEST_THROW( rcp_weak1.get(), DanglingReferenceError );
745 TEST_THROW( rcp_weak1.getRawPtr(), DanglingReferenceError );
746 TEST_THROW( rcp_weak1(), DanglingReferenceError );
747 TEST_THROW( rcp_weak1.release(), DanglingReferenceError );
748 #endif // TEUCHOS_DEBUG
769 TEST_THROW( rcp_weak2.assert_valid_ptr(), DanglingReferenceError );
771 TEST_THROW( rcp_weak2.operator->(), DanglingReferenceError );
772 TEST_THROW( *rcp_weak2, DanglingReferenceError );
773 TEST_THROW( rcp_weak2.create_weak(), DanglingReferenceError );
774 TEST_THROW( rcp_weak2.get(), DanglingReferenceError );
775 TEST_THROW( rcp_weak2.getRawPtr(), DanglingReferenceError );
776 TEST_THROW( rcp_weak2(), DanglingReferenceError );
777 TEST_THROW( rcp_weak2.release(), DanglingReferenceError );
778 #endif // TEUCHOS_DEBUG
789 ECHO(RCP<A> rcp2 = rcp1.create_weak());
797 ECHO(RCP<A> rcp3 = rcp2.create_strong());
829 ECHO(c->set_A(a.create_weak()));
842 TEST_THROW(c->call_A_g(), DanglingReferenceError);
861 ECHO(c->set_A(a.create_weak()));
892 ECHO(c->set_A(c.create_weak()));
902 ECHO(Ptr<A> a_ptr = a_rcp());
903 ECHO(
A *badPtr = a_rcp.getRawPtr());
920 ECHO(badPtr = a_rcp.getRawPtr());
921 ECHO(a_ptr = a_rcp.ptr());
939 ECHO(badPtr = a_rcp.getRawPtr());
940 ECHO(Ptr<A> a_ptr2(a_rcp.ptr()));
941 ECHO(Ptr<A> a_ptr3(a_ptr2));
942 ECHO(a_ptr = a_ptr3);
960 ECHO(badPtr = c_rcp.getRawPtr());
961 ECHO(Ptr<A> a_ptr2(c_rcp.ptr()));
962 ECHO(a_ptr = a_ptr2);
1000 #endif // TEUCHOS_DEBUG
1008 RCP<C> createCAFactory()
1010 RCP<C> c =
rcp(
new C);
1011 c->set_A(
rcp(
new A));
1016 RCP<A> createACFactory()
1018 RCP<C> c = createCAFactory();
1019 return Teuchos::rcpWithInvertedObjOwnership(c->get_A(), c);
1023 RCP<C> extractCFromA(
const RCP<A> &a)
1025 return Teuchos::getInvertedObjOwnershipParent<C>(a);
1031 RCP<A> a = createACFactory();
1032 RCP<C> c = extractCFromA(a);
1046 RCP<A> a = createACFactory();
1047 extractCFromA(a)->set_A(
null);
1048 RCP<C> c = extractCFromA(a);
1061 RCP<A> createRCPWithBadDealloc()
1063 return rcp(
new A[1]);
1067 template<
typename T>
1068 class DeallocArrayDeleteExtraData {
1070 static RCP<DeallocArrayDeleteExtraData<T> > create(T *ptr)
1071 {
return rcp(
new DeallocArrayDeleteExtraData(ptr)); }
1072 ~DeallocArrayDeleteExtraData() {
delete [] ptr_; }
1075 DeallocArrayDeleteExtraData(T *ptr) : ptr_(ptr) {}
1077 DeallocArrayDeleteExtraData();
1078 DeallocArrayDeleteExtraData(
const DeallocArrayDeleteExtraData&);
1079 DeallocArrayDeleteExtraData& operator=(
const DeallocArrayDeleteExtraData&);
1087 using Teuchos::inOutArg;
1088 using Teuchos::set_extra_data;
1090 RCP<A> a = createRCPWithBadDealloc();
1094 set_extra_data( DeallocArrayDeleteExtraData<A>::create(a.getRawPtr()),
"dealloc",
1104 #ifdef HAVE_TEUCHOS_BOOST
1109 using boost::shared_ptr;
1110 ECHO(shared_ptr<A> a_sptr(
new A));
1113 ECHO(a_sptr = shared_ptr<A>());
1119 #endif // HAVE_TEUCHOS_BOOST
1127 #ifdef HAVE_TEUCHOSCORE_CXX11
1132 using std::shared_ptr;
1133 ECHO(shared_ptr<A> a_sptr(
new A));
1136 ECHO(a_sptr = shared_ptr<A>());
1144 const std::shared_ptr<A> a_sptr1;
1145 const RCP<A> a_rsptr1 =
rcp(a_sptr1);
1157 const std::shared_ptr<A> a_sptr1(
new C());
1158 const RCP<A> a_rsptr1 =
rcp(a_sptr1);
1172 const RCP<A> a_rcp1;
1177 const RCP<A> a_rcp2 =
rcp(a_sptr1);
1185 const RCP<A> a_rcp1(
new C());
1191 const RCP<A> a_rcp2 =
rcp(a_sptr1);
1198 #ifdef TEUCHOS_DEBUG
1203 const RCP<A> a_rcp1(
new C());
1207 const RCP<C> c_rcp1 =
rcp(std::dynamic_pointer_cast<C>(a_sptr1));
1215 #endif // TEUCHOS_DEBUG
1218 #endif // HAVE_TEUCHOSCORE_CXX11
1227 #ifdef TEUCHOS_DEBUG
1229 # define DEBUG_UNIT_TEST_GROUP( T ) \
1233 # define DEBUG_UNIT_TEST_GROUP( T )
1238 #define UNIT_TEST_GROUP( T ) \
1239 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCP, weakDelete, T ) \
1240 DEBUG_UNIT_TEST_GROUP(T)
Dangling reference error exception class.
Null reference error exception class.
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 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.
void set_C(const Teuchos::RCP< C > &c)
#define TEST_ASSERT(v1)
Assert the given statement is true.
Ptr< T > inOutArg(T &arg)
create a non-persisting (required or optional) input/output argument for a function call...
#define TEST_INEQUALITY_CONST(v1, v2)
Assert the inequality of v1 and constant v2.
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.
#define TEST_NOTHROW(code)
Asserr that the statement 'code' does not thrown any excpetions.
#define ECHO(statement)
Echo the given statement before it is executed.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
#define TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL(TEST_GROUP, TEST_NAME, TYPE)
Macro for defining a templated unit test with one template parameter.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement 'code' throws the exception 'ExceptType' (otherwise the test fails)...
const T & getConst(T &t)
Return a constant reference to an object given a non-const reference.
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
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.
void free(T *ptr)
Deallocates a pointer ptr using delete ptr (required).
Debug-mode RCPNode tracing class.
RCP< T > rcpCloneNode(const RCP< T > &p)
Allocate a new RCP object with a new RCPNode with memory pointing to the initial node.
#define SET_RCPNODE_TRACING()
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
RCP< T > rcpFromUndefRef(T &r)
Return a non-owning weak RCP object from a raw object reference for an undefined type.
std::shared_ptr< T > get_shared_ptr(const RCP< T > &rcp)
Conversion function that takes in a Teuchos::RCP object and spits out a std::shared_ptr object...
RCP< T > rcpFromRef(T &r)
Return a non-owning weak RCP object from a raw object reference for a defined type.
Policy class for deallocator for non-owned RCPs.
Smart reference counting pointer class for automatic garbage collection.
RCP< T > rcpWithDealloc(T *p, Dealloc_T dealloc, bool owns_mem=true)
Initialize from a raw pointer with a deallocation policy.
Ptr< T > outArg(T &arg)
create a non-persisting (required or optional) output argument for a function call.
Thrown if a duplicate owning RCP is creatd the the same object.
Reference-counted pointer class and non-member templated function implementations.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
bool nonnull(const ArrayRCP< T > &p)
Returns true if p.get()!=NULL.
#define UNIT_TEST_GROUP(T)