1/*
2 * Copyright (C) 2007, 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 COMPUTER, 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 COMPUTER, 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 COMPtr_h
27#define COMPtr_h
28
29#ifndef NOMINMAX
30#define NOMINMAX
31#endif
32
33#include <unknwn.h>
34#include <wtf/Assertions.h>
35#include <wtf/HashTraits.h>
36
37#if !OS(WINCE)
38#include <guiddef.h>
39#endif
40
41typedef long HRESULT;
42
43// FIXME: Should we put this into the WebCore namespace and use "using" on it
44// as we do with things in WTF?
45
46enum AdoptCOMTag { AdoptCOM };
47enum QueryTag { Query };
48enum CreateTag { Create };
49
50template<typename T> class COMPtr {
51public:
52    COMPtr() : m_ptr(0) { }
53    COMPtr(T* ptr) : m_ptr(ptr) { if (m_ptr) m_ptr->AddRef(); }
54    COMPtr(AdoptCOMTag, T* ptr) : m_ptr(ptr) { }
55    COMPtr(const COMPtr& o) : m_ptr(o.m_ptr) { if (T* ptr = m_ptr) ptr->AddRef(); }
56
57    COMPtr(QueryTag, IUnknown* ptr) : m_ptr(copyQueryInterfaceRef(ptr)) { }
58    template<typename U> COMPtr(QueryTag, const COMPtr<U>& ptr) : m_ptr(copyQueryInterfaceRef(ptr.get())) { }
59
60    COMPtr(CreateTag, const IID& clsid) : m_ptr(createInstance(clsid)) { }
61
62    // Hash table deleted values, which are only constructed and never copied or destroyed.
63    COMPtr(WTF::HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { }
64    bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
65
66    ~COMPtr() { if (m_ptr) m_ptr->Release(); }
67
68    T* get() const { return m_ptr; }
69
70    void clear();
71    T* leakRef();
72
73    T& operator*() const { return *m_ptr; }
74    T* operator->() const { return m_ptr; }
75
76    T** operator&() { ASSERT(!m_ptr); return &m_ptr; }
77
78    bool operator!() const { return !m_ptr; }
79
80    // This conversion operator allows implicit conversion to bool but not to other integer types.
81    typedef T* (COMPtr::*UnspecifiedBoolType)() const;
82    operator UnspecifiedBoolType() const { return m_ptr ? &COMPtr::get : 0; }
83
84    COMPtr& operator=(const COMPtr&);
85    COMPtr& operator=(T*);
86    template<typename U> COMPtr& operator=(const COMPtr<U>&);
87
88    void query(IUnknown* ptr) { adoptRef(copyQueryInterfaceRef(ptr)); }
89    template<typename U> void query(const COMPtr<U>& ptr) { query(ptr.get()); }
90
91    void create(const IID& clsid) { adoptRef(createInstance(clsid)); }
92
93    template<typename U> HRESULT copyRefTo(U**);
94    void adoptRef(T*);
95
96private:
97    static T* copyQueryInterfaceRef(IUnknown*);
98    static T* createInstance(const IID& clsid);
99    static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); }
100
101    T* m_ptr;
102};
103
104template<typename T> inline void COMPtr<T>::clear()
105{
106    if (T* ptr = m_ptr) {
107        m_ptr = 0;
108        ptr->Release();
109    }
110}
111
112template<typename T> inline T* COMPtr<T>::leakRef()
113{
114    T* ptr = m_ptr;
115    m_ptr = 0;
116    return ptr;
117}
118
119template<typename T> inline T* COMPtr<T>::createInstance(const IID& clsid)
120{
121    T* result;
122    if (FAILED(CoCreateInstance(clsid, 0, CLSCTX_ALL, __uuidof(result), reinterpret_cast<void**>(&result))))
123        return 0;
124    return result;
125}
126
127template<typename T> inline T* COMPtr<T>::copyQueryInterfaceRef(IUnknown* ptr)
128{
129    if (!ptr)
130        return 0;
131    T* result;
132    if (FAILED(ptr->QueryInterface(&result)))
133        return 0;
134    return result;
135}
136
137template<typename T> template<typename U> inline HRESULT COMPtr<T>::copyRefTo(U** ptr)
138{
139    if (!ptr)
140        return E_POINTER;
141    *ptr = m_ptr;
142    if (m_ptr)
143        m_ptr->AddRef();
144    return S_OK;
145}
146
147template<typename T> inline void COMPtr<T>::adoptRef(T *ptr)
148{
149    if (m_ptr)
150        m_ptr->Release();
151    m_ptr = ptr;
152}
153
154template<typename T> inline COMPtr<T>& COMPtr<T>::operator=(const COMPtr<T>& o)
155{
156    T* optr = o.get();
157    if (optr)
158        optr->AddRef();
159    T* ptr = m_ptr;
160    m_ptr = optr;
161    if (ptr)
162        ptr->Release();
163    return *this;
164}
165
166template<typename T> template<typename U> inline COMPtr<T>& COMPtr<T>::operator=(const COMPtr<U>& o)
167{
168    T* optr = o.get();
169    if (optr)
170        optr->AddRef();
171    T* ptr = m_ptr;
172    m_ptr = optr;
173    if (ptr)
174        ptr->Release();
175    return *this;
176}
177
178template<typename T> inline COMPtr<T>& COMPtr<T>::operator=(T* optr)
179{
180    if (optr)
181        optr->AddRef();
182    T* ptr = m_ptr;
183    m_ptr = optr;
184    if (ptr)
185        ptr->Release();
186    return *this;
187}
188
189template<typename T, typename U> inline bool operator==(const COMPtr<T>& a, const COMPtr<U>& b)
190{
191    return a.get() == b.get();
192}
193
194template<typename T, typename U> inline bool operator==(const COMPtr<T>& a, U* b)
195{
196    return a.get() == b;
197}
198
199template<typename T, typename U> inline bool operator==(T* a, const COMPtr<U>& b)
200{
201    return a == b.get();
202}
203
204template<typename T, typename U> inline bool operator!=(const COMPtr<T>& a, const COMPtr<U>& b)
205{
206    return a.get() != b.get();
207}
208
209template<typename T, typename U> inline bool operator!=(const COMPtr<T>& a, U* b)
210{
211    return a.get() != b;
212}
213
214template<typename T, typename U> inline bool operator!=(T* a, const COMPtr<U>& b)
215{
216    return a != b.get();
217}
218
219namespace WTF {
220
221    template<typename P> struct HashTraits<COMPtr<P> > : GenericHashTraits<COMPtr<P> > {
222        static const bool emptyValueIsZero = true;
223        static void constructDeletedValue(COMPtr<P>& slot) { new (&slot) COMPtr<P>(HashTableDeletedValue); }
224        static bool isDeletedValue(const COMPtr<P>& value) { return value.isHashTableDeletedValue(); }
225    };
226
227    template<typename P> struct PtrHash<COMPtr<P> > : PtrHash<P*> {
228        using PtrHash<P*>::hash;
229        static unsigned hash(const COMPtr<P>& key) { return hash(key.get()); }
230        using PtrHash<P*>::equal;
231        static bool equal(const COMPtr<P>& a, const COMPtr<P>& b) { return a == b; }
232        static bool equal(P* a, const COMPtr<P>& b) { return a == b; }
233        static bool equal(const COMPtr<P>& a, P* b) { return a == b; }
234    };
235
236    template<typename P> struct DefaultHash<COMPtr<P> > { typedef PtrHash<COMPtr<P> > Hash; };
237}
238
239#endif
240