]> git.saurik.com Git - apple/javascriptcore.git/blame - wtf/OwnPtr.h
JavaScriptCore-721.26.tar.gz
[apple/javascriptcore.git] / wtf / OwnPtr.h
CommitLineData
b37bf2e1 1/*
ba379fdc 2 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
b37bf2e1
A
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
ba379fdc
A
24#include "Assertions.h"
25#include "Noncopyable.h"
26#include "OwnPtrCommon.h"
27#include "TypeTraits.h"
b37bf2e1 28#include <algorithm>
9dae56ea 29#include <memory>
b37bf2e1
A
30
31namespace WTF {
32
33 // Unlike most of our smart pointers, OwnPtr can take either the pointer type or the pointed-to type.
34
ba379fdc 35 template <typename T> class PassOwnPtr;
b37bf2e1 36
f9bf01c6 37 template <typename T> class OwnPtr : public Noncopyable {
b37bf2e1 38 public:
ba379fdc 39 typedef typename RemovePointer<T>::Type ValueType;
b37bf2e1
A
40 typedef ValueType* PtrType;
41
42 explicit OwnPtr(PtrType ptr = 0) : m_ptr(ptr) { }
ba379fdc
A
43 // See comment in PassOwnPtr.h for why this takes a const reference.
44 template <typename U> OwnPtr(const PassOwnPtr<U>& o);
45
46 // This copy constructor is used implicitly by gcc when it generates
47 // transients for assigning a PassOwnPtr<T> object to a stack-allocated
48 // OwnPtr<T> object. It should never be called explicitly and gcc
49 // should optimize away the constructor when generating code.
50 OwnPtr(const OwnPtr<ValueType>& o);
51
b37bf2e1
A
52 ~OwnPtr() { deleteOwnedPtr(m_ptr); }
53
54 PtrType get() const { return m_ptr; }
55 PtrType release() { PtrType ptr = m_ptr; m_ptr = 0; return ptr; }
56
9dae56ea 57 // FIXME: This should be renamed to adopt.
b37bf2e1 58 void set(PtrType ptr) { ASSERT(!ptr || m_ptr != ptr); deleteOwnedPtr(m_ptr); m_ptr = ptr; }
9dae56ea 59
b37bf2e1
A
60 void clear() { deleteOwnedPtr(m_ptr); m_ptr = 0; }
61
62 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; }
63 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; }
64
65 bool operator!() const { return !m_ptr; }
66
67 // This conversion operator allows implicit conversion to bool but not to other integer types.
68 typedef PtrType OwnPtr::*UnspecifiedBoolType;
69 operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : 0; }
70
ba379fdc
A
71 OwnPtr& operator=(const PassOwnPtr<T>&);
72 template <typename U> OwnPtr& operator=(const PassOwnPtr<U>&);
73
b37bf2e1
A
74 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); }
75
76 private:
77 PtrType m_ptr;
78 };
ba379fdc
A
79
80 template <typename T> template <typename U> inline OwnPtr<T>::OwnPtr(const PassOwnPtr<U>& o)
81 : m_ptr(o.release())
82 {
83 }
84
85 template <typename T> inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<T>& o)
86 {
87 T* ptr = m_ptr;
88 m_ptr = o.release();
89 ASSERT(!ptr || m_ptr != ptr);
90 if (ptr)
91 deleteOwnedPtr(ptr);
92 return *this;
93 }
94
95 template <typename T> template <typename U> inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<U>& o)
96 {
97 T* ptr = m_ptr;
98 m_ptr = o.release();
99 ASSERT(!ptr || m_ptr != ptr);
100 if (ptr)
101 deleteOwnedPtr(ptr);
102 return *this;
103 }
104
105 template <typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b)
106 {
107 a.swap(b);
108 }
b37bf2e1
A
109
110 template <typename T, typename U> inline bool operator==(const OwnPtr<T>& a, U* b)
ba379fdc 111 {
b37bf2e1
A
112 return a.get() == b;
113 }
ba379fdc 114
b37bf2e1
A
115 template <typename T, typename U> inline bool operator==(T* a, const OwnPtr<U>& b)
116 {
117 return a == b.get();
118 }
119
120 template <typename T, typename U> inline bool operator!=(const OwnPtr<T>& a, U* b)
121 {
122 return a.get() != b;
123 }
124
125 template <typename T, typename U> inline bool operator!=(T* a, const OwnPtr<U>& b)
ba379fdc 126 {
b37bf2e1
A
127 return a != b.get();
128 }
ba379fdc 129
b37bf2e1
A
130 template <typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<T>& p)
131 {
132 return p.get();
133 }
134
135} // namespace WTF
136
137using WTF::OwnPtr;
138
139#endif // WTF_OwnPtr_h