|
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 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are 00012 // met: 00013 // 00014 // 1. Redistributions of source code must retain the above copyright 00015 // notice, this list of conditions and the following disclaimer. 00016 // 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // 3. Neither the name of the Corporation nor the names of the 00022 // contributors may be used to endorse or promote products derived from 00023 // this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 // 00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00038 // 00039 // *********************************************************************** 00040 // @HEADER 00041 00042 #ifndef TEUCHOS_SIMPLE_OBJECT_DB_HPP 00043 #define TEUCHOS_SIMPLE_OBJECT_DB_HPP 00044 00045 00046 #include "Teuchos_Array.hpp" 00047 #include "Teuchos_ConstNonconstObjectContainer.hpp" 00048 #include "Teuchos_as.hpp" 00049 00050 00059 namespace Teuchos 00060 { 00061 00062 00085 template <class T> 00086 class SimpleObjectDB 00087 { 00088 public: 00089 00091 SimpleObjectDB(); 00092 00098 int tableSize() const; 00099 00105 int numFreeIndexes() const; 00106 00109 int numObjects() const; 00110 00115 int storeNonconstObj(const RCP<T> &obj); 00116 00121 int storeConstObj(const RCP<const T> &obj); 00122 00129 template <class TOld> 00130 int storeCastedNonconstObj(const RCP<TOld> & robj_old); 00131 00137 void removeObj(const int index); 00138 00144 RCP<T> removeNonconstObj(const int index); 00145 00148 RCP<const T> removeConstObj(const int index); 00149 00158 int removeRCP(int &index); 00159 00165 RCP<T> getNonconstObjRCP(const int index); 00166 00169 RCP<const T> getConstObjRCP(const int index) const; 00170 00176 Ptr<T> getNonconstObjPtr(const int index); 00177 00180 Ptr<const T> getConstObjPtr(const int index) const; 00181 00187 void purge(); 00188 00189 private: 00190 00191 typedef Array<ConstNonconstObjectContainer<T> > tableOfObjects_t; 00192 typedef Array<int> freedIndices_t; 00193 00194 tableOfObjects_t tableOfObjects_; 00195 freedIndices_t freedIndices_; 00196 00197 void validateIndex(const int index) const 00198 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00199 ; 00200 #else 00201 {} 00202 #endif 00203 00204 template <class T2> 00205 int storeObjectImpl(const RCP<T2> &robj); 00206 00207 void removeObjImpl(const int index); 00208 00209 }; 00210 00211 00213 template <class T> 00214 RCP<SimpleObjectDB<T> > createSimpleObjectDB() 00215 { 00216 return rcp(new SimpleObjectDB<T>); 00217 } 00218 00219 00220 // 00221 // Template definitions 00222 // 00223 00224 00225 template <class T> 00226 SimpleObjectDB<T>::SimpleObjectDB() 00227 {} 00228 00229 00230 template <class T> 00231 int SimpleObjectDB<T>::tableSize() const 00232 { 00233 return tableOfObjects_.size(); 00234 } 00235 00236 00237 template <class T> 00238 int SimpleObjectDB<T>::numFreeIndexes() const 00239 { 00240 return freedIndices_.size(); 00241 } 00242 00243 00244 template <class T> 00245 int SimpleObjectDB<T>::numObjects() const 00246 { 00247 return tableSize() - numFreeIndexes(); 00248 } 00249 00250 00251 template <class T> 00252 int SimpleObjectDB<T>::storeNonconstObj(const RCP<T> &obj) 00253 { 00254 return storeObjectImpl(obj); 00255 } 00256 00257 00258 template <class T> 00259 int SimpleObjectDB<T>::storeConstObj(const RCP<const T> &obj) 00260 { 00261 return storeObjectImpl(obj); 00262 } 00263 00264 00265 template <class T> 00266 template <class TOld> 00267 int SimpleObjectDB<T>::storeCastedNonconstObj(const RCP<TOld> & robj_old) 00268 { 00269 return storeNonconstObj(rcp_dynamic_cast<T>(robj_old, true)); 00270 } 00271 00272 00273 template <class T> 00274 void SimpleObjectDB<T>::removeObj(const int index) 00275 { 00276 validateIndex(index); 00277 removeObjImpl(index); 00278 } 00279 00280 00281 template <class T> 00282 RCP<T> SimpleObjectDB<T>::removeNonconstObj(const int index) 00283 { 00284 validateIndex(index); 00285 const RCP<T> obj = tableOfObjects_[index].getNonconstObj(); 00286 removeObjImpl(index); 00287 return obj; 00288 } 00289 00290 00291 template <class T> 00292 RCP<const T> SimpleObjectDB<T>::removeConstObj(const int index) 00293 { 00294 validateIndex(index); 00295 const RCP<const T> obj = tableOfObjects_[index].getConstObj(); 00296 removeObjImpl(index); 00297 return obj; 00298 } 00299 00300 00301 template <class T> 00302 int SimpleObjectDB<T>::removeRCP(int &index) 00303 { 00304 const int index_in = index; 00305 validateIndex(index); 00306 const int cnt = tableOfObjects_[index_in].count(); 00307 removeObjImpl(index_in); 00308 index = -1; 00309 return (cnt - 1); 00310 } 00311 00312 00313 template <class T> 00314 RCP<T> SimpleObjectDB<T>::getNonconstObjRCP(const int index) 00315 { 00316 validateIndex(index); 00317 return tableOfObjects_[index].getNonconstObj(); 00318 } 00319 00320 00321 template <class T> 00322 RCP<const T> SimpleObjectDB<T>::getConstObjRCP(const int index) const 00323 { 00324 validateIndex(index); 00325 return tableOfObjects_[index].getConstObj(); 00326 } 00327 00328 00329 template <class T> 00330 Ptr<T> SimpleObjectDB<T>::getNonconstObjPtr(const int index) 00331 { 00332 validateIndex(index); 00333 return tableOfObjects_[index].getNonconstObj().ptr(); 00334 } 00335 00336 00337 template <class T> 00338 Ptr<const T> SimpleObjectDB<T>::getConstObjPtr(const int index) const 00339 { 00340 validateIndex(index); 00341 return tableOfObjects_[index].getConstObj().ptr(); 00342 } 00343 00344 00345 template <class T> 00346 void SimpleObjectDB<T>::purge() 00347 { 00348 // Wipe out all memory (see Item 82 in "C++ Coding Standards") 00349 tableOfObjects_t().swap(tableOfObjects_); 00350 freedIndices_t().swap(freedIndices_); 00351 } 00352 00353 00354 // private 00355 00356 00357 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00358 template <class T> 00359 void SimpleObjectDB<T>::validateIndex(const int index) const 00360 { 00361 using Teuchos::as; 00362 TEUCHOS_TEST_FOR_EXCEPTION( 00363 !(0 <= index && index < as<int>(tableOfObjects_.size())), 00364 RangeError, 00365 "Error, the object index = " << index << " falls outside of the range" 00366 << " of valid objects [0,"<<tableOfObjects_.size()<<"]"); 00367 const RCP<const T> &obj = tableOfObjects_[index].getConstObj(); 00368 TEUCHOS_TEST_FOR_EXCEPTION(is_null(obj), NullReferenceError, 00369 "Error, the object at index "<<index<<" of type " 00370 <<TypeNameTraits<T>::name()<<" has already been deleted!"); 00371 } 00372 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00373 00374 00375 template <class T> 00376 template <class T2> 00377 int SimpleObjectDB<T>::storeObjectImpl(const RCP<T2> & robj) 00378 { 00379 robj.assert_not_null(); 00380 00381 int index = -1; 00382 00383 if (freedIndices_.size() != 0) { 00384 index = freedIndices_.back(); 00385 freedIndices_.pop_back(); 00386 tableOfObjects_[index].initialize(robj); 00387 } else { 00388 tableOfObjects_.push_back(robj); 00389 index = tableOfObjects_.size() - 1; 00390 } 00391 00392 return index; 00393 } 00394 00395 00396 template <class T> 00397 void SimpleObjectDB<T>::removeObjImpl(const int index) 00398 { 00399 tableOfObjects_[index] = null; 00400 freedIndices_.push_back(index); 00401 } 00402 00403 00404 } // end namespace Teuchos 00405 00406 00407 #endif // TEUCHOS_SIMPLE_OBJECT_DB_HPP 00408
1.7.6.1