2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
33 #include <WTF/Assertions.h>
34 #include <WTF/HashTraits.h>
38 // FIXME: Should we put this into the WebCore namespace and use "using" on it
39 // as we do with things in WTF?
41 enum AdoptCOMTag { AdoptCOM };
42 enum QueryTag { Query };
43 enum CreateTag { Create };
45 template <typename T> class COMPtr {
47 COMPtr() : m_ptr(0) { }
48 COMPtr(T* ptr) : m_ptr(ptr) { if (m_ptr) m_ptr->AddRef(); }
49 COMPtr(AdoptCOMTag, T* ptr) : m_ptr(ptr) { }
50 COMPtr(const COMPtr& o) : m_ptr(o.m_ptr) { if (T* ptr = m_ptr) ptr->AddRef(); }
52 inline COMPtr(QueryTag, IUnknown* ptr) : m_ptr(copyQueryInterfaceRef(ptr)) { }
53 template <typename U> inline COMPtr(QueryTag, const COMPtr<U>& ptr) : m_ptr(copyQueryInterfaceRef(ptr.get())) { }
55 inline COMPtr(CreateTag, const IID& clsid) : m_ptr(createInstance(clsid)) { }
57 ~COMPtr() { if (m_ptr) m_ptr->Release(); }
59 T* get() const { return m_ptr; }
60 T* releaseRef() { T* tmp = m_ptr; m_ptr = 0; return tmp; }
62 T& operator*() const { return *m_ptr; }
63 T* operator->() const { return m_ptr; }
65 T** operator&() { ASSERT(!m_ptr); return &m_ptr; }
67 bool operator!() const { return !m_ptr; }
69 // This conversion operator allows implicit conversion to bool but not to other integer types.
70 typedef T* (COMPtr::*UnspecifiedBoolType)() const;
71 operator UnspecifiedBoolType() const { return m_ptr ? &COMPtr::get : 0; }
73 COMPtr& operator=(const COMPtr&);
74 COMPtr& operator=(T*);
75 template <typename U> COMPtr& operator=(const COMPtr<U>&);
77 void query(IUnknown* ptr) { adoptRef(copyQueryInterfaceRef(ptr)); }
78 template <typename U> inline void query(const COMPtr<U>& ptr) { query(ptr.get()); }
80 void create(const IID& clsid) { adoptRef(createInstance(clsid)); }
82 template <typename U> HRESULT copyRefTo(U**);
86 static T* copyQueryInterfaceRef(IUnknown*);
87 static T* createInstance(const IID& clsid);
92 template <typename T> inline T* COMPtr<T>::createInstance(const IID& clsid)
95 if (FAILED(CoCreateInstance(clsid, 0, CLSCTX_ALL, __uuidof(result), reinterpret_cast<void**>(&result))))
100 template <typename T> inline T* COMPtr<T>::copyQueryInterfaceRef(IUnknown* ptr)
105 if (FAILED(ptr->QueryInterface(&result)))
110 template <typename T> template <typename U> inline HRESULT COMPtr<T>::copyRefTo(U** ptr)
120 template <typename T> inline void COMPtr<T>::adoptRef(T *ptr)
127 template <typename T> inline COMPtr<T>& COMPtr<T>::operator=(const COMPtr<T>& o)
139 template <typename T> template <typename U> inline COMPtr<T>& COMPtr<T>::operator=(const COMPtr<U>& o)
151 template <typename T> inline COMPtr<T>& COMPtr<T>::operator=(T* optr)
162 template <typename T, typename U> inline bool operator==(const COMPtr<T>& a, const COMPtr<U>& b)
164 return a.get() == b.get();
167 template <typename T, typename U> inline bool operator==(const COMPtr<T>& a, U* b)
172 template <typename T, typename U> inline bool operator==(T* a, const COMPtr<U>& b)
177 template <typename T, typename U> inline bool operator!=(const COMPtr<T>& a, const COMPtr<U>& b)
179 return a.get() != b.get();
182 template <typename T, typename U> inline bool operator!=(const COMPtr<T>& a, U* b)
187 template <typename T, typename U> inline bool operator!=(T* a, const COMPtr<U>& b)
193 template<typename P> struct HashTraits<COMPtr<P> > : GenericHashTraits<COMPtr<P> > {
194 typedef HashTraits<typename IntTypes<sizeof(P*)>::SignedType> StorageTraits;
195 typedef typename StorageTraits::TraitType StorageType;
196 static const bool emptyValueIsZero = true;
197 static const bool needsRef = true;
204 static void ref(const StorageType& s)
206 if (const P* p = reinterpret_cast<const UnionType*>(&s)->m_p)
207 const_cast<P*>(p)->AddRef();
209 static void deref(const StorageType& s)
211 if (const P* p = reinterpret_cast<const UnionType*>(&s)->m_p)
212 const_cast<P*>(p)->Release();
216 template<typename P> struct HashKeyStorageTraits<PtrHash<COMPtr<P> >, HashTraits<COMPtr<P> > > {
217 typedef typename IntTypes<sizeof(P*)>::SignedType IntType;
218 typedef IntHash<IntType> Hash;
219 typedef HashTraits<IntType> Traits;
222 template<typename P> struct DefaultHash<COMPtr<P> > { typedef PtrHash<COMPtr<P> > Hash; };
224 template<typename P> struct PtrHash<COMPtr<P> > {
225 static unsigned hash(const COMPtr<P>& key) { return PtrHash<P*>::hash(key.get()); }
226 static bool equal(const COMPtr<P>& a, const COMPtr<P>& b) { return a == b; }