• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

WTF

PassRefPtr.h

Go to the documentation of this file.
00001 // -*- mode: c++; c-basic-offset: 4 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 2005, 2006 Apple Computer, Inc.
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Library General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Library General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Library General Public License
00017  *  along with this library; see the file COPYING.LIB.  If not, write to
00018  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  *  Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef WTF_PassRefPtr_h
00024 #define WTF_PassRefPtr_h
00025 
00026 namespace WTF {
00027 
00028     template<typename T> class RefPtr;
00029     template<typename T> class PassRefPtr;
00030     template <typename T> PassRefPtr<T> adoptRef(T *p);
00031 
00032     template<typename T> 
00033     class PassRefPtr
00034     {
00035     public:
00036         PassRefPtr() : m_ptr(0) {}
00037         PassRefPtr(T *ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }
00038         // It somewhat breaks the type system to allow transfer of ownership out of
00039         // a const PassRefPtr. However, it makes it much easier to work with PassRefPtr
00040         // temporaries, and we don't really have a need to use real const PassRefPtrs 
00041         // anyway.
00042         PassRefPtr(const PassRefPtr& o) : m_ptr(o.release()) {}
00043         template <typename U> PassRefPtr(const PassRefPtr<U>& o) : m_ptr(o.release()) { }
00044 
00045         ~PassRefPtr() { if (T *ptr = m_ptr) ptr->deref(); }
00046         
00047         template <class U> 
00048         PassRefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { if (T *ptr = m_ptr) ptr->ref(); }
00049         
00050         T *get() const { return m_ptr; }
00051 
00052         T *release() const { T *tmp = m_ptr; m_ptr = 0; return tmp; }
00053 
00054         T& operator*() const { return *m_ptr; }
00055         T *operator->() const { return m_ptr; }
00056         
00057         bool operator!() const { return !m_ptr; }
00058 
00059         // This conversion operator allows implicit conversion to bool but not to other integer types.
00060         typedef T* (PassRefPtr::*UnspecifiedBoolType)() const;
00061         operator UnspecifiedBoolType() const { return m_ptr ? &PassRefPtr::get : 0; }
00062         
00063         PassRefPtr& operator=(T *);
00064         PassRefPtr& operator=(const PassRefPtr&);
00065         template <typename U> PassRefPtr& operator=(const PassRefPtr<U>&);
00066         template <typename U> PassRefPtr& operator=(const RefPtr<U>&);
00067 
00068         friend PassRefPtr adoptRef<T>(T *);
00069     private:
00070         // adopting constructor
00071         PassRefPtr(T *ptr, bool) : m_ptr(ptr) {}
00072         mutable T *m_ptr;
00073     };
00074     
00075     template <typename T> template <typename U> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const RefPtr<U>& o) 
00076     {
00077         T* optr = o.get();
00078         if (optr)
00079             optr->ref();
00080         T* ptr = m_ptr;
00081         m_ptr = optr;
00082         if (ptr)
00083             ptr->deref();
00084         return *this;
00085     }
00086     
00087     template <typename T> inline PassRefPtr<T>& PassRefPtr<T>::operator=(T* optr)
00088     {
00089         if (optr)
00090             optr->ref();
00091         T* ptr = m_ptr;
00092         m_ptr = optr;
00093         if (ptr)
00094             ptr->deref();
00095         return *this;
00096     }
00097 
00098     template <typename T> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const PassRefPtr<T>& ref)
00099     {
00100         T* ptr = m_ptr;
00101         m_ptr = ref.release();
00102         if (ptr)
00103             ptr->deref();
00104         return *this;
00105     }
00106     
00107     template <typename T> template <typename U> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const PassRefPtr<U>& ref)
00108     {
00109         T* ptr = m_ptr;
00110         m_ptr = ref.release();
00111         if (ptr)
00112             ptr->deref();
00113         return *this;
00114     }
00115     
00116     template <typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const PassRefPtr<U>& b) 
00117     { 
00118         return a.get() == b.get(); 
00119     }
00120 
00121     template <typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const RefPtr<U>& b) 
00122     { 
00123         return a.get() == b.get(); 
00124     }
00125 
00126     template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, const PassRefPtr<U>& b) 
00127     { 
00128         return a.get() == b.get(); 
00129     }
00130 
00131     template <typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, U* b) 
00132     { 
00133         return a.get() == b; 
00134     }
00135     
00136     template <typename T, typename U> inline bool operator==(T* a, const PassRefPtr<U>& b) 
00137     {
00138         return a == b.get(); 
00139     }
00140     
00141     template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const PassRefPtr<U>& b) 
00142     { 
00143         return a.get() != b.get(); 
00144     }
00145 
00146     template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const RefPtr<U>& b) 
00147     { 
00148         return a.get() != b.get(); 
00149     }
00150 
00151     template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const PassRefPtr<U>& b) 
00152     { 
00153         return a.get() != b.get(); 
00154     }
00155 
00156     template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, U* b)
00157     {
00158         return a.get() != b; 
00159     }
00160 
00161     template <typename T, typename U> inline bool operator!=(T* a, const PassRefPtr<U>& b) 
00162     { 
00163         return a != b.get(); 
00164     }
00165     
00166     template <typename T> inline PassRefPtr<T> adoptRef(T *p)
00167     {
00168         return PassRefPtr<T>(p, true);
00169     }
00170 
00171     template <typename T, typename U> inline PassRefPtr<T> static_pointer_cast(const PassRefPtr<U>& p) 
00172     { 
00173         return adoptRef(static_cast<T *>(p.release())); 
00174     }
00175 
00176     template <typename T, typename U> inline PassRefPtr<T> const_pointer_cast(const PassRefPtr<U>& p) 
00177     { 
00178         return adoptRef(const_cast<T *>(p.release())); 
00179     }
00180 
00181 } // namespace WTF
00182 
00183 using WTF::PassRefPtr;
00184 using WTF::adoptRef;
00185 using WTF::static_pointer_cast;
00186 using WTF::const_pointer_cast;
00187 
00188 #endif // WTF_PassRefPtr_h

WTF

Skip menu "WTF"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   WTF
  • KJSEmbed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  •   core
  • Phonon
  •   Backend
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal