Ignore:
Timestamp:
Aug 30, 2005, 4:56:15 PM (20 years ago)
Author:
darin
Message:

JavaScriptCore:

Reviewed by John Sullivan.

  • kjs/shared_ptr.h: Updated namespace to KXMLCore instead of kxhmlcore. Made a few small improvements to use local variables a bit more and added an "operator int" to reduce the chance that we'll convert a SharedPtr to an int by accident. Also made the == operators normal functions rather than friend functions, added a couple of comemnts.
  • kjs/function.h: Updated for namespace change.
  • kjs/function.cpp: Ditto.
  • kjs/function_object.cpp: Ditto.
  • kjs/internal.h: Ditto.
  • kjs/internal.cpp: Ditto.
  • kjs/nodes.h: Ditto.
  • kjs/nodes2string.cpp: Ditto.

WebCore:

Reviewed by John Sullivan.

  • ForwardingHeaders/kjs/shared_ptr.h: Added.
  • khtml/misc/shared.h: Removed SharedPtr, and instead included <kjs/shared_ptr.h> and did some using statements to import the template into the khtml namespace.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/shared_ptr.h

    r10352 r10399  
    2424#define KXMLCORE_SHARED_PTR_H
    2525
    26 namespace kxmlcore {
     26namespace KXMLCore {
    2727
     28// FIXME: Change template name to RefPtr?
    2829template <class T> class SharedPtr
    2930{
    3031public:
    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(); }
    3536
    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(); }
    4038
    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; }
    4346   
    44     T * get() const { return m_ptr; }
     47    T *get() const { return m_ptr; }
     48
    4549    T &operator*() const { return *m_ptr; }
    4650    T *operator->() const { return m_ptr; }
    4751
    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; }
    5454
    5555    SharedPtr &operator=(const SharedPtr &);
     
    5757
    5858private:
    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
    6062};
    6163
    6264template <class T> SharedPtr<T> &SharedPtr<T>::operator=(const SharedPtr<T> &o)
    6365{
    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;
    6972    return *this;
    7073}
    7174
    72 template <class T> inline SharedPtr<T> &SharedPtr<T>::operator=(T *ptr)
     75template <class T> inline SharedPtr<T> &SharedPtr<T>::operator=(T *optr)
    7376{
    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;
    7982    return *this;
    8083}
    8184
    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); }
     85template <class T> inline bool operator==(const SharedPtr<T> &a, const SharedPtr<T> &b) { return a.get() == b.get(); }
     86template <class T> inline bool operator==(const SharedPtr<T> &a, const T *b) { return a.get() == b; }
     87template <class T> inline bool operator==(const T *a, const SharedPtr<T> &b) { return a == b.get(); }
     88
     89template <class T> inline bool operator!=(const SharedPtr<T> &a, const SharedPtr<T> &b) { return a.get() != b.get(); }
     90template <class T> inline bool operator!=(const SharedPtr<T> &a, const T *b) { return a.get() != b; }
     91template <class T> inline bool operator!=(const T *a, const SharedPtr<T> &b) { return a != b.get(); }
    8592
    8693template <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.