Ref.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 #ifndef _OASYS_REF_H_
00018 #define _OASYS_REF_H_
00019 
00020 #include "../debug/DebugUtils.h"
00021 #include "TempRef.h"
00022 
00023 namespace oasys {
00024 
00045 template <typename _Type>
00046 class Ref {
00047 public:
00054     Ref(const char* what1 = "!!DEFAULT REF CONSTRUCTOR!!", const char* what2 = "")
00055         : object_(NULL), what1_(what1), what2_(what2)
00056     {
00057     }
00058     
00062     Ref(_Type* object, const char* what1, const char* what2 = "")
00063         : object_(object), what1_(what1), what2_(what2)
00064     {
00065         if (object_)
00066             object->add_ref(what1_, what2_);
00067     }
00068 
00069 private:
00079     Ref(_Type* object);
00080 
00081 public:
00082 
00086     Ref(const TempRef<_Type> temp)
00087         : what1_(temp.what1()),
00088           what2_(temp.what2())
00089     {
00090         object_ = temp.object();
00091         if (object_) {
00092             object_->add_ref(what1_, what2_);
00093             temp.release();
00094         }
00095     }
00096 
00100     Ref(const Ref& other)
00101         : what1_(other.what1_),
00102           what2_(other.what2_)
00103     {
00104         object_ = other.object();
00105         if (object_) {
00106             object_->add_ref(what1_, what2_);
00107         }
00108     }
00109 
00113     ~Ref()
00114     {
00115         release();
00116     }
00117 
00121     void release()
00122     {
00123         if (object_) {
00124             object_->del_ref(what1_, what2_);
00125             object_ = NULL;
00126         }
00127     }
00128 
00132     _Type* object() const
00133     {
00134         return object_;
00135     }
00136 
00140     const _Type* const_object() const
00141     {
00142         return object_;
00143     }
00144 
00148     _Type* operator->() const
00149     {
00150         ASSERT(object_ != 0);
00151         return object_;
00152     }
00153 
00157     _Type& operator*() const
00158     {
00159         ASSERT(object_ != 0);
00160         return *object_;
00161     }
00162 
00166     void assign(_Type* new_obj)
00167     {
00168         if (object_ != new_obj)
00169         {
00170             if (new_obj != 0) {
00171                 new_obj->add_ref(what1_, what2_);
00172             }
00173             
00174             if (object_ != 0) {
00175                 object_->del_ref(what1_, what2_);
00176             }
00177 
00178             object_ = new_obj;
00179         }
00180     }
00181 
00185     Ref& operator=(_Type* o)
00186     {
00187         assign(o);
00188         return *this;
00189     }
00190 
00194     Ref& operator=(const Ref<_Type>& other)
00195     {
00196         assign(other.object_);
00197         return *this;
00198     }
00199 
00203     Ref& operator=(const TempRef<_Type>& temp)
00204     {
00205         if (object_ != temp.object()) {
00206             assign(temp.object());
00207         }
00208         temp.release();
00209         return *this;
00210     }
00211     
00215     bool operator==(_Type* o) const
00216     {
00217         return (object_ == o);
00218     }
00219      
00223     bool operator==(const Ref<_Type>& other) const
00224     {
00225         return (object_ == other.object_);
00226     }
00227     
00231     bool operator!=(_Type* o) const
00232     {
00233         return (object_ != o);
00234     }
00235     
00239     bool operator!=(const Ref<_Type>& other) const
00240     {
00241         return (object_ != other.object_);
00242     }
00243     
00244 private:
00248     _Type* object_;
00249 
00253     const char *what1_, *what2_;
00254 };
00255 
00256 } // namespace oasys
00257 
00258 #endif /* _OASYS_REF_H_ */

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