TCSharedPtr.h

Go to the documentation of this file.
00001 //*******************************************************************************
00002 //
00003 // *******   ***   ***               *
00004 //    *     *     *                  *
00005 //    *    *      *                *****
00006 //    *    *       ***  *   *   **   *    **    ***
00007 //    *    *          *  * *   *     *   ****  * * *
00008 //    *     *         *   *      *   * * *     * * *
00009 //    *      ***   ***    *     **   **   **   *   *
00010 //                        *
00011 //*******************************************************************************
00012 // see http://sourceforge.net/projects/tcsystem/ for details.
00013 // Copyright (C) 2003 - 2008 Thomas Goessler. All Rights Reserved. 
00014 //*******************************************************************************
00015 //
00016 // TCSystem is the legal property of its developers.
00017 // Please refer to the COPYRIGHT file distributed with this source distribution.
00018 // 
00019 // This library is free software; you can redistribute it and/or             
00020 // modify it under the terms of the GNU Lesser General Public                
00021 // License as published by the Free Software Foundation; either              
00022 // version 2.1 of the License, or (at your option) any later version.        
00023 //                                                                           
00024 // This library is distributed in the hope that it will be useful,           
00025 // but WITHOUT ANY WARRANTY; without even the implied warranty of            
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         
00027 // Lesser General Public License for more details.                           
00028 //                                                                           
00029 // You should have received a copy of the GNU Lesser General Public          
00030 // License along with this library; if not, write to the Free Software       
00031 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
00032 //*******************************************************************************
00033 //  $Id: TCSharedPtr.h 863 2008-07-14 20:54:39Z the_____tiger $
00034 //*******************************************************************************
00035 #ifndef _TC_SHARED_PTR_H_
00036 #define _TC_SHARED_PTR_H_
00037 
00038 #include "TCTypes.h"
00039 #include "TCDelete.h"
00040 #include "TCSharedPtrImpl.h"
00041 
00042 namespace TC
00043 {
00044    template<class T> class SharedPtr;
00045    template<class T> class WeakPtr;
00046 
00059    template<class T> struct SharedPtrTraits { typedef T& Reference; };
00060    template<> struct SharedPtrTraits<void> { typedef void Reference; };
00061    template<> struct SharedPtrTraits<const void> { typedef void Reference; };
00062    template<> struct SharedPtrTraits<const volatile void> { typedef void Reference; };
00063 
00078    template <class T>
00079    class SharedPtr
00080    {
00081    private:
00082       typedef SharedPtr<T> ThisType;
00083 
00084    public:
00085       typedef T  ValueType;
00086       typedef T* PointerType;
00087       typedef typename SharedPtrTraits<T>::Reference ReferenceType;
00088 
00089    public:
00091       SharedPtr():m_ptr(0), m_count() {}
00092 
00094       template <class N_PTR>
00095       explicit SharedPtr(N_PTR* ptr):m_ptr(ptr), m_count(ptr, CheckedDelete()) {}
00096 
00098       template <class N_PTR, class DELETER>
00099       SharedPtr(N_PTR* ptr, DELETER deleter):m_ptr(ptr), m_count(ptr, deleter) {}
00100 
00102       template <class N_PTR>
00103       SharedPtr(std::auto_ptr<N_PTR>& ptr):m_ptr(ptr.get()), m_count(ptr, CheckedDelete()) {}
00104 
00106       template<class N_PTR>
00107       explicit SharedPtr(const WeakPtr<N_PTR>& ptr):m_ptr(ptr.m_ptr), m_count(ptr.m_count) {}
00108 
00110       template <class N_PTR>
00111       SharedPtr(const SharedPtr<N_PTR>& src):m_ptr(src.m_ptr), m_count(src.m_count) {}
00112 
00114       const T* operator->() const { return m_ptr;}
00116       T* operator->() { return m_ptr; }
00117 
00118       typedef T* ThisType::*unspecified_bool_type;
00120       operator unspecified_bool_type() const { return m_ptr == 0 ? 0 : &ThisType::m_ptr; }
00122       bool operator !() const { return m_ptr == 0; }
00123 
00124       typedef void* unspecified_pointer_type;
00126       friend bool operator==(const SharedPtr<T>& a, unspecified_pointer_type b) {return a.m_ptr == b;}
00128       friend bool operator!=(const SharedPtr<T>& a, unspecified_pointer_type b) {return a.m_ptr != b;}
00129 
00131       uint32 GetUseCount() const {return m_count.GetUseCount();}
00132 
00134       bool IsUnique() const {return m_count.GetUseCount() == 1;}
00135 
00137       void Reset() { SharedPtr<T> new_ptr; new_ptr.Swap(*this); }
00139       template<class N_PTR>
00140       void Reset(N_PTR* p) { SharedPtr<N_PTR> new_ptr(p); new_ptr.Swap(*this); }
00142       template <class N_PTR, class DELETER>
00143       void Reset(N_PTR* p, DELETER deleter) { SharedPtr<N_PTR> new_ptr(p, deleter); new_ptr.Swap(*this); }
00144 
00146       void Swap(SharedPtr<T>& a) { std::swap(m_ptr, a.m_ptr); m_count.Swap(a.m_count); }
00147 
00149       template <class N_PTR>
00150       bool Equal(const SharedPtr<N_PTR>& src) const { return m_ptr == src.m_ptr; }
00151 
00153       template <class N_PTR>
00154       bool Less(const SharedPtr<N_PTR>& src) const { return m_count < src.m_count; }
00155 
00156 #ifdef TC_SHARED_PTR_TEST
00157       PointerType GetPtr() {return m_ptr;}
00158       ReferenceType operator*() {return *m_ptr;}
00159 #endif
00160 
00162       template <class N_PTR>
00163       static ThisType DynamicCast(const SharedPtr<N_PTR>& src)
00164       {
00165          ThisType dest;
00166          dest.m_ptr = dynamic_cast<T*>(src.m_ptr);
00167          if (dest.m_ptr)
00168          {
00169             dest.m_count = src.m_count;
00170          }
00171          return dest;
00172       }
00173 
00175       template <class N_PTR>
00176       static ThisType StaticCast(const SharedPtr<N_PTR>& src)
00177       {
00178          ThisType dest;
00179          dest.m_ptr   = static_cast<T*>(src.m_ptr);
00180          dest.m_count = src.m_count;
00181 
00182          return dest;
00183       }
00184 
00186       template <class N_PTR>
00187       static ThisType ConstCast(const SharedPtr<N_PTR>& src)
00188       {
00189          ThisType dest;
00190          dest.m_ptr = const_cast<T*>(src.m_ptr);
00191          dest.m_count = src.m_count;
00192 
00193          return dest;
00194       }
00195 
00196 #ifndef TC_NO_FRIEND_TEMPLATES
00197    private:
00198       template <class N_PTR> friend class SharedPtr;
00199       template <class N_PTR> friend class WeakPtr;
00200 #endif
00201 
00202       T* m_ptr;
00203       SharedPtrCount m_count;
00204 
00205    };
00206 
00208    template <class M_PTR, class N_PTR>
00209    bool operator==(const SharedPtr<M_PTR>& a, const SharedPtr<N_PTR>& b) { return a.Equal(b); }
00210 
00212    template <class M_PTR, class N_PTR>
00213    bool operator!=(const SharedPtr<M_PTR>& a, const SharedPtr<N_PTR>& b) { return !a.Equal(b); }
00214 
00216    template <class M_PTR, class N_PTR>
00217    bool operator<(const SharedPtr<M_PTR>& a, const SharedPtr<N_PTR>& b) { return a.Less(b); }
00218 
00223 } // namespace TC
00224 
00225 #endif // _TC_SHARED_PTR_H_
00226 

Copyright (c) Thomas Goessler 2003 - 2008