[webkit-changes] cvs commit: JavaScriptCore/kxmlcore PassRefPtr.h RefPtr.h

Maciej mjs at opensource.apple.com
Sun Dec 25 01:22:36 PST 2005


mjs         05/12/25 01:22:36

  Modified:    .        ChangeLog
               kjs      identifier.cpp property_map.cpp ustring.cpp
                        ustring.h
               kxmlcore PassRefPtr.h RefPtr.h
  Log:
          Reviewed by Eric and Dave Hyatt.
  
  	- make even const PassRefPtrs give transfer of ownership semantics
  	http://bugzilla.opendarwin.org/show_bug.cgi?id=6238
  
  	This is a somewhat cheesy change. Having to use PassRefPtr_Ref creates ambiguities
  	in assignment and copy construction. And this makes life way easier and removes
  	the need for pass(). It is not really correct, but we pretty much never need a real
  	const PassRefPtr, and this takes care of things for PassRefPtr temporaries.
  
          * kjs/identifier.cpp:
          (KJS::Identifier::add): No more need for pass()
          * kjs/property_map.cpp:
          (KJS::PropertyMap::addSparseArrayPropertiesToReferenceList): No more need for pass()
          * kjs/ustring.cpp:
          (KJS::UString::Rep::create): Use adoptRef
          (KJS::UString::UString): No more need for pass
          (KJS::UString::append): No more need for pass
          (KJS::UString::substr): No more need for pass
          * kxmlcore/PassRefPtr.h: made m_ptr mutable (ugh)
          (KXMLCore::PassRefPtr::PassRefPtr): Take a const PassRefPtr reference
          (KXMLCore::PassRefPtr::release): Made this a const method (ugh)
          (KXMLCore::PassRefPtr::operator=): clean up appropriately
          (KXMLCore::adoptRef): Added this to use instead of PassRefPtr<T>::adopt, I think
  	it makes the behavior more clear and it is less verbose.
          (KXMLCore::static_pointer_cast): use adoptRef
          (KXMLCore::const_pointer_cast): use adoptRef
          * kxmlcore/RefPtr.h:
          (KXMLCore::RefPtr::RefPtr): take const PassRefPtr&
          (KXMLCore::PassRefPtr::operator=): take const PassRefPtr&
  
  Revision  Changes    Path
  1.929     +33 -0     JavaScriptCore/ChangeLog
  
  Index: ChangeLog
  ===================================================================
  RCS file: /cvs/root/JavaScriptCore/ChangeLog,v
  retrieving revision 1.928
  retrieving revision 1.929
  diff -u -r1.928 -r1.929
  --- ChangeLog	25 Dec 2005 08:11:16 -0000	1.928
  +++ ChangeLog	25 Dec 2005 09:22:32 -0000	1.929
  @@ -1,3 +1,36 @@
  +2005-12-24  Maciej Stachowiak  <mjs at apple.com>
  +
  +        Reviewed by Eric and Dave Hyatt.
  +
  +	- make even const PassRefPtrs give transfer of ownership semantics
  +	http://bugzilla.opendarwin.org/show_bug.cgi?id=6238
  +	
  +	This is a somewhat cheesy change. Having to use PassRefPtr_Ref creates ambiguities
  +	in assignment and copy construction. And this makes life way easier and removes
  +	the need for pass(). It is not really correct, but we pretty much never need a real
  +	const PassRefPtr, and this takes care of things for PassRefPtr temporaries.
  +	
  +        * kjs/identifier.cpp:
  +        (KJS::Identifier::add): No more need for pass()
  +        * kjs/property_map.cpp:
  +        (KJS::PropertyMap::addSparseArrayPropertiesToReferenceList): No more need for pass()
  +        * kjs/ustring.cpp:
  +        (KJS::UString::Rep::create): Use adoptRef
  +        (KJS::UString::UString): No more need for pass
  +        (KJS::UString::append): No more need for pass
  +        (KJS::UString::substr): No more need for pass
  +        * kxmlcore/PassRefPtr.h: made m_ptr mutable (ugh)
  +        (KXMLCore::PassRefPtr::PassRefPtr): Take a const PassRefPtr reference
  +        (KXMLCore::PassRefPtr::release): Made this a const method (ugh)
  +        (KXMLCore::PassRefPtr::operator=): clean up appropriately
  +        (KXMLCore::adoptRef): Added this to use instead of PassRefPtr<T>::adopt, I think
  +	it makes the behavior more clear and it is less verbose.
  +        (KXMLCore::static_pointer_cast): use adoptRef
  +        (KXMLCore::const_pointer_cast): use adoptRef
  +        * kxmlcore/RefPtr.h:
  +        (KXMLCore::RefPtr::RefPtr): take const PassRefPtr&
  +        (KXMLCore::PassRefPtr::operator=): take const PassRefPtr&
  +
   2005-12-25  Eric Seidel  <eseidel at apple.com>
   
           Reviewed by mjs.
  
  
  
  1.27      +8 -8      JavaScriptCore/kjs/identifier.cpp
  
  Index: identifier.cpp
  ===================================================================
  RCS file: /cvs/root/JavaScriptCore/kjs/identifier.cpp,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- identifier.cpp	23 Dec 2005 01:52:42 -0000	1.26
  +++ identifier.cpp	25 Dec 2005 09:22:34 -0000	1.27
  @@ -129,12 +129,12 @@
   PassRefPtr<UString::Rep> Identifier::add(const char *c)
   {
       if (!c)
  -        return pass(&UString::Rep::null);
  +        return &UString::Rep::null;
       int length = strlen(c);
       if (length == 0)
  -        return pass(&UString::Rep::empty);
  +        return &UString::Rep::empty;
       
  -    return pass(*identifierTable().insert<const char *, CStringTranslator>(c).first);
  +    return *identifierTable().insert<const char *, CStringTranslator>(c).first;
   }
   
   struct UCharBuffer {
  @@ -172,24 +172,24 @@
   PassRefPtr<UString::Rep> Identifier::add(const UChar *s, int length)
   {
       if (length == 0)
  -        return pass(&UString::Rep::empty);
  +        return &UString::Rep::empty;
       
       UCharBuffer buf = {s, length}; 
  -    return pass(*identifierTable().insert<UCharBuffer, UCharBufferTranslator>(buf).first);
  +    return *identifierTable().insert<UCharBuffer, UCharBufferTranslator>(buf).first;
   }
   
   PassRefPtr<UString::Rep> Identifier::add(UString::Rep *r)
   {
       if (r->isIdentifier)
  -        return pass(r);
  +        return r;
   
       if (r->len == 0)
  -        return pass(&UString::Rep::empty);
  +        return &UString::Rep::empty;
   
       UString::Rep *result = *identifierTable().insert(r).first;
       if (result == r)
           r->isIdentifier = true;
  -    return pass(result);
  +    return result;
   }
   
   void Identifier::remove(UString::Rep *r)
  
  
  
  1.56      +4 -4      JavaScriptCore/kjs/property_map.cpp
  
  Index: property_map.cpp
  ===================================================================
  RCS file: /cvs/root/JavaScriptCore/kjs/property_map.cpp,v
  retrieving revision 1.55
  retrieving revision 1.56
  diff -u -r1.55 -r1.56
  --- property_map.cpp	20 Dec 2005 20:12:47 -0000	1.55
  +++ property_map.cpp	25 Dec 2005 09:22:34 -0000	1.56
  @@ -614,11 +614,11 @@
   #if USE_SINGLE_ENTRY
           UString::Rep *key = _singleEntry.key;
           if (key) {
  -            UString k(pass(key));
  +            UString k(key);
               bool fitsInUInt32;
               k.toUInt32(&fitsInUInt32);
               if (fitsInUInt32)
  -                list.append(Reference(base, Identifier(pass(key))));
  +                list.append(Reference(base, Identifier(key)));
           }
   #endif
           return;
  @@ -629,11 +629,11 @@
       for (int i = 0; i != size; ++i) {
           UString::Rep *key = entries[i].key;
           if (key && key != &UString::Rep::null) {
  -            UString k(pass(key));
  +            UString k(key);
               bool fitsInUInt32;
               k.toUInt32(&fitsInUInt32);
               if (fitsInUInt32)
  -                list.append(Reference(base, Identifier(pass(key))));
  +                list.append(Reference(base, Identifier(key)));
           }
       }
   }
  
  
  
  1.67      +8 -8      JavaScriptCore/kjs/ustring.cpp
  
  Index: ustring.cpp
  ===================================================================
  RCS file: /cvs/root/JavaScriptCore/kjs/ustring.cpp,v
  retrieving revision 1.66
  retrieving revision 1.67
  diff -u -r1.66 -r1.67
  --- ustring.cpp	20 Dec 2005 20:12:47 -0000	1.66
  +++ ustring.cpp	25 Dec 2005 09:22:34 -0000	1.67
  @@ -201,7 +201,7 @@
     r->preCapacity = 0;
   
     // steal the single reference this Rep was created with
  -  return PassRefPtr<Rep>::adopt(r);
  +  return adoptRef(r);
   }
   
   PassRefPtr<UString::Rep> UString::Rep::create(PassRefPtr<Rep> base, int offset, int length)
  @@ -231,7 +231,7 @@
     r->preCapacity = 0;
   
     // steal the single reference this Rep was created with
  -  return PassRefPtr<Rep>::adopt(r);
  +  return adoptRef(r);
   }
   
   void UString::Rep::destroy()
  @@ -459,7 +459,7 @@
       UString x(a);
       x.expandCapacity(aOffset + length);
       memcpy(const_cast<UChar *>(a.data() + aSize), b.data(), bSize * sizeof(UChar));
  -    m_rep = Rep::create(pass(a.m_rep), 0, length);
  +    m_rep = Rep::create(a.m_rep, 0, length);
     } else if (-bOffset == b.usedPreCapacity() && 4 * bSize >= aSize) {
       // - b reaches the beginning of its buffer so it qualifies for shared prepend
       // - also, it's at least a quarter the length of a - prepending to a much shorter
  @@ -467,7 +467,7 @@
       UString y(b);
       y.expandPreCapacity(-bOffset + aSize);
       memcpy(const_cast<UChar *>(b.data() - aSize), a.data(), aSize * sizeof(UChar));
  -    m_rep = Rep::create(pass(b.m_rep), -aSize, length);
  +    m_rep = Rep::create(b.m_rep, -aSize, length);
     } else {
       // a does not qualify for append, and b does not qualify for prepend, gotta make a whole new string
       int newCapacity = expandedSize(length, 0);
  @@ -685,7 +685,7 @@
       // this reaches the end of the buffer - extend it
       expandCapacity(thisOffset + length);
       memcpy(const_cast<UChar *>(data() + thisSize), t.data(), tSize * sizeof(UChar));
  -    m_rep = Rep::create(pass(m_rep), 0, length);
  +    m_rep = Rep::create(m_rep, 0, length);
     } else {
       // this is shared with someone using more capacity, gotta make a whole new string
       int newCapacity = expandedSize(length, 0);
  @@ -726,7 +726,7 @@
       UChar *d = const_cast<UChar *>(data());
       for (int i = 0; i < tSize; ++i)
         d[thisSize+i] = t[i];
  -    m_rep = Rep::create(pass(m_rep), 0, length);
  +    m_rep = Rep::create(m_rep, 0, length);
     } else {
       // this is shared with someone using more capacity, gotta make a whole new string
       int newCapacity = expandedSize(length, 0);
  @@ -766,7 +766,7 @@
       expandCapacity(thisOffset + length + 1);
       UChar *d = const_cast<UChar *>(data());
       d[length] = c;
  -    m_rep = Rep::create(pass(m_rep), 0, length + 1);
  +    m_rep = Rep::create(m_rep, 0, length + 1);
     } else {
       // this is shared with someone using more capacity, gotta make a whole new string
       int newCapacity = expandedSize((length + 1), 0);
  @@ -1122,7 +1122,7 @@
     if (pos == 0 && len == s)
       return *this;
   
  -  return UString(Rep::create(pass(m_rep), pos, len));
  +  return UString(Rep::create(m_rep, pos, len));
   }
   
   void UString::copyForWriting()
  
  
  
  1.47      +1 -1      JavaScriptCore/kjs/ustring.h
  
  Index: ustring.h
  ===================================================================
  RCS file: /cvs/root/JavaScriptCore/kjs/ustring.h,v
  retrieving revision 1.46
  retrieving revision 1.47
  diff -u -r1.46 -r1.47
  --- ustring.h	6 Dec 2005 19:38:51 -0000	1.46
  +++ ustring.h	25 Dec 2005 09:22:34 -0000	1.47
  @@ -462,7 +462,7 @@
   #endif
   
       Rep *rep() const { return m_rep.get(); }
  -    UString(PassRefPtr<Rep> r) { m_rep = r; }
  +    UString(PassRefPtr<Rep> r) : m_rep(r) { }
   
       void copyForWriting();
   
  
  
  
  1.7       +24 -49    JavaScriptCore/kxmlcore/PassRefPtr.h
  
  Index: PassRefPtr.h
  ===================================================================
  RCS file: /cvs/root/JavaScriptCore/kxmlcore/PassRefPtr.h,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- PassRefPtr.h	22 Dec 2005 16:48:08 -0000	1.6
  +++ PassRefPtr.h	25 Dec 2005 09:22:35 -0000	1.7
  @@ -27,15 +27,7 @@
   
       template<typename T> class RefPtr;
       template<typename T> class PassRefPtr;
  -
  -    // PassRefPtr_Ref class is a helper to allow proper passing of PassRefPtr by value but not by const
  -    // reference
  -    template<typename T> 
  -    struct PassRefPtr_Ref
  -    {
  -        T* m_ptr;
  -        explicit PassRefPtr_Ref(T* p) : m_ptr(p) {}
  -    };
  +    template <typename T> PassRefPtr<T> adoptRef(T *p);
   
       template<typename T> 
       class PassRefPtr
  @@ -43,8 +35,12 @@
       public:
           PassRefPtr() : m_ptr(0) {}
           PassRefPtr(T *ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }
  -        PassRefPtr(PassRefPtr& o) : m_ptr(o.release()) {}
  -        template <typename U> PassRefPtr(PassRefPtr<U>& o) : m_ptr(o.release()) { }
  +        // It somewhat breaks the type system to allow transfer of ownership out of
  +        // a const PassRefPtr. However, it makes it much easier to work with PassRefPtr
  +        // temporaries, and we don't really have a need to use real const PassRefPtrs 
  +        // anyway.
  +        PassRefPtr(const PassRefPtr& o) : m_ptr(o.release()) {}
  +        template <typename U> PassRefPtr(const PassRefPtr<U>& o) : m_ptr(o.release()) { }
   
           ~PassRefPtr() { if (T *ptr = m_ptr) ptr->deref(); }
           
  @@ -53,14 +49,8 @@
           
           T *get() const { return m_ptr; }
   
  -        T *release() { T *tmp = m_ptr; m_ptr = 0; return tmp; }
  +        T *release() const { T *tmp = m_ptr; m_ptr = 0; return tmp; }
   
  -        template <typename U> static PassRefPtr<T> adopt(U* ptr)
  -        {
  -            PassRefPtr result((PassRefPtr_Ref<T>(ptr)));
  -            return result;
  -        }
  -        
           T& operator*() const { return *m_ptr; }
           T *operator->() const { return m_ptr; }
           
  @@ -71,25 +61,15 @@
           operator UnspecifiedBoolType() const { return m_ptr ? &PassRefPtr::get : 0; }
           
           PassRefPtr& operator=(T *);
  -        PassRefPtr& operator=(PassRefPtr&);
  -        template <typename U> PassRefPtr& operator=(PassRefPtr<U>&);
  +        PassRefPtr& operator=(const PassRefPtr&);
  +        template <typename U> PassRefPtr& operator=(const PassRefPtr<U>&);
           template <typename U> PassRefPtr& operator=(const RefPtr<U>&);
   
  -        PassRefPtr(PassRefPtr_Ref<T> ref) : m_ptr(ref.m_ptr) { }
  -      
  -        PassRefPtr& operator=(PassRefPtr_Ref<T> ref)
  -        {
  -            if (m_ptr)
  -                m_ptr->deref();
  -            m_ptr = ref.m_ptr;
  -            return *this;
  -        }
  -
  -        template <typename U> operator PassRefPtr_Ref<U>() { return PassRefPtr_Ref<U>(release()); }
  -        template <typename U> operator PassRefPtr<U>() { return PassRefPtr_Ref<U>(release()); }
  -        
  +        friend PassRefPtr adoptRef<T>(T *);
       private:
  -        T *m_ptr;
  +        // adopting constructor
  +        PassRefPtr(T *ptr, bool) : m_ptr(ptr) {}
  +        mutable T *m_ptr;
       };
       
       template <typename T> template <typename U> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const RefPtr<U>& o) 
  @@ -113,7 +93,7 @@
           return *this;
       }
   
  -    template <typename T> inline PassRefPtr<T>& PassRefPtr<T>::operator=(PassRefPtr<T>& ref)
  +    template <typename T> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const PassRefPtr<T>& ref)
       {
           T *optr = ref.release();
           if (T *ptr = m_ptr)
  @@ -122,7 +102,7 @@
           return *this;
       }
       
  -    template <typename T> template <typename U> inline PassRefPtr<T>& PassRefPtr<T>::operator=(PassRefPtr<U>& ref)
  +    template <typename T> template <typename U> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const PassRefPtr<U>& ref)
       {
           T *optr = ref.release();
           if (T *ptr = m_ptr)
  @@ -181,31 +161,26 @@
           return a != b.get(); 
       }
       
  +    template <typename T> inline PassRefPtr<T> adoptRef(T *p)
  +    {
  +        return PassRefPtr<T>(p, true);
  +    }
  +
       template <typename T, typename U> inline PassRefPtr<T> static_pointer_cast(const PassRefPtr<U>& p) 
       { 
  -        return PassRefPtr<T>::adopt(static_cast<T *>(p.release())); 
  +        return adoptRef(static_cast<T *>(p.release())); 
       }
   
       template <typename T, typename U> inline PassRefPtr<T> const_pointer_cast(const PassRefPtr<U>& p) 
       { 
  -        return PassRefPtr<T>::adopt(const_cast<T *>(p.release())); 
  -    }
  -
  -    template <typename T> inline PassRefPtr<T> pass(T *ptr)
  -    {
  -        return PassRefPtr<T>(ptr);
  -    }
  -
  -    template <typename T> inline PassRefPtr<T> pass(const RefPtr<T>& ptr)
  -    {
  -        return PassRefPtr<T>(ptr);
  +        return adoptRef(const_cast<T *>(p.release())); 
       }
   
   } // namespace KXMLCore
   
   using KXMLCore::PassRefPtr;
  +using KXMLCore::adoptRef;
   using KXMLCore::static_pointer_cast;
   using KXMLCore::const_pointer_cast;
  -using KXMLCore::pass;
   
   #endif // KXMLCORE_PASS_REF_PTR_H
  
  
  
  1.12      +7 -31     JavaScriptCore/kxmlcore/RefPtr.h
  
  Index: RefPtr.h
  ===================================================================
  RCS file: /cvs/root/JavaScriptCore/kxmlcore/RefPtr.h,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- RefPtr.h	23 Dec 2005 08:34:42 -0000	1.11
  +++ RefPtr.h	25 Dec 2005 09:22:35 -0000	1.12
  @@ -28,7 +28,6 @@
   namespace KXMLCore {
   
       template <typename T> class PassRefPtr;
  -    template <typename T> class PassRefPtr_Ref;
   
       template <typename T> class RefPtr
       {
  @@ -36,8 +35,8 @@
           RefPtr() : m_ptr(0) {}
           RefPtr(T *ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }
           RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { if (T *ptr = m_ptr) ptr->ref(); }
  -        template <typename U> RefPtr(PassRefPtr<U>&);
  -        template <typename U> RefPtr(PassRefPtr_Ref<U>);
  +        // see comment in PassRefPtr.h for why this takes const reference
  +        template <typename U> RefPtr(const PassRefPtr<U>&);
   
           ~RefPtr() { if (T *ptr = m_ptr) ptr->deref(); }
           
  @@ -56,11 +55,9 @@
           
           RefPtr& operator=(const RefPtr&);
           RefPtr& operator=(T *);
  -        RefPtr& operator=(PassRefPtr<T>&);
  -        RefPtr& operator=(PassRefPtr_Ref<T>);
  +        RefPtr& operator=(const PassRefPtr<T>&);
           template <typename U> RefPtr& operator=(const RefPtr<U>&);
  -        template <typename U> RefPtr& operator=(PassRefPtr<U>&);
  -        template <typename U> RefPtr& operator=(PassRefPtr_Ref<U>);
  +        template <typename U> RefPtr& operator=(const PassRefPtr<U>&);
   
           void swap(RefPtr&);
   
  @@ -68,12 +65,7 @@
           T *m_ptr;
       };
       
  -    template <typename T> template <typename U> inline RefPtr<T>::RefPtr(PassRefPtr_Ref<U> ref)
  -        : m_ptr(ref.m_ptr)
  -    {
  -    }
  -    
  -    template <typename T> template <typename U> inline RefPtr<T>::RefPtr(PassRefPtr<U>& o)
  +    template <typename T> template <typename U> inline RefPtr<T>::RefPtr(const PassRefPtr<U>& o)
           : m_ptr(o.release())
       {
       }
  @@ -110,15 +102,7 @@
           return *this;
       }
   
  -    template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(PassRefPtr_Ref<U> ref)
  -    {
  -        if (m_ptr)
  -            m_ptr->deref();
  -        m_ptr = ref.m_ptr;
  -        return *this;
  -    }
  -    
  -    template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(PassRefPtr<T>& o)
  +    template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<T>& o)
       {
           if (m_ptr)
               m_ptr->deref();
  @@ -126,15 +110,7 @@
           return *this;
       }
   
  -    template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(PassRefPtr_Ref<T> ref)
  -    {
  -        if (m_ptr)
  -            m_ptr->deref();
  -        m_ptr = ref.m_ptr;
  -        return *this;
  -    }
  -    
  -    template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(PassRefPtr<U>& o)
  +    template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<U>& o)
       {
           if (m_ptr)
               m_ptr->deref();
  
  
  



More information about the webkit-changes mailing list