Home | History | Annotate | Download | only in wtf
      1 /*
      2  * Copyright (C) 2010 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef WTF_PassOwnArrayPtr_h
     27 #define WTF_PassOwnArrayPtr_h
     28 
     29 #include "wtf/Assertions.h"
     30 #include "wtf/NullPtr.h"
     31 #include "wtf/TypeTraits.h"
     32 
     33 namespace WTF {
     34 
     35 template<typename T> class OwnArrayPtr;
     36 template<typename T> class PassOwnArrayPtr;
     37 template<typename T> PassOwnArrayPtr<T> adoptArrayPtr(T*);
     38 template<typename T> void deleteOwnedArrayPtr(T* ptr);
     39 
     40 template<typename T> class PassOwnArrayPtr {
     41     WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(PassOwnArrayPtr);
     42 public:
     43     typedef T* PtrType;
     44 
     45     PassOwnArrayPtr() : m_ptr(0) { }
     46     PassOwnArrayPtr(std::nullptr_t) : m_ptr(0) { }
     47 
     48     // It somewhat breaks the type system to allow transfer of ownership out of
     49     // a const PassOwnArrayPtr. However, it makes it much easier to work with PassOwnArrayPtr
     50     // temporaries, and we don't have a need to use real const PassOwnArrayPtrs anyway.
     51     PassOwnArrayPtr(const PassOwnArrayPtr& o) : m_ptr(o.leakPtr()) { }
     52     template<typename U> PassOwnArrayPtr(const PassOwnArrayPtr<U>& o) : m_ptr(o.leakPtr()) { }
     53 
     54     ~PassOwnArrayPtr() { deleteOwnedArrayPtr(m_ptr); }
     55 
     56     PtrType get() const { return m_ptr; }
     57 
     58     PtrType leakPtr() const 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     bool operator!() const { return !m_ptr; }
     64 
     65     // This conversion operator allows implicit conversion to bool but not to other integer types.
     66     typedef PtrType PassOwnArrayPtr::*UnspecifiedBoolType;
     67     operator UnspecifiedBoolType() const { return m_ptr ? &PassOwnArrayPtr::m_ptr : 0; }
     68 
     69     PassOwnArrayPtr& operator=(const PassOwnArrayPtr&) { COMPILE_ASSERT(!sizeof(T*), PassOwnArrayPtr_should_never_be_assigned_to); return *this; }
     70 
     71     template<typename U> friend PassOwnArrayPtr<U> adoptArrayPtr(U*);
     72 
     73 private:
     74     explicit PassOwnArrayPtr(PtrType ptr) : m_ptr(ptr) { }
     75 
     76     mutable PtrType m_ptr;
     77 };
     78 
     79 template<typename T> inline typename PassOwnArrayPtr<T>::PtrType PassOwnArrayPtr<T>::leakPtr() const
     80 {
     81     PtrType ptr = m_ptr;
     82     m_ptr = 0;
     83     return ptr;
     84 }
     85 
     86 template<typename T, typename U> inline bool operator==(const PassOwnArrayPtr<T>& a, const PassOwnArrayPtr<U>& b)
     87 {
     88     return a.get() == b.get();
     89 }
     90 
     91 template<typename T, typename U> inline bool operator==(const PassOwnArrayPtr<T>& a, const OwnArrayPtr<U>& b)
     92 {
     93     return a.get() == b.get();
     94 }
     95 
     96 template<typename T, typename U> inline bool operator==(const OwnArrayPtr<T>& a, const PassOwnArrayPtr<U>& b)
     97 {
     98     return a.get() == b.get();
     99 }
    100 
    101 template<typename T, typename U> inline bool operator==(const PassOwnArrayPtr<T>& a, U* b)
    102 {
    103     return a.get() == b;
    104 }
    105 
    106 template<typename T, typename U> inline bool operator==(T* a, const PassOwnArrayPtr<U>& b)
    107 {
    108     return a == b.get();
    109 }
    110 
    111 template<typename T, typename U> inline bool operator!=(const PassOwnArrayPtr<T>& a, const PassOwnArrayPtr<U>& b)
    112 {
    113     return a.get() != b.get();
    114 }
    115 
    116 template<typename T, typename U> inline bool operator!=(const PassOwnArrayPtr<T>& a, const OwnArrayPtr<U>& b)
    117 {
    118     return a.get() != b.get();
    119 }
    120 
    121 template<typename T, typename U> inline bool operator!=(const OwnArrayPtr<T>& a, const PassOwnArrayPtr<U>& b)
    122 {
    123     return a.get() != b.get();
    124 }
    125 
    126 template<typename T, typename U> inline bool operator!=(const PassOwnArrayPtr<T>& a, U* b)
    127 {
    128     return a.get() != b;
    129 }
    130 
    131 template<typename T, typename U> inline bool operator!=(T* a, const PassOwnArrayPtr<U>& b)
    132 {
    133     return a != b.get();
    134 }
    135 
    136 template<typename T> inline PassOwnArrayPtr<T> adoptArrayPtr(T* ptr)
    137 {
    138     return PassOwnArrayPtr<T>(ptr);
    139 }
    140 
    141 template<typename T> inline void deleteOwnedArrayPtr(T* ptr)
    142 {
    143     typedef char known[sizeof(T) ? 1 : -1];
    144     if (sizeof(known))
    145         delete [] ptr;
    146 }
    147 
    148 template<typename T, typename U> inline PassOwnArrayPtr<T> static_pointer_cast(const PassOwnArrayPtr<U>& p)
    149 {
    150     return adoptArrayPtr(static_cast<T*>(p.leakPtr()));
    151 }
    152 
    153 template<typename T, typename U> inline PassOwnArrayPtr<T> const_pointer_cast(const PassOwnArrayPtr<U>& p)
    154 {
    155     return adoptArrayPtr(const_cast<T*>(p.leakPtr()));
    156 }
    157 
    158 template<typename T> inline T* getPtr(const PassOwnArrayPtr<T>& p)
    159 {
    160     return p.get();
    161 }
    162 
    163 } // namespace WTF
    164 
    165 using WTF::PassOwnArrayPtr;
    166 using WTF::adoptArrayPtr;
    167 using WTF::const_pointer_cast;
    168 using WTF::static_pointer_cast;
    169 
    170 #endif // WTF_PassOwnArrayPtr_h
    171