Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2009 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. AND ITS CONTRIBUTORS ``AS IS''
     14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
     17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     23  * THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef WeakGCPtr_h
     27 #define WeakGCPtr_h
     28 
     29 #include "Collector.h"
     30 #include <wtf/Noncopyable.h>
     31 
     32 namespace JSC {
     33 
     34 // A smart pointer whose get() function returns 0 for cells awaiting destruction.
     35 template <typename T> class WeakGCPtr : Noncopyable {
     36 public:
     37     WeakGCPtr() : m_ptr(0) { }
     38     WeakGCPtr(T* ptr) { assign(ptr); }
     39 
     40     T* get() const
     41     {
     42         if (!m_ptr || !Heap::isCellMarked(m_ptr))
     43             return 0;
     44         return m_ptr;
     45     }
     46 
     47     void clear(JSCell* ptr)
     48     {
     49         if (ptr == m_ptr)
     50             m_ptr = 0;
     51     }
     52 
     53     T& operator*() const { return *get(); }
     54     T* operator->() const { return get(); }
     55 
     56     bool operator!() const { return !get(); }
     57 
     58     // This conversion operator allows implicit conversion to bool but not to other integer types.
     59 #if COMPILER(WINSCW)
     60     operator bool() const { return m_ptr; }
     61 #else
     62     typedef T* WeakGCPtr::*UnspecifiedBoolType;
     63     operator UnspecifiedBoolType() const { return get() ? &WeakGCPtr::m_ptr : 0; }
     64 #endif
     65 
     66     WeakGCPtr& operator=(T*);
     67 
     68 private:
     69     void assign(T* ptr)
     70     {
     71         if (ptr)
     72             Heap::markCell(ptr);
     73         m_ptr = ptr;
     74     }
     75 
     76     T* m_ptr;
     77 };
     78 
     79 template <typename T> inline WeakGCPtr<T>& WeakGCPtr<T>::operator=(T* optr)
     80 {
     81     assign(optr);
     82     return *this;
     83 }
     84 
     85 template <typename T, typename U> inline bool operator==(const WeakGCPtr<T>& a, const WeakGCPtr<U>& b)
     86 {
     87     return a.get() == b.get();
     88 }
     89 
     90 template <typename T, typename U> inline bool operator==(const WeakGCPtr<T>& a, U* b)
     91 {
     92     return a.get() == b;
     93 }
     94 
     95 template <typename T, typename U> inline bool operator==(T* a, const WeakGCPtr<U>& b)
     96 {
     97     return a == b.get();
     98 }
     99 
    100 template <typename T, typename U> inline bool operator!=(const WeakGCPtr<T>& a, const WeakGCPtr<U>& b)
    101 {
    102     return a.get() != b.get();
    103 }
    104 
    105 template <typename T, typename U> inline bool operator!=(const WeakGCPtr<T>& a, U* b)
    106 {
    107     return a.get() != b;
    108 }
    109 
    110 template <typename T, typename U> inline bool operator!=(T* a, const WeakGCPtr<U>& b)
    111 {
    112     return a != b.get();
    113 }
    114 
    115 template <typename T, typename U> inline WeakGCPtr<T> static_pointer_cast(const WeakGCPtr<U>& p)
    116 {
    117     return WeakGCPtr<T>(static_cast<T*>(p.get()));
    118 }
    119 
    120 template <typename T, typename U> inline WeakGCPtr<T> const_pointer_cast(const WeakGCPtr<U>& p)
    121 {
    122     return WeakGCPtr<T>(const_cast<T*>(p.get()));
    123 }
    124 
    125 template <typename T> inline T* getPtr(const WeakGCPtr<T>& p)
    126 {
    127     return p.get();
    128 }
    129 
    130 } // namespace JSC
    131 
    132 #endif // WeakGCPtr_h
    133