00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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 }
00158
00159 #endif