Changeset 25413 in webkit for trunk/JavaScriptCore/API/JSRetainPtr.h
- Timestamp:
- Sep 7, 2007, 9:52:45 AM (18 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/API/JSRetainPtr.h
r25394 r25413 33 33 #include <algorithm> 34 34 35 namespace KJS { 35 inline void JSRetain(JSStringRef string) { JSStringRetain(string); } 36 inline void JSRelease(JSStringRef string) { JSStringRelease(string); } 36 37 37 inline void JSRetain(JSStringRef string) { JSStringRetain(string); } 38 inline void JSRelease(JSStringRef string) { JSStringRelease(string); } 38 enum AdoptTag { Adopt }; 39 39 40 enum AdoptTag { Adopt }; 40 template <typename T> class JSRetainPtr { 41 public: 42 JSRetainPtr() : m_ptr(0) {} 43 JSRetainPtr(T ptr) : m_ptr(ptr) { if (ptr) JSRetain(ptr); } 41 44 42 template <typename T> class JSRetainPtr { 43 public: 44 JSRetainPtr() : m_ptr(0) {} 45 JSRetainPtr(T ptr) : m_ptr(ptr) { if (ptr) JSRetain(ptr); } 45 JSRetainPtr(AdoptTag, T ptr) : m_ptr(ptr) { } 46 47 JSRetainPtr(const JSRetainPtr& o) : m_ptr(o.m_ptr) { if (T ptr = m_ptr) JSRetain(ptr); } 46 48 47 JSRetainPtr(AdoptTag, T ptr) : m_ptr(ptr) { } 48 49 JSRetainPtr(const JSRetainPtr& o) : m_ptr(o.m_ptr) { if (T ptr = m_ptr) JSRetain(ptr); } 49 ~JSRetainPtr() { if (T ptr = m_ptr) JSRelease(ptr); } 50 51 template <typename U> JSRetainPtr(const JSRetainPtr<U>& o) : m_ptr(o.get()) { if (T ptr = m_ptr) JSRetain(ptr); } 52 53 T get() const { return m_ptr; } 54 55 T releaseRef() { T tmp = m_ptr; m_ptr = 0; return tmp; } 56 57 T operator->() const { return m_ptr; } 58 59 bool operator!() const { return !m_ptr; } 50 60 51 ~JSRetainPtr() { if (T ptr = m_ptr) JSRelease(ptr); } 52 53 template <typename U> JSRetainPtr(const JSRetainPtr<U>& o) : m_ptr(o.get()) { if (T ptr = m_ptr) JSRetain(ptr); } 54 55 T get() const { return m_ptr; } 56 57 T releaseRef() { T tmp = m_ptr; m_ptr = 0; return tmp; } 58 59 T operator->() const { return m_ptr; } 60 61 bool operator!() const { return !m_ptr; } 61 // This conversion operator allows implicit conversion to bool but not to other integer types. 62 typedef T (JSRetainPtr::*UnspecifiedBoolType)() const; 63 operator UnspecifiedBoolType() const { return m_ptr ? &JSRetainPtr::get : 0; } 62 64 63 // This conversion operator allows implicit conversion to bool but not to other integer types. 64 typedef T (JSRetainPtr::*UnspecifiedBoolType)() const; 65 operator UnspecifiedBoolType() const { return m_ptr ? &JSRetainPtr::get : 0; } 66 67 JSRetainPtr& operator=(const JSRetainPtr&); 68 template <typename U> JSRetainPtr& operator=(const JSRetainPtr<U>&); 69 JSRetainPtr& operator=(T); 70 template <typename U> JSRetainPtr& operator=(U*); 65 JSRetainPtr& operator=(const JSRetainPtr&); 66 template <typename U> JSRetainPtr& operator=(const JSRetainPtr<U>&); 67 JSRetainPtr& operator=(T); 68 template <typename U> JSRetainPtr& operator=(U*); 71 69 72 73 74 70 void adopt(T); 71 72 void swap(JSRetainPtr&); 75 73 76 private: 77 T m_ptr; 78 }; 79 80 template <typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(const JSRetainPtr<T>& o) 81 { 82 T optr = o.get(); 83 if (optr) 84 JSRetain(optr); 85 T ptr = m_ptr; 86 m_ptr = optr; 87 if (ptr) 88 JSRelease(ptr); 89 return *this; 90 } 91 92 template <typename T> template <typename U> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(const JSRetainPtr<U>& o) 93 { 94 T optr = o.get(); 95 if (optr) 96 JSRetain(optr); 97 T ptr = m_ptr; 98 m_ptr = optr; 99 if (ptr) 100 JSRelease(ptr); 101 return *this; 102 } 103 104 template <typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(T optr) 105 { 106 if (optr) 107 JSRetain(optr); 108 T ptr = m_ptr; 109 m_ptr = optr; 110 if (ptr) 111 JSRelease(ptr); 112 return *this; 113 } 74 private: 75 T m_ptr; 76 }; 114 77 115 template <typename T> inline void JSRetainPtr<T>::adopt(T optr) 116 { 117 T ptr = m_ptr; 118 m_ptr = optr; 119 if (ptr) 120 JSRelease(ptr); 121 } 122 123 template <typename T> template <typename U> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(U* optr) 124 { 125 if (optr) 126 JSRetain(optr); 127 T ptr = m_ptr; 128 m_ptr = optr; 129 if (ptr) 130 JSRelease(ptr); 131 return *this; 132 } 78 template <typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(const JSRetainPtr<T>& o) 79 { 80 T optr = o.get(); 81 if (optr) 82 JSRetain(optr); 83 T ptr = m_ptr; 84 m_ptr = optr; 85 if (ptr) 86 JSRelease(ptr); 87 return *this; 88 } 133 89 134 template <class T> inline void JSRetainPtr<T>::swap(JSRetainPtr<T>& o) 135 { 136 std::swap(m_ptr, o.m_ptr); 137 } 90 template <typename T> template <typename U> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(const JSRetainPtr<U>& o) 91 { 92 T optr = o.get(); 93 if (optr) 94 JSRetain(optr); 95 T ptr = m_ptr; 96 m_ptr = optr; 97 if (ptr) 98 JSRelease(ptr); 99 return *this; 100 } 138 101 139 template <class T> inline void swap(JSRetainPtr<T>& a, JSRetainPtr<T>& b) 140 { 141 a.swap(b); 142 } 102 template <typename T> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(T optr) 103 { 104 if (optr) 105 JSRetain(optr); 106 T ptr = m_ptr; 107 m_ptr = optr; 108 if (ptr) 109 JSRelease(ptr); 110 return *this; 111 } 143 112 144 template <typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b) 145 { 146 return a.get() == b.get(); 147 } 113 template <typename T> inline void JSRetainPtr<T>::adopt(T optr) 114 { 115 T ptr = m_ptr; 116 m_ptr = optr; 117 if (ptr) 118 JSRelease(ptr); 119 } 148 120 149 template <typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, U* b) 150 { 151 return a.get() == b; 152 } 153 154 template <typename T, typename U> inline bool operator==(T* a, const JSRetainPtr<U>& b) 155 { 156 return a == b.get(); 157 } 158 159 template <typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b) 160 { 161 return a.get() != b.get(); 162 } 121 template <typename T> template <typename U> inline JSRetainPtr<T>& JSRetainPtr<T>::operator=(U* optr) 122 { 123 if (optr) 124 JSRetain(optr); 125 T ptr = m_ptr; 126 m_ptr = optr; 127 if (ptr) 128 JSRelease(ptr); 129 return *this; 130 } 163 131 164 template <typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, U* b)165 166 return a.get() != b;167 132 template <class T> inline void JSRetainPtr<T>::swap(JSRetainPtr<T>& o) 133 { 134 std::swap(m_ptr, o.m_ptr); 135 } 168 136 169 template <typename T, typename U> inline bool operator!=(T* a, const JSRetainPtr<U>& b)170 { 171 return a != b.get();172 137 template <class T> inline void swap(JSRetainPtr<T>& a, JSRetainPtr<T>& b) 138 { 139 a.swap(b); 140 } 173 141 174 } // namespace KJS 142 template <typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b) 143 { 144 return a.get() == b.get(); 145 } 175 146 176 using KJS::JSRetainPtr; 147 template <typename T, typename U> inline bool operator==(const JSRetainPtr<T>& a, U* b) 148 { 149 return a.get() == b; 150 } 177 151 178 #endif // KJS_JSRetainPtr_h 152 template <typename T, typename U> inline bool operator==(T* a, const JSRetainPtr<U>& b) 153 { 154 return a == b.get(); 155 } 156 157 template <typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, const JSRetainPtr<U>& b) 158 { 159 return a.get() != b.get(); 160 } 161 162 template <typename T, typename U> inline bool operator!=(const JSRetainPtr<T>& a, U* b) 163 { 164 return a.get() != b; 165 } 166 167 template <typename T, typename U> inline bool operator!=(T* a, const JSRetainPtr<U>& b) 168 { 169 return a != b.get(); 170 } 171 172 173 #endif // JSRetainPtr_h
Note:
See TracChangeset
for help on using the changeset viewer.