PointerHandle.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 __POINTERHANDLE_H__
00018 #define __POINTERHANDLE_H__
00019 
00020 #include <map>
00021 
00022 #include "../debug/DebugUtils.h"
00023 
00024 namespace oasys {
00025 
00031 template <
00032     typename _PtrType          // type of pointer being stored
00033 >
00034 class PointerHandle {
00035 public:
00039     PointerHandle() : ptr_(0) {}
00040     
00044     virtual ~PointerHandle() { ptr_ = 0; }
00045 
00047     _PtrType& operator*() const {
00048         restore_and_update();
00049         return *ptr_;
00050     }    
00051     _PtrType* operator->() const {
00052         restore_and_update();
00053         return ptr_;
00054     }
00056 
00058     _PtrType* ptr() const { 
00059         restore_and_update();
00060         return ptr_; 
00061     }
00062     
00063 protected:
00064     mutable _PtrType* ptr_;
00065 
00070     virtual void invalidate() const = 0;
00071     
00076     virtual void restore() const = 0;
00077 
00081     virtual void update() const = 0;
00082 
00083     void restore_and_update() const {
00084         if (ptr_ == 0) {
00085             restore();
00086             ASSERT(ptr_ != 0);
00087         }
00088         update();
00089     }
00090 };
00091 
00092 } // namespace oasys
00093 
00094 #endif /* __POINTERHANDLE_H__ */

Generated on Thu Jun 7 12:54:28 2007 for DTN Reference Implementation by  doxygen 1.5.1