]>
Commit | Line | Data |
---|---|---|
9dae56ea A |
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 | #include <algorithm> | |
26 | #include <glib.h> | |
27 | #include <wtf/Assertions.h> | |
28 | #include <wtf/Noncopyable.h> | |
29 | ||
30 | namespace WTF { | |
31 | template <typename T> inline void freeOwnedGPtr(T* ptr) { g_free(reinterpret_cast<void*>(ptr)); } | |
32 | template<> void freeOwnedGPtr<GError>(GError*); | |
33 | template<> void freeOwnedGPtr<GList>(GList*); | |
34 | template<> void freeOwnedGPtr<GCond>(GCond*); | |
35 | template<> void freeOwnedGPtr<GMutex>(GMutex*); | |
36 | template<> void freeOwnedGPtr<GPatternSpec>(GPatternSpec*); | |
37 | template<> void freeOwnedGPtr<GDir>(GDir*); | |
38 | ||
39 | template <typename T> class GOwnPtr : Noncopyable { | |
40 | public: | |
41 | explicit GOwnPtr(T* ptr = 0) : m_ptr(ptr) { } | |
42 | ~GOwnPtr() { freeOwnedGPtr(m_ptr); } | |
43 | ||
44 | T* get() const { return m_ptr; } | |
45 | T* release() { T* ptr = m_ptr; m_ptr = 0; return ptr; } | |
46 | T*& outPtr() { ASSERT(!m_ptr); return m_ptr; } | |
47 | ||
48 | void set(T* ptr) { ASSERT(!ptr || m_ptr != ptr); freeOwnedGPtr(m_ptr); m_ptr = ptr; } | |
49 | void clear() { freeOwnedGPtr(m_ptr); m_ptr = 0; } | |
50 | ||
51 | T& operator*() const { ASSERT(m_ptr); return *m_ptr; } | |
52 | T* operator->() const { ASSERT(m_ptr); return m_ptr; } | |
53 | ||
54 | bool operator!() const { return !m_ptr; } | |
55 | ||
56 | // This conversion operator allows implicit conversion to bool but not to other integer types. | |
57 | typedef T* GOwnPtr::*UnspecifiedBoolType; | |
58 | operator UnspecifiedBoolType() const { return m_ptr ? &GOwnPtr::m_ptr : 0; } | |
59 | ||
60 | void swap(GOwnPtr& o) { std::swap(m_ptr, o.m_ptr); } | |
61 | ||
62 | private: | |
63 | T* m_ptr; | |
64 | }; | |
65 | ||
66 | template <typename T> inline void swap(GOwnPtr<T>& a, GOwnPtr<T>& b) { a.swap(b); } | |
67 | ||
68 | template <typename T, typename U> inline bool operator==(const GOwnPtr<T>& a, U* b) | |
69 | { | |
70 | return a.get() == b; | |
71 | } | |
72 | ||
73 | template <typename T, typename U> inline bool operator==(T* a, const GOwnPtr<U>& b) | |
74 | { | |
75 | return a == b.get(); | |
76 | } | |
77 | ||
78 | template <typename T, typename U> inline bool operator!=(const GOwnPtr<T>& a, U* b) | |
79 | { | |
80 | return a.get() != b; | |
81 | } | |
82 | ||
83 | template <typename T, typename U> inline bool operator!=(T* a, const GOwnPtr<U>& b) | |
84 | { | |
85 | return a != b.get(); | |
86 | } | |
87 | ||
88 | template <typename T> inline typename GOwnPtr<T>::PtrType getPtr(const GOwnPtr<T>& p) | |
89 | { | |
90 | return p.get(); | |
91 | } | |
92 | ||
93 | } // namespace WTF | |
94 | ||
95 | using WTF::GOwnPtr; | |
96 | ||
97 | #endif // GOwnPtr_h |