PointerCache.h

Go to the documentation of this file.
00001 #ifndef __POINTERCACHE_H__
00002 #define __POINTERCACHE_H__
00003 
00004 #include <map>
00005 
00006 #include "../debug/DebugUtils.h"
00007 
00008 namespace oasys {
00009 
00015 template <
00016     typename _Name,            // namespace for the cache
00017     typename _PtrType          // type of pointer being stored
00018 >
00019 class PointerCache {
00020 public:
00024     PointerCache() : ptr_(0) {
00025         // NOTE! because of the fact that virtual functions cannot be
00026         // called from the constructor, this line of code:
00027         //
00028         // set_ptr(ptr);
00029         // 
00030         // which really should be here needs to be put in the
00031         // constructor of the derived class.
00032     }
00033     
00037     virtual ~PointerCache() {
00038         // NOTE! because of the fact that virtual functions cannot be
00039         // called from the destructor, this line of code:
00040         //
00041         // set_ptr(0);
00042         // 
00043         // which really should be here needs to be put in the
00044         // destructor of the derived class.
00045     }
00046 
00048     _PtrType& operator*() const {
00049         restore_and_update_ptr();       
00050         return *ptr_;
00051     }    
00052     _PtrType* operator->() const {
00053         restore_and_update_ptr();       
00054         return ptr_;
00055     }
00057 
00062     PointerCache& operator=(_PtrType* ptr) {
00063         set_ptr(ptr);
00064         return *this;
00065     }
00066 
00068     PointerCache& operator=(const PointerCache&); 
00069     
00071     _PtrType* ptr() { 
00072         restore_and_update_ptr();       
00073         return ptr_; 
00074     }
00075     
00076 protected:
00077     _PtrType*     ptr_;
00078     
00079     virtual void restore_and_update_ptr() = 0;
00080     virtual bool at_limit(_PtrType* ptr)  = 0;
00081     virtual void evict()                  = 0;
00082 
00083     virtual void register_ptr(_PtrType* ptr)   = 0;
00084     virtual void unregister_ptr(_PtrType* ptr) = 0;
00085 
00086     void set_ptr(_PtrType* ptr) {
00087         if (ptr == ptr_) {
00088             return;
00089         }
00090 
00091         if (ptr_ != 0) {
00092             unregister_ptr(ptr_);
00093             ASSERT(ptr_ == 0);
00094         }
00095 
00096         if (ptr != 0) {
00097             while (at_limit(ptr)) {
00098                 evict();
00099             }
00100             register_ptr(ptr);
00101         }
00102 
00103         ptr_ = ptr;
00104     }
00105 };
00106 
00107 } // namespace oasys
00108 
00109 #endif /* __POINTERCACHE_H__ */

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1