Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RCP_ForwardDeclUnitTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Teuchos_RCP.hpp"
12 
13 
14 /*
15  * This test checks that you can use non-owning Teuchos::RCP with pointers to
16  * types that are only forward declared and not defined.
17  */
18 
19 namespace DummyNS {class UndefinedType;}
20 
21 namespace Teuchos {
23 } // namespace Teuchos
24 
25 
26 namespace {
27 
28 
29 using Teuchos::rcp;
30 using Teuchos::rcpFromRef;
31 using Teuchos::rcpFromUndefRef;
32 using Teuchos::RCP;
33 
34 using DummyNS::UndefinedType;
35 
36 
37 TEUCHOS_UNIT_TEST( RCP, ForwardDeclaredUndefined )
38 {
39  // This test ensures that you can declare a null RCP object to an undefined
40  // type without trouble.
41  RCP<UndefinedType> ut_rcp;
42 }
43 
44 
45 TEUCHOS_UNIT_TEST( RCP, ForwardDeclaredUndefined_rcp )
46 {
47  // This test ensures that you can set a pointer to an undefined type without
48  // trouble. Note that this has to be a non-owning RCP otherwise there will
49  // be issues with the destructor call.
50  UndefinedType *ut_ptr = 0;
51  RCP<UndefinedType> ut_rcp =
52 #if defined(HAS_TEUCHOS_GET_BASE_OBJ_VOID_PTR)
53  rcpFromUndefRef(*ut_ptr)
54  // In this case, you have to use rcpFromUndefRef(...) in this case instead
55  // of rcpFromRef() because the latter requires the object to be defined in
56  // order to call dynamic_cast<const void*>(...) in order to get the base
57  // object address needed for RCPNode tracing.
58 #else
59  rcpFromRef(*ut_ptr)
60  // In this case, you can use rcpFromRef(...) because the object's baseq
61  // address will not be looked up using dynamic_cast and no deallocator
62  // needing to know the object's will be compiled.
63 #endif
64  ;
65 }
66 
67 
68 } // namespace
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Unit testing support.
TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(bool)
RCP< T > rcpFromUndefRef(T &r)
Return a non-owning weak RCP object from a raw object reference for an undefined type.
RCP< T > rcpFromRef(T &r)
Return a non-owning weak RCP object from a raw object reference for a defined type.
Smart reference counting pointer class for automatic garbage collection.
Reference-counted pointer class and non-member templated function implementations.