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
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>
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
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#ifdef TEUCHOS_IMPL_GCC_12_NONNULL_WORKAROUND
281 return tableOfObjects_.at(index).getNonconstObj();
282#else
283 return tableOfObjects_[index].getNonconstObj();
284#endif
285}
286
287
288template <class T>
290{
291 validateIndex(index);
292#ifdef TEUCHOS_IMPL_GCC_12_NONNULL_WORKAROUND
293 return tableOfObjects_.at(index).getConstObj();
294#else
295 return tableOfObjects_[index].getConstObj();
296#endif
297}
298
299
300template <class T>
302{
303 validateIndex(index);
304#ifdef TEUCHOS_IMPL_GCC_12_NONNULL_WORKAROUND
305 return tableOfObjects_.at(index).getNonconstObj().ptr();
306#else
307 return tableOfObjects_[index].getNonconstObj().ptr();
308#endif
309}
310
311
312template <class T>
314{
315 validateIndex(index);
316#ifdef TEUCHOS_IMPL_GCC_12_NONNULL_WORKAROUND
317 return tableOfObjects_.at(index).getConstObj().ptr();
318#else
319 return tableOfObjects_[index].getConstObj().ptr();
320#endif
321}
322
323
324template <class T>
326{
327 // Wipe out all memory (see Item 82 in "C++ Coding Standards")
328 tableOfObjects_t().swap(tableOfObjects_);
329 freedIndices_t().swap(freedIndices_);
330}
331
332
333// private
334
335
336template <class T>
337void SimpleObjectDB<T>::validateIndex(const int index) const
338{
339 using Teuchos::as;
341 !(0 <= index && index < as<int>(tableOfObjects_.size())),
343 "Error, the object index = " << index << " falls outside of the range"
344 << " of valid objects [0,"<<tableOfObjects_.size()<<"]");
345 const RCP<const T> &obj = tableOfObjects_[index].getConstObj();
347 "Error, the object at index "<<index<<" of type "
348 <<TypeNameTraits<T>::name()<<" has already been deleted!");
349}
350
351
352template <class T>
353template <class T2>
354int SimpleObjectDB<T>::storeObjectImpl(const RCP<T2> & robj)
355{
356 robj.assert_not_null();
357
358 int index = -1;
359
360 if (freedIndices_.size() != 0) {
361 index = freedIndices_.back();
362 freedIndices_.pop_back();
363 tableOfObjects_[index].initialize(robj);
364 } else {
365 tableOfObjects_.push_back(robj);
366 index = tableOfObjects_.size() - 1;
367 }
368
369 return index;
370}
371
372
373template <class T>
374void SimpleObjectDB<T>::removeObjImpl(const int index)
375{
376 tableOfObjects_[index] = null;
377 freedIndices_.push_back(index);
378}
379
380
381} // end namespace Teuchos
382
383
384#endif // TEUCHOS_SIMPLE_OBJECT_DB_HPP
Templated array class derived from the STL std::vector.
Definition of Teuchos::as, for conversions between types.
friend void swap(Array< T2 > &a1, Array< T2 > &a2)
Null reference error exception class.
Smart reference counting pointer class for automatic garbage collection.
bool is_null() const
Returns true if the underlying pointer is null.
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).
Default traits class that just returns typeid(T).name().
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
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.