Teuchos - Trilinos Tools Package  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 
51  Array< RCP<T> > tableOfObjects;
52 
53  Array< int > freedIndices;
54 
55 };
56 
57 template <class T>
59 {
60 
61 }
62 
63 template <class T>
64 SimpleObjectTable<T>::~SimpleObjectTable()
65 {
66  purge();
67 }
68 
69 template <class T>
70 int SimpleObjectTable<T>::storeRCP(const RCP<T> & robj)
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>
96 int SimpleObjectTable<T>::storeCastedRCP(const RCP<TOld> & robj_old)
97 {
98  return storeRCP(rcp_dynamic_cast<T>(robj_old, true));
99 }
100 
101 template <class T>
102 int SimpleObjectTable<T>::removeRCP(int &index)
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>
118 const RCP<T> SimpleObjectTable<T>::getRCP(int index)
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>
128 void SimpleObjectTable<T>::purge()
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.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated array class derived from the STL std::vector.
Smart reference counting pointer class for automatic garbage collection.
Reference-counted pointer class and non-member templated function implementations.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...