TempRef.h

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2005-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 
00018 #ifndef _OASYS_TEMPREF_H_
00019 #define _OASYS_TEMPREF_H_
00020 
00021 #include "../debug/DebugUtils.h"
00022 
00023 namespace oasys {
00024 
00036 template <typename _Type>
00037 class TempRef {
00038 public:
00042     TempRef(const char* what1, const char* what2 = "")
00043         : object_(NULL), what1_(what1), what2_(what2)
00044     {
00045     }
00046     
00050     TempRef(_Type* object, const char* what1, const char* what2 = "")
00051         : object_(object), what1_(what1), what2_(what2)
00052     {
00053     }
00054 
00058     TempRef(const TempRef& other)
00059     {
00060         object_ = other.object();
00061         other.release();
00062     }
00063     
00067     ~TempRef() {
00068         if (object_ != NULL) {
00069             PANIC("TempRef %s %s destructor fired but object still exists!!",
00070                   what1_, what2_);
00071         }
00072     }
00073 
00077     TempRef& operator=(const TempRef<_Type>& other)
00078     {
00079         object_ = other.object();
00080         other.release();
00081         return *this;
00082     }
00083 
00087     TempRef& operator=(_Type* object)
00088     {
00089         ASSERTF(object_ == NULL,
00090                 "TempRef can only assign to null reference");
00091         object_ = object;
00092         return *this;
00093     }
00094 
00098     _Type* object() const {
00099         return object_;
00100     }
00101     
00105     _Type* operator->() const
00106     {
00107         return object_;
00108     }
00109 
00113     _Type& operator*() const
00114     {
00115         return *object_;
00116     }
00117 
00122     void release() const {
00123         object_ = NULL;
00124     }
00125 
00129     bool operator==(_Type* o)
00130     {
00131         return (object_ == o);
00132     }
00133     
00137     bool operator!=(_Type* o)
00138     {
00139         return (object_ != o);
00140     }
00141 
00142     const char* what1() const { return what1_; } 
00143     const char* what2() const { return what2_; }
00144     
00145 private:
00149     mutable _Type* object_;
00150 
00154     const char *what1_, *what2_;
00155 };
00156 
00157 } // namespace oasys
00158 
00159 #endif /* _OASYS_TEMPREF_H_ */

Generated on Sat Sep 8 08:36:18 2007 for DTN Reference Implementation by  doxygen 1.5.3