Changeset 10399 in webkit for trunk/JavaScriptCore/kjs/shared_ptr.h
- Timestamp:
- Aug 30, 2005, 4:56:15 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/shared_ptr.h
r10352 r10399 24 24 #define KXMLCORE_SHARED_PTR_H 25 25 26 namespace kxmlcore {26 namespace KXMLCore { 27 27 28 // FIXME: Change template name to RefPtr? 28 29 template <class T> class SharedPtr 29 30 { 30 31 public: 31 SharedPtr() : m_ptr( 0) {}32 explicit SharedPtr(T *ptr) : m_ptr(ptr) { if (m_ptr) m_ptr->ref(); }33 SharedPtr(const SharedPtr &o) : m_ptr(o.m_ptr) { if ( m_ptr) m_ptr->ref(); }34 ~SharedPtr() { if ( m_ptr) m_ptr->deref(); }32 SharedPtr() : m_ptr(NULL) {} 33 SharedPtr(T *ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); } 34 SharedPtr(const SharedPtr &o) : m_ptr(o.m_ptr) { if (T *ptr = m_ptr) ptr->ref(); } 35 ~SharedPtr() { if (T *ptr = m_ptr) ptr->deref(); } 35 36 36 template <class U> explicit SharedPtr(SharedPtr<U> o) : m_ptr(o.get()) { if (m_ptr) m_ptr->ref(); } 37 38 bool isNull() const { return m_ptr == 0; } 39 bool notNull() const { return m_ptr != 0; } 37 template <class U> SharedPtr(const SharedPtr<U> &o) : m_ptr(o.get()) { if (T *ptr = m_ptr) ptr->ref(); } 40 38 41 void reset() { if (m_ptr) m_ptr->deref(); m_ptr = 0; } 42 void reset(T *o) { if (o) o->ref(); if (m_ptr) m_ptr->deref(); m_ptr = o; } 39 // FIXME: Deprecate in favor of operators below, then remove? 40 bool isNull() const { return m_ptr == NULL; } 41 bool notNull() const { return m_ptr != NULL; } 42 43 // FIXME: Deprecate in favor of operator=, then remove? 44 void reset() { if (T *ptr = m_ptr) ptr->deref(); m_ptr = NULL; } 45 void reset(T *o) { if (o) o->ref(); if (T *ptr = m_ptr) ptr->deref(); m_ptr = o; } 43 46 44 T * get() const { return m_ptr; } 47 T *get() const { return m_ptr; } 48 45 49 T &operator*() const { return *m_ptr; } 46 50 T *operator->() const { return m_ptr; } 47 51 48 bool operator!() const { return m_ptr == 0; } 49 operator bool() const { return m_ptr != 0; } 50 51 inline friend bool operator==(const SharedPtr &a, const SharedPtr &b) { return a.m_ptr == b.m_ptr; } 52 inline friend bool operator==(const SharedPtr &a, const T *b) { return a.m_ptr == b; } 53 inline friend bool operator==(const T *a, const SharedPtr &b) { return a == b.m_ptr; } 52 bool operator!() const { return m_ptr == NULL; } 53 operator bool() const { return m_ptr != NULL; } 54 54 55 55 SharedPtr &operator=(const SharedPtr &); … … 57 57 58 58 private: 59 T* m_ptr; 59 T *m_ptr; 60 61 operator int() const; // deliberately not implemented; helps prevent operator bool from converting to int accidentally 60 62 }; 61 63 62 64 template <class T> SharedPtr<T> &SharedPtr<T>::operator=(const SharedPtr<T> &o) 63 65 { 64 if (o.m_ptr) 65 o.m_ptr->ref(); 66 if (m_ptr) 67 m_ptr->deref(); 68 m_ptr = o.m_ptr; 66 T *optr = o.m_ptr; 67 if (optr) 68 optr->ref(); 69 if (T *ptr = m_ptr) 70 ptr->deref(); 71 m_ptr = optr; 69 72 return *this; 70 73 } 71 74 72 template <class T> inline SharedPtr<T> &SharedPtr<T>::operator=(T * ptr)75 template <class T> inline SharedPtr<T> &SharedPtr<T>::operator=(T *optr) 73 76 { 74 if ( ptr)75 ptr->ref();76 if ( m_ptr)77 m_ptr->deref();78 m_ptr = ptr;77 if (optr) 78 optr->ref(); 79 if (T *ptr = m_ptr) 80 ptr->deref(); 81 m_ptr = optr; 79 82 return *this; 80 83 } 81 84 82 template <class T> inline bool operator!=(const SharedPtr<T> &a, const SharedPtr<T> &b) { return !(a==b); } 83 template <class T> inline bool operator!=(const SharedPtr<T> &a, const T *b) { return !(a == b); } 84 template <class T> inline bool operator!=(const T *a, const SharedPtr<T> &b) { return !(a == b); } 85 template <class T> inline bool operator==(const SharedPtr<T> &a, const SharedPtr<T> &b) { return a.get() == b.get(); } 86 template <class T> inline bool operator==(const SharedPtr<T> &a, const T *b) { return a.get() == b; } 87 template <class T> inline bool operator==(const T *a, const SharedPtr<T> &b) { return a == b.get(); } 88 89 template <class T> inline bool operator!=(const SharedPtr<T> &a, const SharedPtr<T> &b) { return a.get() != b.get(); } 90 template <class T> inline bool operator!=(const SharedPtr<T> &a, const T *b) { return a.get() != b; } 91 template <class T> inline bool operator!=(const T *a, const SharedPtr<T> &b) { return a != b.get(); } 85 92 86 93 template <class T, class U> inline SharedPtr<T> static_pointer_cast(const SharedPtr<U> &p) { return SharedPtr<T>(static_cast<T *>(p.get())); }
Note:
See TracChangeset
for help on using the changeset viewer.