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//
4// Teuchos: Common Tools Package
5// Copyright (2004) Sandia Corporation
6//
7// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8// license for use of this work by or on behalf of the U.S. Government.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38//
39// ***********************************************************************
40// @HEADER
41
42#ifndef TEUCHOS_SIMPLE_OBJECT_DB_HPP
43#define TEUCHOS_SIMPLE_OBJECT_DB_HPP
44
45
46#include "Teuchos_Array.hpp"
47#include "Teuchos_ConstNonconstObjectContainer.hpp"
48#include "Teuchos_as.hpp"
49
50
59namespace Teuchos
60{
61
62
85template <class T>
87{
88public:
89
92
98 int tableSize() const;
99
105 int numFreeIndexes() const;
106
109 int numObjects() const;
110
115 int storeNonconstObj(const RCP<T> &obj);
116
121 int storeConstObj(const RCP<const T> &obj);
122
129 template <class TOld>
130 int storeCastedNonconstObj(const RCP<TOld> & robj_old);
131
137 void removeObj(const int index);
138
144 RCP<T> removeNonconstObj(const int index);
145
148 RCP<const T> removeConstObj(const int index);
149
158 int removeRCP(int &index);
159
165 RCP<T> getNonconstObjRCP(const int index);
166
169 RCP<const T> getConstObjRCP(const int index) const;
170
176 Ptr<T> getNonconstObjPtr(const int index);
177
180 Ptr<const T> getConstObjPtr(const int index) const;
181
187 void purge();
188
189private:
190
193
194 tableOfObjects_t tableOfObjects_;
195 freedIndices_t freedIndices_;
196
197 void validateIndex(const int index) const;
198
199 template <class T2>
200 int storeObjectImpl(const RCP<T2> &robj);
201
202 void removeObjImpl(const int index);
203
204};
205
206
208template <class T>
210{
211 return rcp(new SimpleObjectDB<T>);
212}
213
214
215//
216// Template definitions
217//
218
219
220template <class T>
222{}
223
224
225template <class T>
227{
228 return tableOfObjects_.size();
229}
230
231
232template <class T>
234{
235 return freedIndices_.size();
236}
237
238
239template <class T>
241{
242 return tableSize() - numFreeIndexes();
243}
244
245
246template <class T>
248{
249 return storeObjectImpl(obj);
250}
251
252
253template <class T>
255{
256 return storeObjectImpl(obj);
257}
258
259
260template <class T>
261template <class TOld>
263{
264 return storeNonconstObj(rcp_dynamic_cast<T>(robj_old, true));
265}
266
267
268template <class T>
269void SimpleObjectDB<T>::removeObj(const int index)
270{
271 validateIndex(index);
272 removeObjImpl(index);
273}
274
275
276template <class T>
278{
279 validateIndex(index);
280 const RCP<T> obj = tableOfObjects_[index].getNonconstObj();
281 removeObjImpl(index);
282 return obj;
283}
284
285
286template <class T>
288{
289 validateIndex(index);
290 const RCP<const T> obj = tableOfObjects_[index].getConstObj();
291 removeObjImpl(index);
292 return obj;
293}
294
295
296template <class T>
298{
299 const int index_in = index;
300 validateIndex(index);
301 const int cnt = tableOfObjects_[index_in].count();
302 removeObjImpl(index_in);
303 index = -1;
304 return (cnt - 1);
305}
306
307
308template <class T>
310{
311 validateIndex(index);
312 return tableOfObjects_[index].getNonconstObj();
313}
314
315
316template <class T>
318{
319 validateIndex(index);
320 return tableOfObjects_[index].getConstObj();
321}
322
323
324template <class T>
326{
327 validateIndex(index);
328 return tableOfObjects_[index].getNonconstObj().ptr();
329}
330
331
332template <class T>
334{
335 validateIndex(index);
336 return tableOfObjects_[index].getConstObj().ptr();
337}
338
339
340template <class T>
342{
343 // Wipe out all memory (see Item 82 in "C++ Coding Standards")
344 tableOfObjects_t().swap(tableOfObjects_);
345 freedIndices_t().swap(freedIndices_);
346}
347
348
349// private
350
351
352template <class T>
353void SimpleObjectDB<T>::validateIndex(const int index) const
354{
355 using Teuchos::as;
357 !(0 <= index && index < as<int>(tableOfObjects_.size())),
359 "Error, the object index = " << index << " falls outside of the range"
360 << " of valid objects [0,"<<tableOfObjects_.size()<<"]");
361 const RCP<const T> &obj = tableOfObjects_[index].getConstObj();
363 "Error, the object at index "<<index<<" of type "
364 <<TypeNameTraits<T>::name()<<" has already been deleted!");
365}
366
367
368template <class T>
369template <class T2>
370int SimpleObjectDB<T>::storeObjectImpl(const RCP<T2> & robj)
371{
372 robj.assert_not_null();
373
374 int index = -1;
375
376 if (freedIndices_.size() != 0) {
377 index = freedIndices_.back();
378 freedIndices_.pop_back();
379 tableOfObjects_[index].initialize(robj);
380 } else {
381 tableOfObjects_.push_back(robj);
382 index = tableOfObjects_.size() - 1;
383 }
384
385 return index;
386}
387
388
389template <class T>
390void SimpleObjectDB<T>::removeObjImpl(const int index)
391{
392 tableOfObjects_[index] = null;
393 freedIndices_.push_back(index);
394}
395
396
397} // end namespace Teuchos
398
399
400#endif // TEUCHOS_SIMPLE_OBJECT_DB_HPP
401
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.
friend void swap(Array< T2 > &a1, Array< T2 > &a2)
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.
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.
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.