Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
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
24namespace Teuchos
25{
26
27template <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
57template <class T>
59{
60
61}
62
63template <class T>
64SimpleObjectTable<T>::~SimpleObjectTable()
65{
66 purge();
67}
68
69template <class T>
70int 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
88template <class T>
89int SimpleObjectTable<T>::storeNew(T* obj, bool owned)
90{
91 return storeRCP(rcp(obj, owned));
92}
93
94template <class T>
95template <class TOld>
96int SimpleObjectTable<T>::storeCastedRCP(const RCP<TOld> & robj_old)
97{
98 return storeRCP(rcp_dynamic_cast<T>(robj_old, true));
99}
100
101template <class T>
102int 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
117template <class T>
118const 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
127template <class T>
128void 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
Templated array class derived from the STL std::vector.
Reference-counted pointer class and non-member templated function implementations.
Smart reference counting pointer class for automatic garbage collection.
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object.
This class provides a central place to store objects.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...