]> git.saurik.com Git - apple/javascriptcore.git/blame - wtf/GOwnPtr.h
JavaScriptCore-554.1.tar.gz
[apple/javascriptcore.git] / wtf / GOwnPtr.h
CommitLineData
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
30namespace 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*);
ba379fdc 38 template<> void freeOwnedGPtr<GHashTable>(GHashTable*);
9dae56ea
A
39
40 template <typename T> class GOwnPtr : Noncopyable {
41 public:
42 explicit GOwnPtr(T* ptr = 0) : m_ptr(ptr) { }
43 ~GOwnPtr() { freeOwnedGPtr(m_ptr); }
44
45 T* get() const { return m_ptr; }
46 T* release() { T* ptr = m_ptr; m_ptr = 0; return ptr; }
47 T*& outPtr() { ASSERT(!m_ptr); return m_ptr; }
48
49 void set(T* ptr) { ASSERT(!ptr || m_ptr != ptr); freeOwnedGPtr(m_ptr); m_ptr = ptr; }
50 void clear() { freeOwnedGPtr(m_ptr); m_ptr = 0; }
51
52 T& operator*() const { ASSERT(m_ptr); return *m_ptr; }
53 T* operator->() const { ASSERT(m_ptr); return m_ptr; }
54
55 bool operator!() const { return !m_ptr; }
56
57 // This conversion operator allows implicit conversion to bool but not to other integer types.
58 typedef T* GOwnPtr::*UnspecifiedBoolType;
59 operator UnspecifiedBoolType() const { return m_ptr ? &GOwnPtr::m_ptr : 0; }
60
61 void swap(GOwnPtr& o) { std::swap(m_ptr, o.m_ptr); }
62
63 private:
64 T* m_ptr;
65 };
66
67 template <typename T> inline void swap(GOwnPtr<T>& a, GOwnPtr<T>& b) { a.swap(b); }
68
69 template <typename T, typename U> inline bool operator==(const GOwnPtr<T>& a, U* b)
70 {
71 return a.get() == b;
72 }
73
74 template <typename T, typename U> inline bool operator==(T* a, const GOwnPtr<U>& b)
75 {
76 return a == b.get();
77 }
78
79 template <typename T, typename U> inline bool operator!=(const GOwnPtr<T>& a, U* b)
80 {
81 return a.get() != b;
82 }
83
84 template <typename T, typename U> inline bool operator!=(T* a, const GOwnPtr<U>& b)
85 {
86 return a != b.get();
87 }
88
89 template <typename T> inline typename GOwnPtr<T>::PtrType getPtr(const GOwnPtr<T>& p)
90 {
91 return p.get();
92 }
93
94} // namespace WTF
95
96using WTF::GOwnPtr;
97
98#endif // GOwnPtr_h