Ignore:
Timestamp:
Aug 26, 2010, 2:38:11 PM (15 years ago)
Author:
[email protected]
Message:

Add PassOwnArrayPtr
https://bugs.webkit.org/show_bug.cgi?id=44627

Reviewed by Darin Adler.

JavaScriptCore:

Add the new files.

  • wtf/Forward.h:

Forward declare PassOwnArrayPtr.

  • wtf/OwnArrayPtr.h:

Mimic the OwnPtr interface.

  • wtf/OwnArrayPtrCommon.h: Added.

(WTF::deleteOwnedArrayPtr):
Move delete function here so it can be shared by OwnArrayPtr and
PassOwnArrayPtr.

  • wtf/PassOwnArrayPtr.h: Added.

Mimic the PassOwnPtr interface.

JavaScriptGlue:

Add new forwarding header.

  • ForwardingHeaders/wtf/OwnArrayPtrCommon.h: Added.

WebCore:

Add new forwarding headers.

  • ForwardingHeaders/wtf/OwnArrayPtrCommon.h: Added.
  • ForwardingHeaders/wtf/PassOwnArrayPtr.h: Added.

WebKit2:

  • UIProcess/WebContext.cpp:

(WebKit::PostMessageEncoder::PostMessageDecoder::decode):

  • WebProcess/InjectedBundle/InjectedBundle.cpp:

(WebKit::PostMessageEncoder::PostMessageDecoder::decode):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/OwnArrayPtr.h

    r62674 r66139  
    2222#define WTF_OwnArrayPtr_h
    2323
     24#include "Assertions.h"
     25#include "Noncopyable.h"
     26#include "OwnArrayPtrCommon.h"
    2427#include <algorithm>
    25 #include <wtf/Assertions.h>
    26 #include <wtf/Noncopyable.h>
     28
     29// Remove this once we make all WebKit code compatible with stricter rules about OwnArrayPtr.
     30#define LOOSE_OWN_ARRAY_PTR
    2731
    2832namespace WTF {
    2933
    30     template <typename T> class OwnArrayPtr : public Noncopyable {
    31     public:
    32         explicit OwnArrayPtr(T* ptr = 0) : m_ptr(ptr) { }
    33         ~OwnArrayPtr() { safeDelete(m_ptr); }
     34template<typename T> class PassOwnArrayPtr;
     35template<typename T> PassOwnArrayPtr<T> adoptArrayPtr(T*);
    3436
    35         T* get() const { return m_ptr; }
    36         T* release() { T* ptr = m_ptr; m_ptr = 0; return ptr; }
     37template <typename T> class OwnArrayPtr : public Noncopyable {
     38public:
     39    typedef T* PtrType;
    3740
    38         // FIXME: This should be removed and replaced with PassOwnArrayPtr.
    39         void set(T* ptr)
    40         {
    41             ASSERT(!ptr || m_ptr != ptr);
    42             T* oldPtr = m_ptr;
    43             m_ptr = ptr;
    44             safeDelete(oldPtr);
    45         }
     41    OwnArrayPtr() : m_ptr(0) { }
    4642
    47         void clear();
     43    // See comment in PassOwnArrayPtr.h for why this takes a const reference.
     44    template<typename U> OwnArrayPtr(const PassOwnArrayPtr<U>& o);
    4845
    49         T& operator*() const { ASSERT(m_ptr); return *m_ptr; }
    50         T* operator->() const { ASSERT(m_ptr); return m_ptr; }
     46    // This copy constructor is used implicitly by gcc when it generates
     47    // transients for assigning a PassOwnArrayPtr<T> object to a stack-allocated
     48    // OwnArrayPtr<T> object. It should never be called explicitly and gcc
     49    // should optimize away the constructor when generating code.
     50    OwnArrayPtr(const OwnArrayPtr<T>&);
    5151
    52         T& operator[](std::ptrdiff_t i) const { ASSERT(m_ptr); ASSERT(i >= 0); return m_ptr[i]; }
     52    ~OwnArrayPtr() { deleteOwnedArrayPtr(m_ptr); }
    5353
    54         bool operator!() const { return !m_ptr; }
     54    PtrType get() const { return m_ptr; }
    5555
    56         // This conversion operator allows implicit conversion to bool but not to other integer types.
     56    void clear();
     57    PassOwnArrayPtr<T> release();
     58    PtrType leakPtr() WARN_UNUSED_RETURN;
     59
     60    T& operator*() const { ASSERT(m_ptr); return *m_ptr; }
     61    PtrType operator->() const { ASSERT(m_ptr); return m_ptr; }
     62
     63    T& operator[](std::ptrdiff_t i) const { ASSERT(m_ptr); ASSERT(i >= 0); return m_ptr[i]; }
     64
     65    bool operator!() const { return !m_ptr; }
     66
     67    // This conversion operator allows implicit conversion to bool but not to other integer types.
    5768#if COMPILER(WINSCW)
    58         operator bool() const { return m_ptr; }
     69    operator bool() const { return m_ptr; }
    5970#else
    60         typedef T* OwnArrayPtr::*UnspecifiedBoolType;
    61         operator UnspecifiedBoolType() const { return m_ptr ? &OwnArrayPtr::m_ptr : 0; }
     71    typedef T* OwnArrayPtr::*UnspecifiedBoolType;
     72    operator UnspecifiedBoolType() const { return m_ptr ? &OwnArrayPtr::m_ptr : 0; }
    6273#endif
    6374
    64         void swap(OwnArrayPtr& o) { std::swap(m_ptr, o.m_ptr); }
     75    OwnArrayPtr& operator=(const PassOwnArrayPtr<T>&);
     76    template<typename U> OwnArrayPtr& operator=(const PassOwnArrayPtr<U>&);
    6577
    66     private:
    67         static void safeDelete(T*);
     78    void swap(OwnArrayPtr& o) { std::swap(m_ptr, o.m_ptr); }
    6879
    69         T* m_ptr;
    70     };
    71    
    72     template<typename T> inline void OwnArrayPtr<T>::clear()
    73     {
    74         T* ptr = m_ptr;
    75         m_ptr = 0;
    76         safeDelete(ptr);
    77     }
     80#ifdef LOOSE_OWN_ARRAY_PTR
     81    explicit OwnArrayPtr(PtrType ptr) : m_ptr(ptr) { }
     82    void set(PtrType);
     83#endif
    7884
    79     template<typename T> inline void OwnArrayPtr<T>::safeDelete(T* ptr)
    80     {
    81         typedef char known[sizeof(T) ? 1 : -1];
    82         if (sizeof(known))
    83             delete [] ptr;
    84     }
     85private:
     86    PtrType m_ptr;
     87};
    8588
    86     template <typename T> inline void swap(OwnArrayPtr<T>& a, OwnArrayPtr<T>& b) { a.swap(b); }
     89template<typename T> template<typename U> inline OwnArrayPtr<T>::OwnArrayPtr(const PassOwnArrayPtr<U>& o)
     90    : m_ptr(o.leakPtr())
     91{
     92}
    8793
    88     template <typename T> inline T* getPtr(const OwnArrayPtr<T>& p)
    89     {
    90         return p.get();
    91     }
     94template<typename T> inline void OwnArrayPtr<T>::clear()
     95{
     96    PtrType ptr = m_ptr;
     97    m_ptr = 0;
     98    deleteOwnedArrayPtr(ptr);
     99}
     100
     101template<typename T> inline PassOwnArrayPtr<T> OwnArrayPtr<T>::release()
     102{
     103    PtrType ptr = m_ptr;
     104    m_ptr = 0;
     105    return adoptArrayPtr(ptr);
     106}
     107
     108template<typename T> inline typename OwnArrayPtr<T>::PtrType OwnArrayPtr<T>::leakPtr()
     109{
     110    PtrType ptr = m_ptr;
     111    m_ptr = 0;
     112    return ptr;
     113}
     114
     115#ifdef LOOSE_OWN_ARRAY_PTR
     116template<typename T> inline void OwnArrayPtr<T>::set(PtrType ptr)
     117{
     118    ASSERT(!ptr || m_ptr != ptr);
     119    PtrType oldPtr = m_ptr;
     120    m_ptr = ptr;
     121    deleteOwnedPtr(oldPtr);
     122}
     123#endif
     124
     125template<typename T> inline OwnArrayPtr<T>& OwnArrayPtr<T>::operator=(const PassOwnArrayPtr<T>& o)
     126{
     127    PtrType ptr = m_ptr;
     128    m_ptr = o.leakPtr();
     129    ASSERT(!ptr || m_ptr != ptr);
     130    deleteOwnedArrayPtr(ptr);
     131    return *this;
     132}
     133
     134template<typename T> template<typename U> inline OwnArrayPtr<T>& OwnArrayPtr<T>::operator=(const PassOwnArrayPtr<U>& o)
     135{
     136    PtrType ptr = m_ptr;
     137    m_ptr = o.leakPtr();
     138    ASSERT(!ptr || m_ptr != ptr);
     139    deleteOwnedArrayPtr(ptr);
     140    return *this;
     141}
     142
     143template <typename T> inline void swap(OwnArrayPtr<T>& a, OwnArrayPtr<T>& b)
     144{
     145    a.swap(b);
     146}
     147
     148template<typename T, typename U> inline bool operator==(const OwnArrayPtr<T>& a, U* b)
     149{
     150    return a.get() == b;
     151}
     152
     153template<typename T, typename U> inline bool operator==(T* a, const OwnArrayPtr<U>& b)
     154{
     155    return a == b.get();
     156}
     157
     158template<typename T, typename U> inline bool operator!=(const OwnArrayPtr<T>& a, U* b)
     159{
     160    return a.get() != b;
     161}
     162
     163template<typename T, typename U> inline bool operator!=(T* a, const OwnArrayPtr<U>& b)
     164{
     165    return a != b.get();
     166}
     167
     168template <typename T> inline T* getPtr(const OwnArrayPtr<T>& p)
     169{
     170    return p.get();
     171}
    92172
    93173} // namespace WTF
Note: See TracChangeset for help on using the changeset viewer.