| OLD | NEW |
| 1 /* | 1 #include "../common/WebPrivatePtr.h" |
| 2 * Copyright (C) 2010 Google 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 are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #ifndef WebPrivatePtr_h | |
| 32 #define WebPrivatePtr_h | |
| 33 | |
| 34 #include "WebCommon.h" | |
| 35 | |
| 36 #if WEBKIT_IMPLEMENTATION | |
| 37 #include <wtf/PassRefPtr.h> | |
| 38 #endif | |
| 39 | |
| 40 namespace WebKit { | |
| 41 | |
| 42 // This class is an implementation detail of the WebKit API. It exists | |
| 43 // to help simplify the implementation of WebKit interfaces that merely | |
| 44 // wrap a reference counted WebCore class. | |
| 45 // | |
| 46 // A typical implementation of a class which uses WebPrivatePtr might look like | |
| 47 // this: | |
| 48 // class WebFoo { | |
| 49 // public: | |
| 50 // virtual ~WebFoo() { } // Only necessary if WebFoo will be used as a | |
| 51 // // base class. | |
| 52 // WebFoo() { } | |
| 53 // WebFoo(const WebFoo& other) { assign(other); } | |
| 54 // WebFoo& operator=(const WebFoo& other) | |
| 55 // { | |
| 56 // assign(other); | |
| 57 // return *this; | |
| 58 // } | |
| 59 // WEBKIT_EXPORT void assign(const WebFoo&); // Implemented in the body. | |
| 60 // | |
| 61 // // Methods that are exposed to Chromium and which are specific to | |
| 62 // // WebFoo go here. | |
| 63 // WEBKIT_EXPORT doWebFooThing(); | |
| 64 // | |
| 65 // // Methods that are used only by other WebKit/chromium API classes | |
| 66 // // should only be declared when WEBKIT_IMPLEMENTATION is set. | |
| 67 // #if WEBKIT_IMPLEMENTATION | |
| 68 // WebFoo(const WTF::PassRefPtr<WebCore::Foo>&); | |
| 69 // #endif | |
| 70 // | |
| 71 // private: | |
| 72 // WebPrivatePtr<WebCore::Foo> m_private; | |
| 73 // }; | |
| 74 // | |
| 75 template <typename T> | |
| 76 class WebPrivatePtr { | |
| 77 public: | |
| 78 WebPrivatePtr() : m_ptr(0) { } | |
| 79 ~WebPrivatePtr() { WEBKIT_ASSERT(!m_ptr); } | |
| 80 | |
| 81 bool isNull() const { return !m_ptr; } | |
| 82 | |
| 83 #if WEBKIT_IMPLEMENTATION | |
| 84 WebPrivatePtr(const PassRefPtr<T>& prp) | |
| 85 : m_ptr(prp.leakRef()) | |
| 86 { | |
| 87 } | |
| 88 | |
| 89 void reset() | |
| 90 { | |
| 91 assign(0); | |
| 92 } | |
| 93 | |
| 94 WebPrivatePtr<T>& operator=(const WebPrivatePtr<T>& other) | |
| 95 { | |
| 96 T* p = other.m_ptr; | |
| 97 if (p) | |
| 98 p->ref(); | |
| 99 assign(p); | |
| 100 return *this; | |
| 101 } | |
| 102 | |
| 103 WebPrivatePtr<T>& operator=(const PassRefPtr<T>& prp) | |
| 104 { | |
| 105 assign(prp.leakRef()); | |
| 106 return *this; | |
| 107 } | |
| 108 | |
| 109 T* get() const | |
| 110 { | |
| 111 return m_ptr; | |
| 112 } | |
| 113 | |
| 114 T* operator->() const | |
| 115 { | |
| 116 ASSERT(m_ptr); | |
| 117 return m_ptr; | |
| 118 } | |
| 119 #endif | |
| 120 | |
| 121 private: | |
| 122 #if WEBKIT_IMPLEMENTATION | |
| 123 void assign(T* p) | |
| 124 { | |
| 125 // p is already ref'd for us by the caller | |
| 126 if (m_ptr) | |
| 127 m_ptr->deref(); | |
| 128 m_ptr = p; | |
| 129 } | |
| 130 #else | |
| 131 // Disable the assignment operator; we define it above for when | |
| 132 // WEBKIT_IMPLEMENTATION is set, but we need to make sure that it is not | |
| 133 // used outside there; the compiler-provided version won't handle reference | |
| 134 // counting properly. | |
| 135 WebPrivatePtr<T>& operator=(const WebPrivatePtr<T>& other); | |
| 136 #endif | |
| 137 // Disable the copy constructor; classes that contain a WebPrivatePtr | |
| 138 // should implement their copy constructor using assign(). | |
| 139 WebPrivatePtr(const WebPrivatePtr<T>&); | |
| 140 | |
| 141 T* m_ptr; | |
| 142 }; | |
| 143 | |
| 144 } // namespace WebKit | |
| 145 | |
| 146 #endif | |
| OLD | NEW |