Home | History | Annotate | Download | only in gobject
      1 /*
      2  *  Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
      3  *  Copyright (C) 2008 Collabora Ltd.
      4  *
      5  *  This library is free software; you can redistribute it and/or
      6  *  modify it under the terms of the GNU Library General Public
      7  *  License as published by the Free Software Foundation; either
      8  *  version 2 of the License, or (at your option) any later version.
      9  *
     10  *  This library is distributed in the hope that it will be useful,
     11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13  *  Library General Public License for more details.
     14  *
     15  *  You should have received a copy of the GNU Library General Public License
     16  *  along with this library; see the file COPYING.LIB.  If not, write to
     17  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18  *  Boston, MA 02110-1301, USA.
     19  *
     20  */
     21 
     22 #ifndef GOwnPtr_h
     23 #define GOwnPtr_h
     24 
     25 #if ENABLE(GLIB_SUPPORT)
     26 
     27 #include <algorithm>
     28 #include <wtf/Assertions.h>
     29 #include <wtf/Noncopyable.h>
     30 
     31 extern "C" void g_free(void*);
     32 
     33 namespace WTF {
     34 
     35 template <typename T> inline void freeOwnedGPtr(T* ptr);
     36 template<> void freeOwnedGPtr<GError>(GError*);
     37 template<> void freeOwnedGPtr<GList>(GList*);
     38 template<> void freeOwnedGPtr<GCond>(GCond*);
     39 template<> void freeOwnedGPtr<GMutex>(GMutex*);
     40 template<> void freeOwnedGPtr<GPatternSpec>(GPatternSpec*);
     41 template<> void freeOwnedGPtr<GDir>(GDir*);
     42 
     43 template <typename T> class GOwnPtr {
     44     WTF_MAKE_NONCOPYABLE(GOwnPtr);
     45 public:
     46     explicit GOwnPtr(T* ptr = 0) : m_ptr(ptr) { }
     47     ~GOwnPtr() { freeOwnedGPtr(m_ptr); }
     48 
     49     T* get() const { return m_ptr; }
     50     T* release()
     51     {
     52         T* ptr = m_ptr;
     53         m_ptr = 0;
     54         return ptr;
     55     }
     56 
     57     T*& outPtr()
     58     {
     59         ASSERT(!m_ptr);
     60         return m_ptr;
     61     }
     62 
     63     void set(T* ptr)
     64     {
     65         ASSERT(!ptr || m_ptr != ptr);
     66         freeOwnedGPtr(m_ptr);
     67         m_ptr = ptr;
     68     }
     69 
     70     void clear()
     71     {
     72         T* ptr = m_ptr;
     73         m_ptr = 0;
     74         freeOwnedGPtr(ptr);
     75     }
     76 
     77     T& operator*() const
     78     {
     79         ASSERT(m_ptr);
     80         return *m_ptr;
     81     }
     82 
     83     T* operator->() const
     84     {
     85         ASSERT(m_ptr);
     86         return m_ptr;
     87     }
     88 
     89     bool operator!() const { return !m_ptr; }
     90 
     91     // This conversion operator allows implicit conversion to bool but not to other integer types.
     92     typedef T* GOwnPtr::*UnspecifiedBoolType;
     93     operator UnspecifiedBoolType() const { return m_ptr ? &GOwnPtr::m_ptr : 0; }
     94 
     95     void swap(GOwnPtr& o) { std::swap(m_ptr, o.m_ptr); }
     96 
     97 private:
     98     T* m_ptr;
     99 };
    100 
    101 template <typename T> inline void swap(GOwnPtr<T>& a, GOwnPtr<T>& b)
    102 {
    103     a.swap(b);
    104 }
    105 
    106 template <typename T, typename U> inline bool operator==(const GOwnPtr<T>& a, U* b)
    107 {
    108     return a.get() == b;
    109 }
    110 
    111 template <typename T, typename U> inline bool operator==(T* a, const GOwnPtr<U>& b)
    112 {
    113     return a == b.get();
    114 }
    115 
    116 template <typename T, typename U> inline bool operator!=(const GOwnPtr<T>& a, U* b)
    117 {
    118     return a.get() != b;
    119 }
    120 
    121 template <typename T, typename U> inline bool operator!=(T* a, const GOwnPtr<U>& b)
    122 {
    123     return a != b.get();
    124 }
    125 
    126 template <typename T> inline typename GOwnPtr<T>::PtrType getPtr(const GOwnPtr<T>& p)
    127 {
    128     return p.get();
    129 }
    130 
    131 template <typename T> inline void freeOwnedGPtr(T* ptr)
    132 {
    133     g_free(ptr);
    134 }
    135 
    136 } // namespace WTF
    137 
    138 using WTF::GOwnPtr;
    139 
    140 #endif // ENABLE(GLIB_SUPPORT)
    141 
    142 #endif // GOwnPtr_h
    143 
    144