Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_SimpleObjectDB.hpp
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#ifndef TEUCHOS_SIMPLE_OBJECT_DB_HPP
11#define TEUCHOS_SIMPLE_OBJECT_DB_HPP
12
13
14#include "Teuchos_Array.hpp"
15#include "Teuchos_ConstNonconstObjectContainer.hpp"
16#include "Teuchos_as.hpp"
17
18
22
26
27namespace Teuchos
28{
29
30
53template <class T>
55{
56public:
57
60
66 int tableSize() const;
67
73 int numFreeIndexes() const;
74
77 int numObjects() const;
78
83 int storeNonconstObj(const RCP<T> &obj);
84
89 int storeConstObj(const RCP<const T> &obj);
90
97 template <class TOld>
98 int storeCastedNonconstObj(const RCP<TOld> & robj_old);
99
105 void removeObj(const int index);
106
112 RCP<T> removeNonconstObj(const int index);
113
116 RCP<const T> removeConstObj(const int index);
117
126 int removeRCP(int &index);
127
133 RCP<T> getNonconstObjRCP(const int index);
134
137 RCP<const T> getConstObjRCP(const int index) const;
138
144 Ptr<T> getNonconstObjPtr(const int index);
145
148 Ptr<const T> getConstObjPtr(const int index) const;
149
155 void purge();
156
157private:
158
159 typedef Array<ConstNonconstObjectContainer<T> > tableOfObjects_t;
160 typedef Array<int> freedIndices_t;
161
162 tableOfObjects_t tableOfObjects_;
163 freedIndices_t freedIndices_;
164
165 void validateIndex(const int index) const;
166
167 template <class T2>
168 int storeObjectImpl(const RCP<T2> &robj);
169
170 void removeObjImpl(const int index);
171
172};
173
174
176template <class T>
181
182
183//
184// Template definitions
185//
186
187
188template <class T>
191
192
193template <class T>
195{
196 return tableOfObjects_.size();
197}
198
199
200template <class T>
202{
203 return freedIndices_.size();
204}
205
206
207template <class T>
209{
210 return tableSize() - numFreeIndexes();
211}
212
213
214template <class T>
216{
217 return storeObjectImpl(obj);
218}
219
220
221template <class T>
223{
224 return storeObjectImpl(obj);
225}
226
227
228template <class T>
229template <class TOld>
231{
232 return storeNonconstObj(rcp_dynamic_cast<T>(robj_old, true));
233}
234
235
236template <class T>
237void SimpleObjectDB<T>::removeObj(const int index)
238{
239 validateIndex(index);
240 removeObjImpl(index);
241}
242
243
244template <class T>
246{
247 validateIndex(index);
248 const RCP<T> obj = tableOfObjects_[index].getNonconstObj();
249 removeObjImpl(index);
250 return obj;
251}
252
253
254template <class T>
256{
257 validateIndex(index);
258 const RCP<const T> obj = tableOfObjects_[index].getConstObj();
259 removeObjImpl(index);
260 return obj;
261}
262
263
264template <class T>
266{
267 const int index_in = index;
268 validateIndex(index);
269 const int cnt = tableOfObjects_[index_in].count();
270 removeObjImpl(index_in);
271 index = -1;
272 return (cnt - 1);
273}
274
275
276template <class T>
278{
279 validateIndex(index);
280 return tableOfObjects_[index].getNonconstObj();
281}
282
283
284template <class T>
286{
287 validateIndex(index);
288 return tableOfObjects_[index].getConstObj();
289}
290
291
292template <class T>
294{
295 validateIndex(index);
296 return tableOfObjects_[index].getNonconstObj().ptr();
297}
298
299
300template <class T>
302{
303 validateIndex(index);
304 return tableOfObjects_[index].getConstObj().ptr();
305}
306
307
308template <class T>
310{
311 // Wipe out all memory (see Item 82 in "C++ Coding Standards")
312 tableOfObjects_t().swap(tableOfObjects_);
313 freedIndices_t().swap(freedIndices_);
314}
315
316
317// private
318
319
320template <class T>
321void SimpleObjectDB<T>::validateIndex(const int index) const
322{
323 using Teuchos::as;
325 !(0 <= index && index < as<int>(tableOfObjects_.size())),
327 "Error, the object index = " << index << " falls outside of the range"
328 << " of valid objects [0,"<<tableOfObjects_.size()<<"]");
329 const RCP<const T> &obj = tableOfObjects_[index].getConstObj();
331 "Error, the object at index "<<index<<" of type "
332 <<TypeNameTraits<T>::name()<<" has already been deleted!");
333}
334
335
336template <class T>
337template <class T2>
338int SimpleObjectDB<T>::storeObjectImpl(const RCP<T2> & robj)
339{
340 robj.assert_not_null();
341
342 int index = -1;
343
344 if (freedIndices_.size() != 0) {
345 index = freedIndices_.back();
346 freedIndices_.pop_back();
347 tableOfObjects_[index].initialize(robj);
348 } else {
349 tableOfObjects_.push_back(robj);
350 index = tableOfObjects_.size() - 1;
351 }
352
353 return index;
354}
355
356
357template <class T>
358void SimpleObjectDB<T>::removeObjImpl(const int index)
359{
360 tableOfObjects_[index] = null;
361 freedIndices_.push_back(index);
362}
363
364
365} // end namespace Teuchos
366
367
368#endif // TEUCHOS_SIMPLE_OBJECT_DB_HPP
369
Templated array class derived from the STL std::vector.
Definition of Teuchos::as, for conversions between types.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes.
Null reference error exception class.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists.
Smart reference counting pointer class for automatic garbage collection.
RCP< T2 > rcp_dynamic_cast(const RCP< T1 > &p1, bool throw_on_fail=false)
Dynamic cast of underlying RCP type from T1* to T2*.
Range error exception class.
Simple object object database.
int storeCastedNonconstObj(const RCP< TOld > &robj_old)
Performs an rcp_dynamic_cast<>() to store the obejct.
int removeRCP(int &index)
Remove an indexed object from the table.
RCP< const T > removeConstObj(const int index)
int storeNonconstObj(const RCP< T > &obj)
Store a non-const object.
void purge()
Clear out all storage.
RCP< T > removeNonconstObj(const int index)
void removeObj(const int index)
Remove a stored object without returning it.
SimpleObjectDB()
Construct an empty DB.
int storeConstObj(const RCP< const T > &obj)
Store a const object.
int numFreeIndexes() const
Return number of free indexes.
int tableSize() const
Return the current size of the table.
int numObjects() const
Return number of non-null stored objects.
Ptr< T > getNonconstObjPtr(const int index)
Get an object (nonconst semi-persisting association).
RCP< T > getNonconstObjRCP(const int index)
Get an object (nonconst persisting association).
RCP< const T > getConstObjRCP(const int index) const
Get an object (const persisting association).
Ptr< const T > getConstObjPtr(const int index) const
Get an object (const semi-persisting association).
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
RCP< SimpleObjectDB< T > > createSimpleObjectDB()
Nonmember constructor.