00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef _OASYS_REF_H_
00039 #define _OASYS_REF_H_
00040
00041 #include "../debug/DebugUtils.h"
00042 #include "TempRef.h"
00043
00044 namespace oasys {
00045
00066 template <typename _Type>
00067 class Ref {
00068 public:
00075 Ref(const char* what1 = "!!DEFAULT REF CONSTRUCTOR!!", const char* what2 = "")
00076 : object_(NULL), what1_(what1), what2_(what2)
00077 {
00078 }
00079
00083 Ref(_Type* object, const char* what1, const char* what2 = "")
00084 : object_(object), what1_(what1), what2_(what2)
00085 {
00086 if (object_)
00087 object->add_ref(what1_, what2_);
00088 }
00089
00090 private:
00100 Ref(_Type* object);
00101
00102 public:
00103
00107 Ref(const TempRef<_Type> temp)
00108 : what1_(temp.what1()),
00109 what2_(temp.what2())
00110 {
00111 object_ = temp.object();
00112 if (object_) {
00113 object_->add_ref(what1_, what2_);
00114 temp.release();
00115 }
00116 }
00117
00121 Ref(const Ref& other)
00122 : what1_(other.what1_),
00123 what2_(other.what2_)
00124 {
00125 object_ = other.object();
00126 if (object_) {
00127 object_->add_ref(what1_, what2_);
00128 }
00129 }
00130
00134 ~Ref()
00135 {
00136 release();
00137 }
00138
00142 void release()
00143 {
00144 if (object_) {
00145 object_->del_ref(what1_, what2_);
00146 object_ = NULL;
00147 }
00148 }
00149
00153 _Type* object() const
00154 {
00155 return object_;
00156 }
00157
00161 const _Type* const_object() const
00162 {
00163 return object_;
00164 }
00165
00169 _Type* operator->() const
00170 {
00171 ASSERT(object_ != 0);
00172 return object_;
00173 }
00174
00178 _Type& operator*() const
00179 {
00180 ASSERT(object_ != 0);
00181 return *object_;
00182 }
00183
00187 void assign(_Type* new_obj)
00188 {
00189 if (object_ != new_obj)
00190 {
00191 if (new_obj != 0) {
00192 new_obj->add_ref(what1_, what2_);
00193 }
00194
00195 if (object_ != 0) {
00196 object_->del_ref(what1_, what2_);
00197 }
00198
00199 object_ = new_obj;
00200 }
00201 }
00202
00206 Ref& operator=(_Type* o)
00207 {
00208 assign(o);
00209 return *this;
00210 }
00211
00215 Ref& operator=(const Ref<_Type>& other)
00216 {
00217 assign(other.object_);
00218 return *this;
00219 }
00220
00224 Ref& operator=(const TempRef<_Type>& temp)
00225 {
00226 if (object_ != temp.object()) {
00227 assign(temp.object());
00228 }
00229 temp.release();
00230 return *this;
00231 }
00232
00236 bool operator==(_Type* o) const
00237 {
00238 return (object_ == o);
00239 }
00240
00244 bool operator==(const Ref<_Type>& other) const
00245 {
00246 return (object_ == other.object_);
00247 }
00248
00252 bool operator!=(_Type* o) const
00253 {
00254 return (object_ != o);
00255 }
00256
00260 bool operator!=(const Ref<_Type>& other) const
00261 {
00262 return (object_ != other.object_);
00263 }
00264
00265 private:
00269 _Type* object_;
00270
00274 const char *what1_, *what2_;
00275 };
00276
00277 }
00278
00279 #endif