JavaScriptCore/wtf/OwnArrayPtr.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  *  Copyright (C) 2006, 2010 Apple Inc. All rights reserved.
       
     3  *
       
     4  *  This library is free software; you can redistribute it and/or
       
     5  *  modify it under the terms of the GNU Library General Public
       
     6  *  License as published by the Free Software Foundation; either
       
     7  *  version 2 of the License, or (at your option) any later version.
       
     8  *
       
     9  *  This library is distributed in the hope that it will be useful,
       
    10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    12  *  Library General Public License for more details.
       
    13  *
       
    14  *  You should have received a copy of the GNU Library General Public License
       
    15  *  along with this library; see the file COPYING.LIB.  If not, write to
       
    16  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    17  *  Boston, MA 02110-1301, USA.
       
    18  *
       
    19  */
       
    20 
       
    21 #ifndef WTF_OwnArrayPtr_h
       
    22 #define WTF_OwnArrayPtr_h
       
    23 
       
    24 #include <algorithm>
       
    25 #include <wtf/Assertions.h>
       
    26 #include <wtf/Noncopyable.h>
       
    27 
       
    28 namespace WTF {
       
    29 
       
    30     template <typename T> class OwnArrayPtr : public Noncopyable {
       
    31     public:
       
    32         explicit OwnArrayPtr(T* ptr = 0) : m_ptr(ptr) { }
       
    33         ~OwnArrayPtr() { safeDelete(m_ptr); }
       
    34 
       
    35         T* get() const { return m_ptr; }
       
    36         T* release() { T* ptr = m_ptr; m_ptr = 0; return ptr; }
       
    37 
       
    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         }
       
    46 
       
    47         void clear();
       
    48 
       
    49         T& operator*() const { ASSERT(m_ptr); return *m_ptr; }
       
    50         T* operator->() const { ASSERT(m_ptr); return m_ptr; }
       
    51 
       
    52         T& operator[](std::ptrdiff_t i) const { ASSERT(m_ptr); ASSERT(i >= 0); return m_ptr[i]; }
       
    53 
       
    54         bool operator!() const { return !m_ptr; }
       
    55 
       
    56         // This conversion operator allows implicit conversion to bool but not to other integer types.
       
    57 #if COMPILER(WINSCW)
       
    58         operator bool() const { return m_ptr; }
       
    59 #else
       
    60         typedef T* OwnArrayPtr::*UnspecifiedBoolType;
       
    61         operator UnspecifiedBoolType() const { return m_ptr ? &OwnArrayPtr::m_ptr : 0; }
       
    62 #endif
       
    63 
       
    64         void swap(OwnArrayPtr& o) { std::swap(m_ptr, o.m_ptr); }
       
    65 
       
    66     private:
       
    67         static void safeDelete(T*);
       
    68 
       
    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     }
       
    78 
       
    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     }
       
    85 
       
    86     template <typename T> inline void swap(OwnArrayPtr<T>& a, OwnArrayPtr<T>& b) { a.swap(b); }
       
    87 
       
    88     template <typename T> inline T* getPtr(const OwnArrayPtr<T>& p)
       
    89     {
       
    90         return p.get();
       
    91     }
       
    92 
       
    93 } // namespace WTF
       
    94 
       
    95 using WTF::OwnArrayPtr;
       
    96 
       
    97 #endif // WTF_OwnArrayPtr_h