Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
27 namespace Teuchos
28 {
29 
30 
53 template <class T>
55 {
56 public:
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 
157 private:
158 
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 
176 template <class T>
178 {
179  return rcp(new SimpleObjectDB<T>);
180 }
181 
182 
183 //
184 // Template definitions
185 //
186 
187 
188 template <class T>
190 {}
191 
192 
193 template <class T>
195 {
196  return tableOfObjects_.size();
197 }
198 
199 
200 template <class T>
202 {
203  return freedIndices_.size();
204 }
205 
206 
207 template <class T>
209 {
210  return tableSize() - numFreeIndexes();
211 }
212 
213 
214 template <class T>
216 {
217  return storeObjectImpl(obj);
218 }
219 
220 
221 template <class T>
223 {
224  return storeObjectImpl(obj);
225 }
226 
227 
228 template <class T>
229 template <class TOld>
231 {
232  return storeNonconstObj(rcp_dynamic_cast<T>(robj_old, true));
233 }
234 
235 
236 template <class T>
237 void SimpleObjectDB<T>::removeObj(const int index)
238 {
239  validateIndex(index);
240  removeObjImpl(index);
241 }
242 
243 
244 template <class T>
246 {
247  validateIndex(index);
248  const RCP<T> obj = tableOfObjects_[index].getNonconstObj();
249  removeObjImpl(index);
250  return obj;
251 }
252 
253 
254 template <class T>
256 {
257  validateIndex(index);
258  const RCP<const T> obj = tableOfObjects_[index].getConstObj();
259  removeObjImpl(index);
260  return obj;
261 }
262 
263 
264 template <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 
276 template <class T>
278 {
279  validateIndex(index);
280  return tableOfObjects_[index].getNonconstObj();
281 }
282 
283 
284 template <class T>
286 {
287  validateIndex(index);
288  return tableOfObjects_[index].getConstObj();
289 }
290 
291 
292 template <class T>
294 {
295  validateIndex(index);
296  return tableOfObjects_[index].getNonconstObj().ptr();
297 }
298 
299 
300 template <class T>
302 {
303  validateIndex(index);
304  return tableOfObjects_[index].getConstObj().ptr();
305 }
306 
307 
308 template <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 
320 template <class T>
321 void SimpleObjectDB<T>::validateIndex(const int index) const
322 {
323  using Teuchos::as;
325  !(0 <= index && index < as<int>(tableOfObjects_.size())),
326  RangeError,
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 
336 template <class T>
337 template <class T2>
338 int 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 
357 template <class T>
358 void 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 
Null reference error exception class.
Simple object object database.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
RCP< const T > getConstObjRCP(const int index) const
Get an object (const persisting association).
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
RCP< T > removeNonconstObj(const int index)
int storeNonconstObj(const RCP< T > &obj)
Store a non-const object.
RCP< T > getNonconstObjRCP(const int index)
Get an object (nonconst persisting association).
int removeRCP(int &index)
Remove an indexed object from the table.
Ptr< T > getNonconstObjPtr(const int index)
Get an object (nonconst semi-persisting association).
SimpleObjectDB()
Construct an empty DB.
int numObjects() const
Return number of non-null stored objects.
void removeObj(const int index)
Remove a stored object without returning it.
Ptr< const T > getConstObjPtr(const int index) const
Get an object (const semi-persisting association).
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
RCP< SimpleObjectDB< T > > createSimpleObjectDB()
Nonmember constructor.
friend void swap(Array< T2 > &a1, Array< T2 > &a2)
int tableSize() const
Return the current size of the table.
Templated array class derived from the STL std::vector.
Default traits class that just returns typeid(T).name().
int numFreeIndexes() const
Return number of free indexes.
int storeCastedNonconstObj(const RCP< TOld > &robj_old)
Performs an rcp_dynamic_cast&lt;&gt;() to store the obejct.
RCP< const T > removeConstObj(const int index)
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
Smart reference counting pointer class for automatic garbage collection.
Range error exception class.
Definition of Teuchos::as, for conversions between types.
int storeConstObj(const RCP< const T > &obj)
Store a const object.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...
void purge()
Clear out all storage.