Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConstNonconstObjectContainer.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 
11 
13 
14 namespace Teuchos {
15 
18  RCP<std::vector<double> > vec = rcp(new std::vector<double> );
19  vectorObj.initialize(vec); // nonconst
20  TEST_ASSERT( vectorObj.isConst() == false );
21 }
22 
23 
26  TEST_ASSERT( vectorObj.isConst() == true );
27  TEST_ASSERT( vectorObj.getConstObj() == null );
28  // This does not throw an exception because the pointer is null
29  TEST_ASSERT( vectorObj.getNonconstObj() == null );
30 }
31 
32 
34  RCP<std::vector<double> > vec = rcp(new std::vector<double> );
36  TEST_ASSERT( vectorObj.isConst() == false );
37  RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
38  TEST_ASSERT( vec == vec2 );
39  RCP<std::vector<double> > vec3 = vectorObj.getNonconstObj();
40  TEST_ASSERT( vec == vec3 );
41 }
42 
43 
45  RCP<const std::vector<double> > vec = rcp(new std::vector<double> );
47  TEST_ASSERT( vectorObj.isConst() == true );
48  RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
49  TEST_ASSERT( vec == vec2 );
51  TEST_THROW( vec3 = vectorObj.getNonconstObj(), NonconstAccessError );
52 }
53 
54 
56  RCP<std::vector<double> > vec = rcp(new std::vector<double> );
58  vectorObj.initialize(vec);
59  TEST_ASSERT( vectorObj.isConst() == false );
60  RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
61  TEST_ASSERT( vec == vec2 );
62  RCP<std::vector<double> > vec3 = vectorObj.getNonconstObj();
63  TEST_ASSERT( vec == vec3 );
64 }
65 
66 
68  RCP<const std::vector<double> > vec = rcp(new std::vector<double> );
70  vectorObj.initialize(vec);
71  TEST_ASSERT( vectorObj.isConst() == true );
72  RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
73  TEST_ASSERT( vec == vec2 );
75  TEST_THROW( vec3 = vectorObj.getNonconstObj(), NonconstAccessError );
76 }
77 
78 
80  RCP<const std::vector<double> > vec = rcp(new std::vector<double> );
82  vectorObj = vec;
83  TEST_ASSERT( vectorObj.isConst() == true );
84  RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
85  TEST_ASSERT( vec == vec2 );
87  TEST_THROW( vec3 = vectorObj.getNonconstObj(), NonconstAccessError );
88 }
89 
90 
92  RCP<const std::vector<double> > vec = rcp(new std::vector<double> );
94  TEST_ASSERT( vectorObj.isConst() == true );
95  RCP<const std::vector<double> > vec2 = vectorObj.getConstObj();
96  TEST_ASSERT( vec == vec2 );
97  vectorObj.uninitialize();
98  TEST_ASSERT( vectorObj.isConst() == true );
99  TEST_ASSERT( vectorObj.getConstObj() == null );
100 }
101 
102 
105  RCP<std::vector<double> > vec = rcp(new std::vector<double>);
106  vectorObj.initialize(vec);
107  RCP<const std::vector<double> > vec2 = vectorObj();
108  TEST_ASSERT( vec == vec2 );
109 }
110 
111 
112 // operator->
114  RCP<std::vector<double> > vec = rcp(new std::vector<double>);
115  vec->push_back(25.0);
116  vec->push_back(32.0);
118  TEST_ASSERT( vectorObj->size() == 2 );
119 }
120 
121 
124 #ifdef TEUCHOS_DEBUG
125  TEST_THROW( vectorObj->size(), NullReferenceError );
126 #endif
127 }
128 
129 
130 // operator*
132  RCP<std::vector<double> > vec = rcp(new std::vector<double>);
133  vec->push_back(25.0);
134  vec->push_back(32.0);
136  TEST_ASSERT( (*vectorObj)[0] == 25.0 );
137  TEST_ASSERT( (*vectorObj)[1] == 32.0 );
138 }
139 
140 
143 #ifdef TEUCHOS_DEBUG
144  TEST_THROW( (*vectorObj).size(), NullReferenceError );
145 #endif
146 }
147 
148 
149 // implicit cast
150 // RCP<const ObjType> <-
151 // ConstNonconstObjectContainer<const ObjType>
153  RCP<std::vector<double> > vec = rcp(new std::vector<double>);
155  RCP<const std::vector<double> > vec2(vectorObj);
156  TEST_ASSERT( vec == vec2 );
157 }
158 
159 
160 // implicit cast
161 // RCP<const ObjType> ->
162 // ConstNonconstObjectContainer<const ObjType>
163 // This is already done through the constructors on
164 // ConstNonconstObjectContainer.
166  RCP<std::vector<double> > vec = rcp(new std::vector<double>);
168  TEST_ASSERT( vectorObj.getConstObj() == vec );
169 }
170 
171 
174  TEST_ASSERT( is_null(vectorObj) );
175 }
176 
177 
179  RCP<std::vector<double> > vec = rcp(new std::vector<double>);
181  TEST_ASSERT( nonnull(vectorObj) );
182 }
183 
184 
185 } // namespace Teuchos
186 
187 
188 
Null reference error exception class.
#define TEST_ASSERT(v1)
Assert the given statement is true.
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.
void initialize(const RCP< ObjType > &obj)
. Initialize using a non-const object. Allows both const and non-const access to the contained object...
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
bool isConst() const
Returns true if const-only access to the object is allowed.
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
RCP< const ObjType > getConstObj() const
Get an RCP to the const contained object.
Unit testing support.
Simple class supporting the &quot;runtime protection of const&quot; idiom.
Smart reference counting pointer class for automatic garbage collection.
RCP< ObjType > getNonconstObj() const
Get an RCP to the non-const contained object.
Null reference error exception class.