]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
1 | // -*- mode: c++; c-basic-offset: 4 -*- |
2 | /* | |
3 | * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. | |
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 WTF_OwnPtr_h | |
23 | #define WTF_OwnPtr_h | |
24 | ||
25 | #include <algorithm> | |
26 | #include <wtf/Assertions.h> | |
27 | #include <wtf/Noncopyable.h> | |
28 | ||
29 | #if PLATFORM(WIN) | |
30 | ||
31 | typedef struct HBITMAP__* HBITMAP; | |
32 | typedef struct HBRUSH__* HBRUSH; | |
33 | typedef struct HFONT__* HFONT; | |
34 | typedef struct HPALETTE__* HPALETTE; | |
35 | typedef struct HPEN__* HPEN; | |
36 | typedef struct HRGN__* HRGN; | |
37 | ||
38 | #endif | |
39 | ||
40 | namespace WTF { | |
41 | ||
42 | // Unlike most of our smart pointers, OwnPtr can take either the pointer type or the pointed-to type. | |
43 | ||
44 | // FIXME: Share a single RemovePointer class template with RetainPtr. | |
45 | template <typename T> struct OwnPtrRemovePointer { typedef T type; }; | |
46 | template <typename T> struct OwnPtrRemovePointer<T*> { typedef T type; }; | |
47 | ||
48 | template <typename T> inline void deleteOwnedPtr(T* ptr) | |
49 | { | |
50 | typedef char known[sizeof(T) ? 1 : -1]; | |
51 | if (sizeof(known)) | |
52 | delete ptr; | |
53 | } | |
54 | ||
55 | #if PLATFORM(WIN) | |
56 | void deleteOwnedPtr(HBITMAP); | |
57 | void deleteOwnedPtr(HBRUSH); | |
58 | void deleteOwnedPtr(HFONT); | |
59 | void deleteOwnedPtr(HPALETTE); | |
60 | void deleteOwnedPtr(HPEN); | |
61 | void deleteOwnedPtr(HRGN); | |
62 | #endif | |
63 | ||
64 | template <typename T> class OwnPtr : Noncopyable { | |
65 | public: | |
66 | typedef typename OwnPtrRemovePointer<T>::type ValueType; | |
67 | typedef ValueType* PtrType; | |
68 | ||
69 | explicit OwnPtr(PtrType ptr = 0) : m_ptr(ptr) { } | |
70 | ~OwnPtr() { deleteOwnedPtr(m_ptr); } | |
71 | ||
72 | PtrType get() const { return m_ptr; } | |
73 | PtrType release() { PtrType ptr = m_ptr; m_ptr = 0; return ptr; } | |
74 | ||
75 | void set(PtrType ptr) { ASSERT(!ptr || m_ptr != ptr); deleteOwnedPtr(m_ptr); m_ptr = ptr; } | |
76 | void clear() { deleteOwnedPtr(m_ptr); m_ptr = 0; } | |
77 | ||
78 | ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } | |
79 | PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } | |
80 | ||
81 | bool operator!() const { return !m_ptr; } | |
82 | ||
83 | // This conversion operator allows implicit conversion to bool but not to other integer types. | |
84 | typedef PtrType OwnPtr::*UnspecifiedBoolType; | |
85 | operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : 0; } | |
86 | ||
87 | void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); } | |
88 | ||
89 | private: | |
90 | PtrType m_ptr; | |
91 | }; | |
92 | ||
93 | template <typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b) { a.swap(b); } | |
94 | ||
95 | template <typename T, typename U> inline bool operator==(const OwnPtr<T>& a, U* b) | |
96 | { | |
97 | return a.get() == b; | |
98 | } | |
99 | ||
100 | template <typename T, typename U> inline bool operator==(T* a, const OwnPtr<U>& b) | |
101 | { | |
102 | return a == b.get(); | |
103 | } | |
104 | ||
105 | template <typename T, typename U> inline bool operator!=(const OwnPtr<T>& a, U* b) | |
106 | { | |
107 | return a.get() != b; | |
108 | } | |
109 | ||
110 | template <typename T, typename U> inline bool operator!=(T* a, const OwnPtr<U>& b) | |
111 | { | |
112 | return a != b.get(); | |
113 | } | |
114 | ||
115 | template <typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<T>& p) | |
116 | { | |
117 | return p.get(); | |
118 | } | |
119 | ||
120 | } // namespace WTF | |
121 | ||
122 | using WTF::OwnPtr; | |
123 | ||
124 | #endif // WTF_OwnPtr_h |