1 /* 2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. 3 * Copyright (C) 2008 Collabora Ltd. 4 * Copyright (C) 2010 Company 100, Inc. 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Library General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Library General Public License for more details. 15 * 16 * You should have received a copy of the GNU Library General Public License 17 * along with this library; see the file COPYING.LIB. If not, write to 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA. 20 * 21 */ 22 23 #ifndef OwnPtrBrew_h 24 #define OwnPtrBrew_h 25 26 #include <algorithm> 27 #include <wtf/Assertions.h> 28 #include <wtf/Noncopyable.h> 29 30 // Forward delcarations at this point avoid the need to include BREW includes 31 // in WTF headers. 32 typedef struct _IFileMgr IFileMgr; 33 typedef struct _IFile IFile; 34 typedef struct IBitmap IBitmap; 35 36 namespace WTF { 37 38 template <typename T> void freeOwnedPtrBrew(T* ptr); 39 template<> void freeOwnedPtrBrew<IFileMgr>(IFileMgr*); 40 template<> void freeOwnedPtrBrew<IFile>(IFile*); 41 template<> void freeOwnedPtrBrew<IBitmap>(IBitmap*); 42 43 template <typename T> class OwnPtrBrew : public Noncopyable { 44 public: 45 explicit OwnPtrBrew(T* ptr = 0) : m_ptr(ptr) { } 46 ~OwnPtrBrew() { freeOwnedPtrBrew(m_ptr); } 47 48 T* get() const { return m_ptr; } 49 T* release() 50 { 51 T* ptr = m_ptr; 52 m_ptr = 0; 53 return ptr; 54 } 55 56 T*& outPtr() 57 { 58 ASSERT(!m_ptr); 59 return m_ptr; 60 } 61 62 void set(T* ptr) 63 { 64 ASSERT(!ptr || m_ptr != ptr); 65 freeOwnedPtrBrew(m_ptr); 66 m_ptr = ptr; 67 } 68 69 void clear() 70 { 71 freeOwnedPtrBrew(m_ptr); 72 m_ptr = 0; 73 } 74 75 T& operator*() const 76 { 77 ASSERT(m_ptr); 78 return *m_ptr; 79 } 80 81 T* operator->() const 82 { 83 ASSERT(m_ptr); 84 return m_ptr; 85 } 86 87 bool operator!() const { return !m_ptr; } 88 89 // This conversion operator allows implicit conversion to bool but not to other integer types. 90 typedef T* OwnPtrBrew::*UnspecifiedBoolType; 91 operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtrBrew::m_ptr : 0; } 92 93 void swap(OwnPtrBrew& o) { std::swap(m_ptr, o.m_ptr); } 94 95 private: 96 T* m_ptr; 97 }; 98 99 template <typename T> inline void swap(OwnPtrBrew<T>& a, OwnPtrBrew<T>& b) 100 { 101 a.swap(b); 102 } 103 104 template <typename T, typename U> inline bool operator==(const OwnPtrBrew<T>& a, U* b) 105 { 106 return a.get() == b; 107 } 108 109 template <typename T, typename U> inline bool operator==(T* a, const OwnPtrBrew<U>& b) 110 { 111 return a == b.get(); 112 } 113 114 template <typename T, typename U> inline bool operator!=(const OwnPtrBrew<T>& a, U* b) 115 { 116 return a.get() != b; 117 } 118 119 template <typename T, typename U> inline bool operator!=(T* a, const OwnPtrBrew<U>& b) 120 { 121 return a != b.get(); 122 } 123 124 template <typename T> inline typename OwnPtrBrew<T>::PtrType getPtr(const OwnPtrBrew<T>& p) 125 { 126 return p.get(); 127 } 128 129 } // namespace WTF 130 131 using WTF::OwnPtrBrew; 132 133 #endif // OwnPtrBrew_h 134