Teuchos - Trilinos Tools Package Version of the Day
Teuchos_SimpleObjectDB.hpp
Go to the documentation of this file.
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 
00199   template <class T2>
00200   int storeObjectImpl(const RCP<T2> &robj);
00201   
00202   void removeObjImpl(const int index);
00203 
00204 };
00205 
00206 
00208 template <class T>
00209 RCP<SimpleObjectDB<T> > createSimpleObjectDB()
00210 {
00211   return rcp(new SimpleObjectDB<T>);
00212 }
00213 
00214 
00215 //
00216 // Template definitions
00217 //
00218 
00219 
00220 template <class T>
00221 SimpleObjectDB<T>::SimpleObjectDB()
00222 {}
00223 
00224 
00225 template <class T>
00226 int SimpleObjectDB<T>::tableSize() const
00227 {
00228   return tableOfObjects_.size();
00229 }
00230 
00231 
00232 template <class T>
00233 int SimpleObjectDB<T>::numFreeIndexes() const
00234 {
00235   return freedIndices_.size();
00236 }
00237 
00238 
00239 template <class T>
00240 int SimpleObjectDB<T>::numObjects() const
00241 {
00242   return tableSize() - numFreeIndexes();
00243 }
00244 
00245 
00246 template <class T>
00247 int SimpleObjectDB<T>::storeNonconstObj(const RCP<T> &obj)
00248 {
00249   return storeObjectImpl(obj);
00250 }
00251 
00252 
00253 template <class T>
00254 int SimpleObjectDB<T>::storeConstObj(const RCP<const T> &obj)
00255 {
00256   return storeObjectImpl(obj);
00257 }
00258 
00259 
00260 template <class T>
00261 template <class TOld>
00262 int SimpleObjectDB<T>::storeCastedNonconstObj(const RCP<TOld> & robj_old)
00263 {
00264   return storeNonconstObj(rcp_dynamic_cast<T>(robj_old, true));
00265 }
00266 
00267 
00268 template <class T>
00269 void SimpleObjectDB<T>::removeObj(const int index)
00270 {
00271   validateIndex(index);
00272   removeObjImpl(index);
00273 }
00274 
00275 
00276 template <class T>
00277 RCP<T> SimpleObjectDB<T>::removeNonconstObj(const int index)
00278 {
00279   validateIndex(index);
00280   const RCP<T> obj = tableOfObjects_[index].getNonconstObj();
00281   removeObjImpl(index);
00282   return obj;
00283 }
00284 
00285 
00286 template <class T>
00287 RCP<const T> SimpleObjectDB<T>::removeConstObj(const int index)
00288 {
00289   validateIndex(index);
00290   const RCP<const T> obj = tableOfObjects_[index].getConstObj();
00291   removeObjImpl(index);
00292   return obj;
00293 }
00294 
00295 
00296 template <class T>
00297 int SimpleObjectDB<T>::removeRCP(int &index)
00298 {
00299   const int index_in = index;
00300   validateIndex(index);
00301   const int cnt = tableOfObjects_[index_in].count();
00302   removeObjImpl(index_in);
00303   index = -1;
00304   return (cnt - 1);
00305 }
00306 
00307 
00308 template <class T>
00309 RCP<T> SimpleObjectDB<T>::getNonconstObjRCP(const int index)
00310 {
00311   validateIndex(index);
00312   return tableOfObjects_[index].getNonconstObj();
00313 }
00314 
00315 
00316 template <class T>
00317 RCP<const T> SimpleObjectDB<T>::getConstObjRCP(const int index) const
00318 {
00319   validateIndex(index);
00320   return tableOfObjects_[index].getConstObj();
00321 }
00322 
00323 
00324 template <class T>
00325 Ptr<T> SimpleObjectDB<T>::getNonconstObjPtr(const int index)
00326 {
00327   validateIndex(index);
00328   return tableOfObjects_[index].getNonconstObj().ptr();
00329 }
00330 
00331 
00332 template <class T>
00333 Ptr<const T> SimpleObjectDB<T>::getConstObjPtr(const int index) const
00334 {
00335   validateIndex(index);
00336   return tableOfObjects_[index].getConstObj().ptr();
00337 }
00338 
00339 
00340 template <class T>
00341 void SimpleObjectDB<T>::purge()
00342 {
00343   // Wipe out all memory (see Item 82 in "C++ Coding Standards")
00344   tableOfObjects_t().swap(tableOfObjects_);
00345   freedIndices_t().swap(freedIndices_);
00346 }
00347 
00348 
00349 // private
00350 
00351 
00352 template <class T>
00353 void SimpleObjectDB<T>::validateIndex(const int index) const
00354 {
00355   using Teuchos::as;
00356   TEUCHOS_TEST_FOR_EXCEPTION(
00357     !(0 <= index && index < as<int>(tableOfObjects_.size())),
00358     RangeError,
00359     "Error, the object index = " << index << " falls outside of the range"
00360     << " of valid objects [0,"<<tableOfObjects_.size()<<"]");
00361   const RCP<const T> &obj = tableOfObjects_[index].getConstObj();
00362   TEUCHOS_TEST_FOR_EXCEPTION(is_null(obj), NullReferenceError,
00363     "Error, the object at index "<<index<<" of type "
00364     <<TypeNameTraits<T>::name()<<" has already been deleted!");
00365 }
00366 
00367 
00368 template <class T>
00369 template <class T2>
00370 int SimpleObjectDB<T>::storeObjectImpl(const RCP<T2> & robj)
00371 {
00372   robj.assert_not_null();
00373 
00374   int index = -1;
00375 
00376   if (freedIndices_.size() != 0) {
00377     index = freedIndices_.back();
00378     freedIndices_.pop_back();
00379     tableOfObjects_[index].initialize(robj);
00380   } else {
00381     tableOfObjects_.push_back(robj);
00382     index = tableOfObjects_.size() - 1;
00383   }
00384 
00385   return index;
00386 }
00387 
00388 
00389 template <class T>
00390 void SimpleObjectDB<T>::removeObjImpl(const int index)
00391 {
00392   tableOfObjects_[index] = null;
00393   freedIndices_.push_back(index);
00394 }
00395 
00396 
00397 } // end namespace Teuchos
00398 
00399 
00400 #endif // TEUCHOS_SIMPLE_OBJECT_DB_HPP
00401 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines