PointerCache.h

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 #ifndef __POINTERCACHE_H__
00018 #define __POINTERCACHE_H__
00019 
00020 #include <map>
00021 
00022 #include "../debug/DebugUtils.h"
00023 
00024 namespace oasys {
00025 
00031 template <
00032     typename _Name,            // namespace for the cache
00033     typename _PtrType          // type of pointer being stored
00034 >
00035 class PointerCache {
00036 public:
00040     PointerCache() : ptr_(0) {
00041         // NOTE! because of the fact that virtual functions cannot be
00042         // called from the constructor, this line of code:
00043         //
00044         // set_ptr(ptr);
00045         // 
00046         // which really should be here needs to be put in the
00047         // constructor of the derived class.
00048     }
00049     
00053     virtual ~PointerCache() {
00054         // NOTE! because of the fact that virtual functions cannot be
00055         // called from the destructor, this line of code:
00056         //
00057         // set_ptr(0);
00058         // 
00059         // which really should be here needs to be put in the
00060         // destructor of the derived class.
00061     }
00062 
00064     _PtrType& operator*() const {
00065         restore_and_update_ptr();       
00066         return *ptr_;
00067     }    
00068     _PtrType* operator->() const {
00069         restore_and_update_ptr();       
00070         return ptr_;
00071     }
00073 
00078     PointerCache& operator=(_PtrType* ptr) {
00079         set_ptr(ptr);
00080         return *this;
00081     }
00082 
00084     PointerCache& operator=(const PointerCache&); 
00085     
00087     _PtrType* ptr() { 
00088         restore_and_update_ptr();       
00089         return ptr_; 
00090     }
00091     
00092 protected:
00093     _PtrType*     ptr_;
00094     
00095     virtual void restore_and_update_ptr() = 0;
00096     virtual bool at_limit(_PtrType* ptr)  = 0;
00097     virtual void evict()                  = 0;
00098 
00099     virtual void register_ptr(_PtrType* ptr)   = 0;
00100     virtual void unregister_ptr(_PtrType* ptr) = 0;
00101 
00102     void set_ptr(_PtrType* ptr) {
00103         if (ptr == ptr_) {
00104             return;
00105         }
00106 
00107         if (ptr_ != 0) {
00108             unregister_ptr(ptr_);
00109             ASSERT(ptr_ == 0);
00110         }
00111 
00112         if (ptr != 0) {
00113             while (at_limit(ptr)) {
00114                 evict();
00115             }
00116             register_ptr(ptr);
00117         }
00118 
00119         ptr_ = ptr;
00120     }
00121 };
00122 
00123 } // namespace oasys
00124 
00125 #endif /* __POINTERCACHE_H__ */

Generated on Thu Jun 7 16:56:51 2007 for DTN Reference Implementation by  doxygen 1.5.1