|
Teuchos Package Browser (Single Doxygen Collection) Version of the Day
|
00001 // @HEADER 00002 // *********************************************************************** 00003 // 00004 // Teuchos: Common Tools Package 00005 // Copyright (2004) Sandia Corporation 00006 // 00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00008 // license for use of this work by or on behalf of the U.S. Government. 00009 // 00010 // This library is free software; you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as 00012 // published by the Free Software Foundation; either version 2.1 of the 00013 // License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 // USA 00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00025 // 00026 // *********************************************************************** 00027 // @HEADER 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 } // end namespace Teuchos 00161 00162 #endif 00163
1.7.4