14 #ifdef HAVE_TEUCHOS_BOOST
17 #ifdef HAVE_TEUCHOSCORE_CXX11
34 using Teuchos::rcpFromRef;
35 using Teuchos::rcpFromUndefRef;
36 using Teuchos::outArg;
37 using Teuchos::rcpWithEmbeddedObj;
38 using Teuchos::getEmbeddedObj;
39 using Teuchos::getOptionalEmbeddedObj;
40 using Teuchos::getOptionalNonconstEmbeddedObj;
41 using Teuchos::set_extra_data;
42 using Teuchos::get_optional_nonconst_extra_data;
122 RCP<A> b_rcp(std::move(a_rcp));
149 A *a_raw_ptr = a_rcp.getRawPtr();
178 b_rcp = std::move(a_rcp);
232 RCP<const A> ca_rcp = a_rcp.getConst();
253 RCP<A> a_rcp(0,
null);
261 RCP<A> a_rcp(a,
null);
278 RCP<A> a_rcp1 = rcp<A>(
new A);
281 if (RCPNodeTracer::isTracingActiveRCPNodes())
339 ECHO(RCP<int> rcp1 =
rcp(
new int(0)));
346 ECHO(RCP<int> rcp3 = rcp2);
351 ECHO(RCP <int> rcp4 = rcp1);
391 ECHO(
A *a_ptr = c_ptr);
392 ECHO(RCP<C> c_rcp =
rcp(c_ptr));
393 #if defined(TEUCHOS_DEBUG) && defined(HAS_TEUCHOS_GET_BASE_OBJ_VOID_PTR)
395 TEST_THROW(RCP<A> a_rcp =
rcp(a_ptr), DuplicateOwningRCPError);
398 ECHO(RCP<A> a_rcp =
rcp(a_ptr));
413 ECHO(
A *a_ptr2 = a_ptr1);
414 ECHO(RCP<A> a_rcp1 =
rcp(a_ptr1));
415 #if defined(TEUCHOS_DEBUG)
417 TEST_THROW(RCP<A> a_rcp2 =
rcp(a_ptr2), DuplicateOwningRCPError);
420 ECHO(RCP<A> a_rcp2 =
rcp(a_ptr2));
422 ECHO(a_rcp2.release());
435 ECHO(
E *d_ptr = e_ptr);
436 ECHO(RCP<E> e_rcp =
rcp(e_ptr));
437 #if defined(TEUCHOS_DEBUG)
440 TEST_THROW(RCP<D> d_rcp =
rcp(d_ptr), DuplicateOwningRCPError);
443 ECHO(RCP<D> d_rcp =
rcp(d_ptr));
445 ECHO(d_rcp.release());
464 ECHO(RCP<C> c_rcp(
new C));
465 ECHO(
A &a_ref = *c_rcp);
466 ECHO(RCP<A> a_rcp =
rcp(&a_ref,
false));
468 #if defined(TEUCHOS_DEBUG) && defined(HAS_TEUCHOS_GET_BASE_OBJ_VOID_PTR)
479 ECHO(RCP<A> a_rcp1(
new C));
480 ECHO(
A &a_ref = *a_rcp1);
481 ECHO(RCP<A> a_rcp2 =
rcp(&a_ref,
false));
483 #if defined(TEUCHOS_DEBUG)
484 TEST_THROW(a_rcp2->A_g(), DanglingReferenceError);
494 ECHO(RCP<E> e_rcp(
new E));
495 ECHO(
D &d_ref = *e_rcp);
496 ECHO(RCP<D> d_rcp =
rcp(&d_ref,
false));
498 #if defined(TEUCHOS_DEBUG)
499 TEST_THROW(d_rcp->D_g(), DanglingReferenceError);
509 TEUCHOS_UNIT_TEST( RCP, rcpFromRef_duplicate_rcp_nonowning_polymorphic_different_addr )
512 ECHO(RCP<C> c_rcp(
new C));
513 ECHO(
A &a_ref = *c_rcp);
516 #if defined(TEUCHOS_DEBUG) && defined(HAS_TEUCHOS_GET_BASE_OBJ_VOID_PTR)
524 TEUCHOS_UNIT_TEST( RCP, rcpFromRef_duplicate_rcp_nonowning_polymorphic_same_addr )
527 ECHO(RCP<A> a_rcp1(
new C));
528 ECHO(
A &a_ref = *a_rcp1);
531 #if defined(TEUCHOS_DEBUG)
532 TEST_THROW(a_rcp2->A_g(), DanglingReferenceError);
542 ECHO(RCP<E> e_rcp(
new E));
543 ECHO(
D &d_ref = *e_rcp);
546 #if defined(TEUCHOS_DEBUG)
547 TEST_THROW(d_rcp->D_g(), DanglingReferenceError);
557 TEUCHOS_UNIT_TEST( RCP, rcpFromUndefRef_duplicate_rcp_nonowning_polymorphic_same_addr )
560 ECHO(RCP<A> a_rcp1(
new C));
561 ECHO(
A &a_ref = *a_rcp1);
575 RCP<A> a_rcp =
rcp(
new A);
584 const Ptr<const RCP<C> > c_ptr_rcp_1 =
585 getOptionalEmbeddedObj<A, RCP<C> >(a_rcp);
587 const Ptr<RCP<C> > c_ptr_rcp_2 =
588 getOptionalNonconstEmbeddedObj<A, RCP<C> >(a_rcp);
599 Ptr<const RCP<C> > c_ptr_rcp_1 =
600 getOptionalEmbeddedObj<A, RCP<C> >(a_rcp);
602 TEST_EQUALITY( (*c_ptr_rcp_1).getRawPtr(), c_rcp.getRawPtr() );
605 Ptr<RCP<C> > c_ptr_rcp_2 =
606 getOptionalNonconstEmbeddedObj<A, RCP<C> >(a_rcp);
608 TEST_EQUALITY( (*c_ptr_rcp_2).getRawPtr(), c_rcp.getRawPtr() );
616 RCP<A> a_rcp =
rcp(
new A);
624 RCP<A> a_rcp =
rcp(
new A);
646 ECHO(RCP<T> rcp_strong =
rcp(
new T));
654 ECHO(RCP<T> rcp_weak1 = rcp_strong.create_weak());
671 TEST_EQUALITY( rcp_weak1.getRawPtr(), rcp_strong.getRawPtr() );
673 ECHO(RCP<T> rcp_weak2 = rcp_weak1);
691 TEST_EQUALITY( rcp_weak2.getRawPtr(), rcp_strong.getRawPtr() );
722 ECHO(rcp_weak1.assert_not_null());
723 ECHO(rcp_weak2.assert_not_null());
725 TEST_THROW( rcp_weak1.assert_valid_ptr(), DanglingReferenceError );
727 TEST_THROW( rcp_weak1.operator->(), DanglingReferenceError );
728 TEST_THROW( *rcp_weak1, DanglingReferenceError );
729 TEST_THROW( rcp_weak1.create_weak(), DanglingReferenceError );
730 TEST_THROW( rcp_weak1.get(), DanglingReferenceError );
731 TEST_THROW( rcp_weak1.getRawPtr(), DanglingReferenceError );
732 TEST_THROW( rcp_weak1(), DanglingReferenceError );
733 TEST_THROW( rcp_weak1.release(), DanglingReferenceError );
734 #endif // TEUCHOS_DEBUG
755 TEST_THROW( rcp_weak2.assert_valid_ptr(), DanglingReferenceError );
757 TEST_THROW( rcp_weak2.operator->(), DanglingReferenceError );
758 TEST_THROW( *rcp_weak2, DanglingReferenceError );
759 TEST_THROW( rcp_weak2.create_weak(), DanglingReferenceError );
760 TEST_THROW( rcp_weak2.get(), DanglingReferenceError );
761 TEST_THROW( rcp_weak2.getRawPtr(), DanglingReferenceError );
762 TEST_THROW( rcp_weak2(), DanglingReferenceError );
763 TEST_THROW( rcp_weak2.release(), DanglingReferenceError );
764 #endif // TEUCHOS_DEBUG
775 ECHO(RCP<A> rcp2 = rcp1.create_weak());
783 ECHO(RCP<A> rcp3 = rcp2.create_strong());
815 ECHO(c->set_A(a.create_weak()));
828 TEST_THROW(c->call_A_g(), DanglingReferenceError);
847 ECHO(c->set_A(a.create_weak()));
878 ECHO(c->set_A(c.create_weak()));
888 ECHO(Ptr<A> a_ptr = a_rcp());
889 ECHO(
A *badPtr = a_rcp.getRawPtr());
906 ECHO(badPtr = a_rcp.getRawPtr());
907 ECHO(a_ptr = a_rcp.ptr());
925 ECHO(badPtr = a_rcp.getRawPtr());
926 ECHO(Ptr<A> a_ptr2(a_rcp.ptr()));
927 ECHO(Ptr<A> a_ptr3(a_ptr2));
928 ECHO(a_ptr = a_ptr3);
946 ECHO(badPtr = c_rcp.getRawPtr());
947 ECHO(Ptr<A> a_ptr2(c_rcp.ptr()));
948 ECHO(a_ptr = a_ptr2);
986 #endif // TEUCHOS_DEBUG
994 RCP<C> createCAFactory()
996 RCP<C> c =
rcp(
new C);
997 c->set_A(
rcp(
new A));
1002 RCP<A> createACFactory()
1004 RCP<C> c = createCAFactory();
1005 return Teuchos::rcpWithInvertedObjOwnership(c->get_A(), c);
1009 RCP<C> extractCFromA(
const RCP<A> &a)
1011 return Teuchos::getInvertedObjOwnershipParent<C>(a);
1017 RCP<A> a = createACFactory();
1018 RCP<C> c = extractCFromA(a);
1032 RCP<A> a = createACFactory();
1033 extractCFromA(a)->set_A(
null);
1034 RCP<C> c = extractCFromA(a);
1047 RCP<A> createRCPWithBadDealloc()
1049 return rcp(
new A[1]);
1053 template<
typename T>
1054 class DeallocArrayDeleteExtraData {
1056 static RCP<DeallocArrayDeleteExtraData<T> > create(T *ptr)
1057 {
return rcp(
new DeallocArrayDeleteExtraData(ptr)); }
1058 ~DeallocArrayDeleteExtraData() {
delete [] ptr_; }
1061 DeallocArrayDeleteExtraData(T *ptr) : ptr_(ptr) {}
1063 DeallocArrayDeleteExtraData();
1064 DeallocArrayDeleteExtraData(
const DeallocArrayDeleteExtraData&);
1065 DeallocArrayDeleteExtraData& operator=(
const DeallocArrayDeleteExtraData&);
1073 using Teuchos::inOutArg;
1074 using Teuchos::set_extra_data;
1076 RCP<A> a = createRCPWithBadDealloc();
1080 set_extra_data( DeallocArrayDeleteExtraData<A>::create(a.getRawPtr()),
"dealloc",
1107 #ifdef HAVE_TEUCHOS_BOOST
1112 using boost::shared_ptr;
1113 ECHO(shared_ptr<A> a_sptr(
new A));
1116 ECHO(a_sptr = shared_ptr<A>());
1122 #endif // HAVE_TEUCHOS_BOOST
1130 #ifdef HAVE_TEUCHOSCORE_CXX11
1135 using std::shared_ptr;
1136 ECHO(shared_ptr<A> a_sptr(
new A));
1139 ECHO(a_sptr = shared_ptr<A>());
1147 const std::shared_ptr<A> a_sptr1;
1148 const RCP<A> a_rsptr1 =
rcp(a_sptr1);
1160 const std::shared_ptr<A> a_sptr1(
new C());
1161 const RCP<A> a_rsptr1 =
rcp(a_sptr1);
1175 const RCP<A> a_rcp1;
1180 const RCP<A> a_rcp2 =
rcp(a_sptr1);
1188 const RCP<A> a_rcp1(
new C());
1194 const RCP<A> a_rcp2 =
rcp(a_sptr1);
1201 #ifdef TEUCHOS_DEBUG
1206 const RCP<A> a_rcp1(
new C());
1210 const RCP<C> c_rcp1 =
rcp(std::dynamic_pointer_cast<C>(a_sptr1));
1218 #endif // TEUCHOS_DEBUG
1221 #endif // HAVE_TEUCHOSCORE_CXX11
1230 #ifdef TEUCHOS_DEBUG
1232 # define DEBUG_UNIT_TEST_GROUP( T ) \
1236 # define DEBUG_UNIT_TEST_GROUP( T )
1241 #define UNIT_TEST_GROUP( T ) \
1242 TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( RCP, weakDelete, T ) \
1243 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.
T * get() const
Get the raw C++ pointer to the underlying object.
#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.
T * getRawPtr() const
Get the raw C++ pointer to the underlying object.
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.
Ptr< T > release()
Release the ownership of the underlying dynamically allocated object.
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...
auto make_rcp(Args &&...args)
bool nonnull(const ArrayRCP< T > &p)
Returns true if p.get()!=NULL.
#define UNIT_TEST_GROUP(T)