weakptr.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2009 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 **    Harry Storbacka
00028 */
00029 
00032 
00033 
00034 #pragma once
00035 
00036 
00037 #include "../api_core.h"
00038 #include "sharedptr.h"
00039 
00047 template <typename Type>
00048 class CL_WeakPtr : public CL_SharedPtr_Link
00049 {
00050 public:
00051         CL_WeakPtr()
00052         : ptr(0)
00053         {
00054                 set_weak_link();
00055         }
00056 
00057         CL_WeakPtr(const CL_WeakPtr<Type> &copy)
00058         : ptr(0)
00059         {
00060                 set_weak_link();
00061                 connect(copy);
00062                 ptr = copy.ptr;
00063         }
00064 
00065         explicit CL_WeakPtr(const CL_SharedPtr<Type> &copy)
00066         : ptr(0)
00067         {
00068                 set_weak_link();
00069                 connect(copy);
00070                 ptr = copy.ptr;
00071         }
00072 
00073         explicit CL_WeakPtr(const CL_UnknownSharedPtr &copy)
00074         : ptr(0)
00075         {
00076                 set_weak_link();
00077                 connect(copy);
00078                 ptr = (Type *) copy.ptr;
00079         }
00080 
00081         ~CL_WeakPtr()
00082         {
00083                 disconnect();
00084         }
00085 
00086         CL_WeakPtr &operator =(const CL_UnknownSharedPtr &copy)
00087         {
00088                 disconnect();
00089                 connect(copy);
00090                 ptr = (Type *) copy.ptr;
00091                 set_weak_link();
00092                 return *this;
00093         }
00094 
00095         CL_WeakPtr &operator =(const CL_SharedPtr<Type> &copy)
00096         {
00097                 disconnect();
00098                 connect(copy);
00099                 ptr = copy.ptr;
00100                 set_weak_link();
00101                 return *this;
00102         }
00103 
00104         CL_WeakPtr &operator =(const CL_WeakPtr &copy)
00105         {
00106                 if (this == &copy)
00107                         return *this;
00108                 disconnect();
00109                 connect(copy);
00110                 ptr = copy.ptr;
00111                 set_weak_link();
00112                 return *this;
00113         }
00114 
00116 
00117         bool disconnect()
00118         {
00119                 bool result = CL_SharedPtr_Link::disconnect();
00120                 ptr = NULL;
00121                 return result;
00122         }
00123 
00125 
00126         bool is_null() const { return is_invalid_weak_link() || ptr == 0; }
00127 
00129 
00130         Type *get() { return !is_invalid_weak_link() ? ptr : 0; }
00131 
00133 
00134         const Type *get() const { return !is_invalid_weak_link() ? ptr : 0; }
00135 
00136         CL_UnknownSharedPtr to_unknownptr() const
00137         {
00138                 if (!is_invalid_weak_link())
00139                         return CL_UnknownSharedPtr(*this, (Type *) ptr);
00140                 else
00141                         return CL_UnknownSharedPtr();
00142         }
00143 
00144         CL_SharedPtr<Type> to_sharedptr() const
00145         {
00146                 if (!is_invalid_weak_link())
00147                         return CL_SharedPtr<Type>(*this, (Type *) ptr);
00148                 else
00149                         return CL_SharedPtr<Type>();
00150         }
00151 
00152         operator Type *() { return get(); }
00153 
00154         operator const Type *() const { return get(); }
00155 
00156         operator CL_UnknownSharedPtr() { return to_unknownptr(); }
00157 
00158         operator CL_UnknownSharedPtr() const { return to_unknownptr(); }
00159 
00160         operator CL_SharedPtr<Type>() { return to_sharedptr(); }
00161 
00162         operator CL_SharedPtr<Type>() const { return to_sharedptr(); }
00163 
00164         Type *operator ->() { return get(); }
00165 
00166         const Type *operator ->() const { return get(); }
00167 
00168         template <typename OtherType>
00169         bool operator ==(OtherType *other) const { return ptr == other; }
00170 
00171         template <typename OtherType>
00172         bool operator !=(OtherType *other) const { return ptr != other; }
00173 
00174         template <typename OtherType>
00175         bool operator <(OtherType *other) const { return ptr < other; }
00176 
00177         template <typename OtherType>
00178         bool operator <=(OtherType *other) const { return ptr <= other; }
00179 
00180         template <typename OtherType>
00181         bool operator >(OtherType *other) const { return ptr > other; }
00182 
00183         template <typename OtherType>
00184         bool operator >=(OtherType *other) const { return ptr >= other; }
00185 
00186 public:
00187         Type *ptr;
00188 };
00189 
00190 

Generated on Thu Dec 3 02:39:33 2009 for ClanLib by  doxygen 1.4.6