00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "Teuchos_Array.hpp"
00030 #include "Teuchos_RCP.hpp"
00031
00032 #ifndef TEUCHOS_SIMPLEOBJECTTABLE_HPP
00033 #define TEUCHOS_SIMPLEOBJECTTABLE_HPP
00034
00043 namespace Teuchos
00044 {
00045
00046 template <class T>
00047 class SimpleObjectTable
00048 {
00049 public:
00050
00051 SimpleObjectTable();
00052
00053 ~SimpleObjectTable();
00054
00055 int storeRCP(const RCP<T> & robj);
00056
00057 int storeNew(T* obj, bool owned = true);
00058
00059 template <class TOld>
00060 int storeCastedRCP(const RCP<TOld> & robj_old);
00061
00062 int removeRCP(int &index);
00063
00064 const RCP<T> getRCP(int index);
00065
00066 void purge();
00067
00068 private:
00069
00070 Array< RCP<T> > tableOfObjects;
00071
00072 Array< int > freedIndices;
00073
00074 };
00075
00076 template <class T>
00077 SimpleObjectTable<T>::SimpleObjectTable()
00078 {
00079
00080 }
00081
00082 template <class T>
00083 SimpleObjectTable<T>::~SimpleObjectTable()
00084 {
00085 purge();
00086 }
00087
00088 template <class T>
00089 int SimpleObjectTable<T>::storeRCP(const RCP<T> & robj)
00090 {
00091 robj.assert_not_null();
00092
00093 int index = -1;
00094
00095 if (freedIndices.size() != 0) {
00096 index = freedIndices.back();
00097 freedIndices.pop_back();
00098 tableOfObjects[index] = robj;
00099 } else {
00100 tableOfObjects.push_back(robj);
00101 index = tableOfObjects.size() - 1;
00102 }
00103
00104 return index;
00105 }
00106
00107 template <class T>
00108 int SimpleObjectTable<T>::storeNew(T* obj, bool owned)
00109 {
00110 return storeRCP(rcp(obj, owned));
00111 }
00112
00113 template <class T>
00114 template <class TOld>
00115 int SimpleObjectTable<T>::storeCastedRCP(const RCP<TOld> & robj_old)
00116 {
00117 return storeRCP(rcp_dynamic_cast<T>(robj_old, true));
00118 }
00119
00120 template <class T>
00121 int SimpleObjectTable<T>::removeRCP(int &index)
00122 {
00123 if (tableOfObjects[index] == Teuchos::null) {
00124 throw RangeError("Item has already been deleted from SimpleObjectTable.");
00125 }
00126
00127 int cnt = tableOfObjects[index].count();
00128
00129 tableOfObjects[index] = Teuchos::null;
00130 freedIndices.push_back(index);
00131 index = -1;
00132
00133 return (cnt-1);
00134 }
00135
00136 template <class T>
00137 const RCP<T> SimpleObjectTable<T>::getRCP(int index)
00138 {
00139 if (tableOfObjects[index] == Teuchos::null) {
00140 throw RangeError("Item has already been deleted from SimpleObjectTable.");
00141 }
00142
00143 return tableOfObjects[index];
00144 }
00145
00146 template <class T>
00147 void SimpleObjectTable<T>::purge()
00148 {
00149 int ocnt = tableOfObjects.size();
00150 for (int i=0; i<ocnt; i++) {
00151 tableOfObjects[i] = Teuchos::null;
00152 }
00153
00154 if (tableOfObjects.size() > 0)
00155 tableOfObjects.erase(tableOfObjects.begin(), tableOfObjects.end());
00156 if (freedIndices.size() > 0)
00157 freedIndices.erase(freedIndices.begin(), freedIndices.end());
00158 }
00159
00160 }
00161
00162 #endif
00163