Ref.h

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2005 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 } // namespace oasys
00278 
00279 #endif /* _OASYS_REF_H_ */

Generated on Fri Dec 22 14:48:00 2006 for DTN Reference Implementation by  doxygen 1.5.1