Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_SimpleObjectTable.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 #include "Teuchos_Array.hpp"
11 #include "Teuchos_RCP.hpp"
12 
13 #ifndef TEUCHOS_SIMPLEOBJECTTABLE_HPP
14 #define TEUCHOS_SIMPLEOBJECTTABLE_HPP
15 
24 namespace Teuchos
25 {
26 
27 template <class T>
29 {
30  public:
31 
33 
35 
36  int storeRCP(const RCP<T> & robj);
37 
38  int storeNew(T* obj, bool owned = true);
39 
40  template <class TOld>
41  int storeCastedRCP(const RCP<TOld> & robj_old);
42 
43  int removeRCP(int &index);
44 
45  const RCP<T> getRCP(int index);
46 
47  void purge();
48 
49  private:
50 
52 
54 
55 };
56 
57 template <class T>
59 {
60 
61 }
62 
63 template <class T>
65 {
66  purge();
67 }
68 
69 template <class T>
71 {
72  robj.assert_not_null();
73 
74  int index = -1;
75 
76  if (freedIndices.size() != 0) {
77  index = freedIndices.back();
78  freedIndices.pop_back();
79  tableOfObjects[index] = robj;
80  } else {
81  tableOfObjects.push_back(robj);
82  index = tableOfObjects.size() - 1;
83  }
84 
85  return index;
86 }
87 
88 template <class T>
89 int SimpleObjectTable<T>::storeNew(T* obj, bool owned)
90 {
91  return storeRCP(rcp(obj, owned));
92 }
93 
94 template <class T>
95 template <class TOld>
97 {
98  return storeRCP(rcp_dynamic_cast<T>(robj_old, true));
99 }
100 
101 template <class T>
103 {
104  if (tableOfObjects[index] == Teuchos::null) {
105  throw RangeError("Item has already been deleted from SimpleObjectTable.");
106  }
107 
108  int cnt = tableOfObjects[index].strong_count();
109 
110  tableOfObjects[index] = Teuchos::null;
111  freedIndices.push_back(index);
112  index = -1;
113 
114  return (cnt-1);
115 }
116 
117 template <class T>
119 {
120  if (tableOfObjects[index] == Teuchos::null) {
121  throw RangeError("Item has already been deleted from SimpleObjectTable.");
122  }
123 
124  return tableOfObjects[index];
125 }
126 
127 template <class T>
129 {
130  int ocnt = tableOfObjects.size();
131  for (int i=0; i<ocnt; i++) {
132  tableOfObjects[i] = Teuchos::null;
133  }
134 
135  if (tableOfObjects.size() > 0)
136  tableOfObjects.erase(tableOfObjects.begin(), tableOfObjects.end());
137  if (freedIndices.size() > 0)
138  freedIndices.erase(freedIndices.begin(), freedIndices.end());
139 }
140 
141 } // end namespace Teuchos
142 
143 #endif
144 
This class provides a central place to store objects.
int storeNew(T *obj, bool owned=true)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
int storeCastedRCP(const RCP< TOld > &robj_old)
int storeRCP(const RCP< T > &robj)
const RCP< T > & assert_not_null() const
Throws NullReferenceError if this-&gt;get()==NULL, otherwise returns reference to *this.
Templated array class derived from the STL std::vector.
Smart reference counting pointer class for automatic garbage collection.
Range error exception class.
Reference-counted pointer class and non-member templated function implementations.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...