1926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)/*
2926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Copyright (C) 2013 Google, Inc. All Rights Reserved.
3926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *
4926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
5926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * modification, are permitted provided that the following conditions
6926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * are met:
7926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * 1. Redistributions of source code must retain the above copyright
8926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
9926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright
10926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer in the
11926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *    documentation and/or other materials provided with the distribution.
12926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *
13926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) */
25926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
26926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#ifndef WTF_WeakPtr_h
27926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#define WTF_WeakPtr_h
28926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
29591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "wtf/Noncopyable.h"
30591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "wtf/PassRefPtr.h"
31591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "wtf/RefPtr.h"
32591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "wtf/ThreadSafeRefCounted.h"
33591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "wtf/Threading.h"
34926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
35926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)namespace WTF {
36926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
37926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)template<typename T>
38926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class WeakReference : public ThreadSafeRefCounted<WeakReference<T> > {
39926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    WTF_MAKE_NONCOPYABLE(WeakReference<T>);
40926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    WTF_MAKE_FAST_ALLOCATED;
41926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)public:
42926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    static PassRefPtr<WeakReference<T> > create(T* ptr) { return adoptRef(new WeakReference(ptr)); }
43926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    static PassRefPtr<WeakReference<T> > createUnbound() { return adoptRef(new WeakReference()); }
44926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
45926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    T* get() const
46926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    {
47926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        ASSERT(m_boundThread == currentThread());
48926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return m_ptr;
49926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
50926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
51926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    void clear()
52926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    {
53926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        ASSERT(m_boundThread == currentThread());
54926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        m_ptr = 0;
55926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
56926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
57926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    void bindTo(T* ptr)
58926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    {
59926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        ASSERT(!m_ptr);
60197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#if ENABLE(ASSERT)
61926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        m_boundThread = currentThread();
62926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#endif
63926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        m_ptr = ptr;
64926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
65926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
66926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)private:
67926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    WeakReference() : m_ptr(0) { }
68926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
69926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    explicit WeakReference(T* ptr)
70926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        : m_ptr(ptr)
71197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#if ENABLE(ASSERT)
72926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        , m_boundThread(currentThread())
73926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#endif
74926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    {
75926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
76926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
77926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    T* m_ptr;
78197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#if ENABLE(ASSERT)
79926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    ThreadIdentifier m_boundThread;
80926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#endif
81926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)};
82926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
83926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)template<typename T>
84926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class WeakPtr {
85926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    WTF_MAKE_FAST_ALLOCATED;
86926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)public:
87926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    WeakPtr() { }
88f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    WeakPtr(std::nullptr_t) { }
89926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    WeakPtr(PassRefPtr<WeakReference<T> > ref) : m_ref(ref) { }
90926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
91e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    T* get() const { return m_ref ? m_ref->get() : 0; }
92323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    void clear() { m_ref.clear(); }
93926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
9409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    T* operator->() const
9509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    {
9609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        ASSERT(get());
9709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        return get();
9809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    }
9909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
1008abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    typedef RefPtr<WeakReference<T> > (WeakPtr::*UnspecifiedBoolType);
1011e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    operator UnspecifiedBoolType() const { return get() ? &WeakPtr::m_ref : 0; }
1028abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
103926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)private:
104926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    RefPtr<WeakReference<T> > m_ref;
105926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)};
106926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
10709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)template<typename T, typename U> inline bool operator==(const WeakPtr<T>& a, const WeakPtr<U>& b)
10809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
10909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return a.get() == b.get();
11009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
11109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
11209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)template<typename T, typename U> inline bool operator!=(const WeakPtr<T>& a, const WeakPtr<U>& b)
11309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
11409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return a.get() != b.get();
11509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
11609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
117926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)template<typename T>
118926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class WeakPtrFactory {
119926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    WTF_MAKE_NONCOPYABLE(WeakPtrFactory<T>);
120926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    WTF_MAKE_FAST_ALLOCATED;
121926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)public:
122926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    explicit WeakPtrFactory(T* ptr) : m_ref(WeakReference<T>::create(ptr)) { }
123926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
124926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    WeakPtrFactory(PassRefPtr<WeakReference<T> > ref, T* ptr)
125926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        : m_ref(ref)
126926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    {
127926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        m_ref->bindTo(ptr);
128926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
129926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
130926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    ~WeakPtrFactory() { m_ref->clear(); }
131926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
132926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    // We should consider having createWeakPtr populate m_ref the first time createWeakPtr is called.
133926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    WeakPtr<T> createWeakPtr() { return WeakPtr<T>(m_ref); }
134926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
135926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    void revokeAll()
136926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    {
137926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        T* ptr = m_ref->get();
138926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        m_ref->clear();
139926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        // We create a new WeakReference so that future calls to createWeakPtr() create nonzero WeakPtrs.
140926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        m_ref = WeakReference<T>::create(ptr);
141926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
142926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
143926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)private:
144926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    RefPtr<WeakReference<T> > m_ref;
145926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)};
146926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
147926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} // namespace WTF
148926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
149926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)using WTF::WeakPtr;
150926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)using WTF::WeakPtrFactory;
151926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)using WTF::WeakReference;
152926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
153926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#endif
154