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;
104 A *a_raw_ptr = a_rcp.getRawPtr();
114 RCP<const A> ca_rcp = a_rcp.getConst();
135 RCP<A> a_rcp(0,
null);
143 RCP<A> a_rcp(a,
null);
160 RCP<A> a_rcp1 = rcp<A>(
new A);
163 if (RCPNodeTracer::isTracingActiveRCPNodes())
201 ECHO(RCP<int> rcp1 =
rcp(
new int(0)));
208 ECHO(RCP<int> rcp3 = rcp2);
213 ECHO(RCP <int> rcp4 = rcp1);
253 ECHO(
A *a_ptr = c_ptr);
254 ECHO(RCP<C> c_rcp =
rcp(c_ptr));
255 #if defined(TEUCHOS_DEBUG) && defined(HAS_TEUCHOS_GET_BASE_OBJ_VOID_PTR)
257 TEST_THROW(RCP<A> a_rcp =
rcp(a_ptr), DuplicateOwningRCPError);
260 ECHO(RCP<A> a_rcp =
rcp(a_ptr));
262 ECHO(a_rcp.release());
275 ECHO(
A *a_ptr2 = a_ptr1);
276 ECHO(RCP<A> a_rcp1 =
rcp(a_ptr1));
277 #if defined(TEUCHOS_DEBUG)
279 TEST_THROW(RCP<A> a_rcp2 =
rcp(a_ptr2), DuplicateOwningRCPError);
282 ECHO(RCP<A> a_rcp2 =
rcp(a_ptr2));
284 ECHO(a_rcp2.release());
297 ECHO(
E *d_ptr = e_ptr);
298 ECHO(RCP<E> e_rcp =
rcp(e_ptr));
299 #if defined(TEUCHOS_DEBUG)
302 TEST_THROW(RCP<D> d_rcp =
rcp(d_ptr), DuplicateOwningRCPError);
305 ECHO(RCP<D> d_rcp =
rcp(d_ptr));
307 ECHO(d_rcp.release());
326 ECHO(RCP<C> c_rcp(
new C));
327 ECHO(
A &a_ref = *c_rcp);
328 ECHO(RCP<A> a_rcp =
rcp(&a_ref,
false));
330 #if defined(TEUCHOS_DEBUG) && defined(HAS_TEUCHOS_GET_BASE_OBJ_VOID_PTR)
331 TEST_THROW(a_rcp->A_g(), DanglingReferenceError);
341 ECHO(RCP<A> a_rcp1(
new C));
342 ECHO(
A &a_ref = *a_rcp1);
343 ECHO(RCP<A> a_rcp2 =
rcp(&a_ref,
false));
345 #if defined(TEUCHOS_DEBUG)
346 TEST_THROW(a_rcp2->A_g(), DanglingReferenceError);
356 ECHO(RCP<E> e_rcp(
new E));
357 ECHO(
D &d_ref = *e_rcp);
358 ECHO(RCP<D> d_rcp =
rcp(&d_ref,
false));
360 #if defined(TEUCHOS_DEBUG)
361 TEST_THROW(d_rcp->D_g(), DanglingReferenceError);
371 TEUCHOS_UNIT_TEST( RCP, rcpFromRef_duplicate_rcp_nonowning_polymorphic_different_addr )
374 ECHO(RCP<C> c_rcp(
new C));
375 ECHO(
A &a_ref = *c_rcp);
378 #if defined(TEUCHOS_DEBUG) && defined(HAS_TEUCHOS_GET_BASE_OBJ_VOID_PTR)
379 TEST_THROW(a_rcp->A_g(), DanglingReferenceError);
386 TEUCHOS_UNIT_TEST( RCP, rcpFromRef_duplicate_rcp_nonowning_polymorphic_same_addr )
389 ECHO(RCP<A> a_rcp1(
new C));
390 ECHO(
A &a_ref = *a_rcp1);
393 #if defined(TEUCHOS_DEBUG)
394 TEST_THROW(a_rcp2->A_g(), DanglingReferenceError);
404 ECHO(RCP<E> e_rcp(
new E));
405 ECHO(
D &d_ref = *e_rcp);
408 #if defined(TEUCHOS_DEBUG)
409 TEST_THROW(d_rcp->D_g(), DanglingReferenceError);
419 TEUCHOS_UNIT_TEST( RCP, rcpFromUndefRef_duplicate_rcp_nonowning_polymorphic_same_addr )
422 ECHO(RCP<A> a_rcp1(
new C));
423 ECHO(
A &a_ref = *a_rcp1);
437 RCP<A> a_rcp =
rcp(
new A);
446 const Ptr<const RCP<C> > c_ptr_rcp_1 =
447 getOptionalEmbeddedObj<A, RCP<C> >(a_rcp);
449 const Ptr<RCP<C> > c_ptr_rcp_2 =
450 getOptionalNonconstEmbeddedObj<A, RCP<C> >(a_rcp);
461 Ptr<const RCP<C> > c_ptr_rcp_1 =
462 getOptionalEmbeddedObj<A, RCP<C> >(a_rcp);
464 TEST_EQUALITY( (*c_ptr_rcp_1).getRawPtr(), c_rcp.getRawPtr() );
467 Ptr<RCP<C> > c_ptr_rcp_2 =
468 getOptionalNonconstEmbeddedObj<A, RCP<C> >(a_rcp);
470 TEST_EQUALITY( (*c_ptr_rcp_2).getRawPtr(), c_rcp.getRawPtr() );
478 RCP<A> a_rcp =
rcp(
new A);
486 RCP<A> a_rcp =
rcp(
new A);
508 ECHO(RCP<T> rcp_strong =
rcp(
new T));
516 ECHO(RCP<T> rcp_weak1 = rcp_strong.create_weak());
533 TEST_EQUALITY( rcp_weak1.getRawPtr(), rcp_strong.getRawPtr() );
535 ECHO(RCP<T> rcp_weak2 = rcp_weak1);
553 TEST_EQUALITY( rcp_weak2.getRawPtr(), rcp_strong.getRawPtr() );
584 ECHO(rcp_weak1.assert_not_null());
585 ECHO(rcp_weak2.assert_not_null());
587 TEST_THROW( rcp_weak1.assert_valid_ptr(), DanglingReferenceError );
589 TEST_THROW( rcp_weak1.operator->(), DanglingReferenceError );
590 TEST_THROW( *rcp_weak1, DanglingReferenceError );
591 TEST_THROW( rcp_weak1.create_weak(), DanglingReferenceError );
592 TEST_THROW( rcp_weak1.get(), DanglingReferenceError );
593 TEST_THROW( rcp_weak1.getRawPtr(), DanglingReferenceError );
594 TEST_THROW( rcp_weak1(), DanglingReferenceError );
595 TEST_THROW( rcp_weak1.release(), DanglingReferenceError );
596 #endif // TEUCHOS_DEBUG
617 TEST_THROW( rcp_weak2.assert_valid_ptr(), DanglingReferenceError );
619 TEST_THROW( rcp_weak2.operator->(), DanglingReferenceError );
620 TEST_THROW( *rcp_weak2, DanglingReferenceError );
621 TEST_THROW( rcp_weak2.create_weak(), DanglingReferenceError );
622 TEST_THROW( rcp_weak2.get(), DanglingReferenceError );
623 TEST_THROW( rcp_weak2.getRawPtr(), DanglingReferenceError );
624 TEST_THROW( rcp_weak2(), DanglingReferenceError );
625 TEST_THROW( rcp_weak2.release(), DanglingReferenceError );
626 #endif // TEUCHOS_DEBUG
637 ECHO(RCP<A> rcp2 = rcp1.create_weak());
645 ECHO(RCP<A> rcp3 = rcp2.create_strong());
677 ECHO(c->set_A(a.create_weak()));
690 TEST_THROW(c->call_A_g(), DanglingReferenceError);
709 ECHO(c->set_A(a.create_weak()));
740 ECHO(c->set_A(c.create_weak()));
750 ECHO(Ptr<A> a_ptr = a_rcp());
751 ECHO(
A *badPtr = a_rcp.getRawPtr());
768 ECHO(badPtr = a_rcp.getRawPtr());
769 ECHO(a_ptr = a_rcp.ptr());
787 ECHO(badPtr = a_rcp.getRawPtr());
788 ECHO(Ptr<A> a_ptr2(a_rcp.ptr()));
789 ECHO(Ptr<A> a_ptr3(a_ptr2));
790 ECHO(a_ptr = a_ptr3);
808 ECHO(badPtr = c_rcp.getRawPtr());
809 ECHO(Ptr<A> a_ptr2(c_rcp.ptr()));
810 ECHO(a_ptr = a_ptr2);
848 #endif // TEUCHOS_DEBUG
856 RCP<C> createCAFactory()
858 RCP<C> c =
rcp(
new C);
859 c->set_A(
rcp(
new A));
864 RCP<A> createACFactory()
866 RCP<C> c = createCAFactory();
867 return Teuchos::rcpWithInvertedObjOwnership(c->get_A(), c);
871 RCP<C> extractCFromA(
const RCP<A> &a)
873 return Teuchos::getInvertedObjOwnershipParent<C>(a);
879 RCP<A> a = createACFactory();
880 RCP<C> c = extractCFromA(a);
894 RCP<A> a = createACFactory();
895 extractCFromA(a)->set_A(
null);
896 RCP<C> c = extractCFromA(a);
909 RCP<A> createRCPWithBadDealloc()
911 return rcp(
new A[1]);
916 class DeallocArrayDeleteExtraData {
918 static RCP<DeallocArrayDeleteExtraData<T> > create(T *ptr)
919 {
return rcp(
new DeallocArrayDeleteExtraData(ptr)); }
920 ~DeallocArrayDeleteExtraData() {
delete [] ptr_; }
923 DeallocArrayDeleteExtraData(T *ptr) : ptr_(ptr) {}
925 DeallocArrayDeleteExtraData();
926 DeallocArrayDeleteExtraData(
const DeallocArrayDeleteExtraData&);
927 DeallocArrayDeleteExtraData& operator=(
const DeallocArrayDeleteExtraData&);
935 using Teuchos::inOutArg;
936 using Teuchos::set_extra_data;
938 RCP<A> a = createRCPWithBadDealloc();
942 set_extra_data( DeallocArrayDeleteExtraData<A>::create(a.getRawPtr()),
"dealloc",
952 #ifdef HAVE_TEUCHOS_BOOST
957 using boost::shared_ptr;
958 ECHO(shared_ptr<A> a_sptr(
new A));
961 ECHO(a_sptr = shared_ptr<A>());
967 #endif // HAVE_TEUCHOS_BOOST
975 #ifdef HAVE_TEUCHOSCORE_CXX11
980 using std::shared_ptr;
981 ECHO(shared_ptr<A> a_sptr(
new A));
984 ECHO(a_sptr = shared_ptr<A>());
992 const std::shared_ptr<A> a_sptr1;
993 const RCP<A> a_rsptr1 =
rcp(a_sptr1);
1005 const std::shared_ptr<A> a_sptr1(
new C());
1006 const RCP<A> a_rsptr1 =
rcp(a_sptr1);
1020 const RCP<A> a_rcp1;
1025 const RCP<A> a_rcp2 =
rcp(a_sptr1);
1033 const RCP<A> a_rcp1(
new C());
1039 const RCP<A> a_rcp2 =
rcp(a_sptr1);
1046 #ifdef TEUCHOS_DEBUG
1051 const RCP<A> a_rcp1(
new C());
1055 const RCP<C> c_rcp1 =
rcp(std::dynamic_pointer_cast<C>(a_sptr1));
1063 #endif // TEUCHOS_DEBUG
1066 #endif // HAVE_TEUCHOSCORE_CXX11
1075 #ifdef TEUCHOS_DEBUG
1077 # define DEBUG_UNIT_TEST_GROUP( T ) \
1081 # define DEBUG_UNIT_TEST_GROUP( T )
1086 #define UNIT_TEST_GROUP( T ) \
1087 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCP, weakDelete, T ) \
1088 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...
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)