TCWeakPtr.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: TCWeakPtr.h 863 2008-07-14 20:54:39Z the_____tiger $
00034 //*******************************************************************************
00035 #ifndef _TC_WEAK_PTR_H
00036 #define _TC_WEAK_PTR_H
00037 
00038 #include "TCSharedPtr.h"
00039 
00040 namespace TC
00041 {
00067    template<class T>
00068    class WeakPtr
00069    {
00070    public:
00072       WeakPtr():m_ptr(0), m_count() {}
00073 
00075       template<class M_PTR>
00076       WeakPtr(const WeakPtr<M_PTR>& r):m_ptr(r.Lock().m_ptr), m_count(r.m_count) {}
00077 
00079       template<class M_PTR>
00080       WeakPtr(const SharedPtr<M_PTR>&  r):m_ptr(r.m_ptr), m_count(r.m_count) {}
00081 
00083       SharedPtr<T> Lock() const { return IsExpired() ? SharedPtr<T>(): SharedPtr<T>(*this); }
00084 
00086       long GetUseCount() const { return m_count.GetUseCount(); }
00087 
00089       bool IsExpired() const { return m_count.GetUseCount() == 0;}
00090 
00092       void Reset() { WeakPtr<T> new_ptr; new_ptr.Swap(*this); }
00093 
00095       void Swap(WeakPtr<T>& other) { std::swap(m_ptr, other.m_ptr); m_count.Swap(other.m_count); }
00096 
00098       template <class N_PTR>
00099       bool Equal(const WeakPtr<N_PTR>& src) const { return m_ptr == src.m_ptr; }
00100 
00102       template<class M_PTR>
00103       bool Less(const WeakPtr<M_PTR>& src) const { return m_count < src.m_count; }
00104 
00105    private:
00106       T* m_ptr;                // contained pointer
00107       WeakPtrCount m_count;  // reference counter
00108 
00109       template<class M_PTR> friend class WeakPtr;
00110       template<class M_PTR> friend class SharedPtr;
00111    };
00112 
00114    template <class M_PTR, class N_PTR>
00115    bool operator==(const WeakPtr<M_PTR>& a, const WeakPtr<N_PTR>& b) { return a.Equal(b); }
00116 
00118    template <class M_PTR, class N_PTR>
00119    bool operator!=(const WeakPtr<M_PTR>& a, const WeakPtr<N_PTR>& b) { return !a.Equal(b); }
00120 
00122    template <class M_PTR, class N_PTR>
00123    bool operator<(const WeakPtr<M_PTR>& a, const WeakPtr<N_PTR>& b) { return a.Less(b); }
00124 
00129 }
00130 
00131 #endif  // _TC_WEAK_PTR_H

Copyright (c) Thomas Goessler 2003 - 2008