00001 /* 00002 ** ClanLib SDK 00003 ** Copyright (c) 1997-2005 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 link to may have additional 00022 ** requirements or restrictions. 00023 ** 00024 ** File Author(s): 00025 ** 00026 ** Magnus Norddahl 00027 */ 00028 00031 00032 #ifndef header_weakptr 00033 #define header_weakptr 00034 00035 #include "sharedptr.h" 00036 00037 //: Weak pointer class (pointer to a CL_SharedPtr object that dont increase reference count). 00038 //- !group=Core/System! 00039 //- !header=core.h! 00040 //- <p>Use CL_WeakPtr when you want to have a pointer that is reference counted by CL_SharedPtr 00041 //- but want a pointer that dont increase the reference count.</p> 00042 //- <p>The purpose of CL_WeakPtr is to avoid circular loop issues. By using CL_WeakPtr you 00043 //- can construct new CL_SharedPtr'ed objects based on the weak pointer.</p> 00044 template <typename T, typename U = T> 00045 class CL_WeakPtr 00046 { 00048 public: 00049 CL_WeakPtr() : impl(0) { return; } 00050 00051 CL_WeakPtr(CL_SharedPtr<T, U> &other) : impl(other.get_impl()) { return; } 00052 00054 public: 00055 //: Returns true if this CL_SharedPtr is not dereferencable. 00056 bool is_null() const { return impl ? (impl->ptr == 0) : true; } 00057 00058 //: Returns number of references (including this one) to the data cache. 00059 //- <p> Returns 0 if this pointer is null. </p> 00060 int get_ref_count() const { if (impl == 0) return 0; return impl->ref_count; } 00061 00062 //: Gives access to the pointer itself. 00063 //- <p> Be careful not to keep the returned pointer around after doing any 00064 //- non-const operations on the CL_LazyCopyPtr; it could be invalid 00065 //- after that.</p> 00066 U* get() { return (U*) ((impl != 0) ? impl->ptr : 0); } 00067 00068 const U* get() const { return (const U*) ((impl != 0) ? impl->ptr : 0); } 00069 00070 //: Returns the pointer. 00071 operator U*() { return get(); } 00072 00073 //: Returns the pointer. 00074 operator const U*() const { return get(); } 00075 00076 //: Returns the pointer as a shared ptr. 00077 operator CL_SharedPtr<T, U>() const { return CL_SharedPtr<T, U>(impl); } 00078 00079 //: Dereferencing operator. 00080 U& operator*() { return *((U*) impl->ptr); } 00081 00082 U const& operator*() const { return *((const U*) impl->ptr); } 00083 00084 //: Indirect member access operator. 00085 U* operator->() { return (U*) impl->ptr; } 00086 00087 U const* operator->() const { return (const U*) impl->ptr; } 00088 00089 //: Pointer equality check operator. 00090 //- <p> This will return true if the CL_SharedPtrs point to the same data. It doesn't 00091 //- check the data itself for equality. </p> 00092 bool operator==(const T* other) const { return other == ((impl != 0) ? impl->ptr : 0); } 00093 00094 bool operator==(const CL_SharedPtr<T, U>& other) const { return other.impl == impl; } 00095 00096 //: Copy assignment operator. 00097 CL_WeakPtr<T, U>& operator=(const CL_WeakPtr<T, U>& other) 00098 { 00099 impl = other.impl; 00100 return *this; 00101 } 00102 00104 private: 00105 CL_SharedPtr_Generic<T> *impl; 00106 }; 00107 00108 #endif
1.4.1