1 | /*
|
---|
2 | * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
|
---|
3 | *
|
---|
4 | * This library is free software; you can redistribute it and/or
|
---|
5 | * modify it under the terms of the GNU Library General Public
|
---|
6 | * License as published by the Free Software Foundation; either
|
---|
7 | * version 2 of the License, or (at your option) any later version.
|
---|
8 | *
|
---|
9 | * This library is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
12 | * Library General Public License for more details.
|
---|
13 | *
|
---|
14 | * You should have received a copy of the GNU Library General Public License
|
---|
15 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
17 | * Boston, MA 02110-1301, USA.
|
---|
18 | *
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifndef WTF_OwnPtr_h
|
---|
22 | #define WTF_OwnPtr_h
|
---|
23 |
|
---|
24 | #include <algorithm>
|
---|
25 | #include <wtf/Assertions.h>
|
---|
26 | #include <wtf/Noncopyable.h>
|
---|
27 |
|
---|
28 | #if PLATFORM(WIN)
|
---|
29 |
|
---|
30 | typedef struct HBITMAP__* HBITMAP;
|
---|
31 | typedef struct HBRUSH__* HBRUSH;
|
---|
32 | typedef struct HFONT__* HFONT;
|
---|
33 | typedef struct HPALETTE__* HPALETTE;
|
---|
34 | typedef struct HPEN__* HPEN;
|
---|
35 | typedef struct HRGN__* HRGN;
|
---|
36 |
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | namespace WTF {
|
---|
40 |
|
---|
41 | // Unlike most of our smart pointers, OwnPtr can take either the pointer type or the pointed-to type.
|
---|
42 |
|
---|
43 | // FIXME: Share a single RemovePointer class template with RetainPtr.
|
---|
44 | template <typename T> struct OwnPtrRemovePointer { typedef T type; };
|
---|
45 | template <typename T> struct OwnPtrRemovePointer<T*> { typedef T type; };
|
---|
46 |
|
---|
47 | template <typename T> inline void deleteOwnedPtr(T* ptr)
|
---|
48 | {
|
---|
49 | typedef char known[sizeof(T) ? 1 : -1];
|
---|
50 | if (sizeof(known))
|
---|
51 | delete ptr;
|
---|
52 | }
|
---|
53 |
|
---|
54 | #if PLATFORM(WIN)
|
---|
55 | void deleteOwnedPtr(HBITMAP);
|
---|
56 | void deleteOwnedPtr(HBRUSH);
|
---|
57 | void deleteOwnedPtr(HFONT);
|
---|
58 | void deleteOwnedPtr(HPALETTE);
|
---|
59 | void deleteOwnedPtr(HPEN);
|
---|
60 | void deleteOwnedPtr(HRGN);
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | template <typename T> class OwnPtr : Noncopyable {
|
---|
64 | public:
|
---|
65 | typedef typename OwnPtrRemovePointer<T>::type ValueType;
|
---|
66 | typedef ValueType* PtrType;
|
---|
67 |
|
---|
68 | explicit OwnPtr(PtrType ptr = 0) : m_ptr(ptr) { }
|
---|
69 | ~OwnPtr() { deleteOwnedPtr(m_ptr); }
|
---|
70 |
|
---|
71 | PtrType get() const { return m_ptr; }
|
---|
72 | PtrType release() { PtrType ptr = m_ptr; m_ptr = 0; return ptr; }
|
---|
73 |
|
---|
74 | void set(PtrType ptr) { ASSERT(!ptr || m_ptr != ptr); deleteOwnedPtr(m_ptr); m_ptr = ptr; }
|
---|
75 | void clear() { deleteOwnedPtr(m_ptr); m_ptr = 0; }
|
---|
76 |
|
---|
77 | ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; }
|
---|
78 | PtrType operator->() const { ASSERT(m_ptr); return m_ptr; }
|
---|
79 |
|
---|
80 | bool operator!() const { return !m_ptr; }
|
---|
81 |
|
---|
82 | // This conversion operator allows implicit conversion to bool but not to other integer types.
|
---|
83 | typedef PtrType OwnPtr::*UnspecifiedBoolType;
|
---|
84 | operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : 0; }
|
---|
85 |
|
---|
86 | void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); }
|
---|
87 |
|
---|
88 | private:
|
---|
89 | PtrType m_ptr;
|
---|
90 | };
|
---|
91 |
|
---|
92 | template <typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b) { a.swap(b); }
|
---|
93 |
|
---|
94 | template <typename T, typename U> inline bool operator==(const OwnPtr<T>& a, U* b)
|
---|
95 | {
|
---|
96 | return a.get() == b;
|
---|
97 | }
|
---|
98 |
|
---|
99 | template <typename T, typename U> inline bool operator==(T* a, const OwnPtr<U>& b)
|
---|
100 | {
|
---|
101 | return a == b.get();
|
---|
102 | }
|
---|
103 |
|
---|
104 | template <typename T, typename U> inline bool operator!=(const OwnPtr<T>& a, U* b)
|
---|
105 | {
|
---|
106 | return a.get() != b;
|
---|
107 | }
|
---|
108 |
|
---|
109 | template <typename T, typename U> inline bool operator!=(T* a, const OwnPtr<U>& b)
|
---|
110 | {
|
---|
111 | return a != b.get();
|
---|
112 | }
|
---|
113 |
|
---|
114 | template <typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<T>& p)
|
---|
115 | {
|
---|
116 | return p.get();
|
---|
117 | }
|
---|
118 |
|
---|
119 | } // namespace WTF
|
---|
120 |
|
---|
121 | using WTF::OwnPtr;
|
---|
122 |
|
---|
123 | #endif // WTF_OwnPtr_h
|
---|