OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 #if WEBKIT_IMPLEMENTATION | 36 #if WEBKIT_IMPLEMENTATION |
37 #include <wtf/PassRefPtr.h> | 37 #include <wtf/PassRefPtr.h> |
38 #endif | 38 #endif |
39 | 39 |
40 namespace WebKit { | 40 namespace WebKit { |
41 | 41 |
42 // This class is an implementation detail of the WebKit API. It exists | 42 // This class is an implementation detail of the WebKit API. It exists |
43 // to help simplify the implementation of WebKit interfaces that merely | 43 // to help simplify the implementation of WebKit interfaces that merely |
44 // wrap a reference counted WebCore class. | 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 // |
45 template <typename T> | 75 template <typename T> |
46 class WebPrivatePtr { | 76 class WebPrivatePtr { |
47 public: | 77 public: |
48 WebPrivatePtr() : m_ptr(0) { } | 78 WebPrivatePtr() : m_ptr(0) { } |
49 ~WebPrivatePtr() { WEBKIT_ASSERT(!m_ptr); } | 79 ~WebPrivatePtr() { WEBKIT_ASSERT(!m_ptr); } |
50 | 80 |
51 bool isNull() const { return !m_ptr; } | 81 bool isNull() const { return !m_ptr; } |
52 | 82 |
53 #if WEBKIT_IMPLEMENTATION | 83 #if WEBKIT_IMPLEMENTATION |
54 WebPrivatePtr(const PassRefPtr<T>& prp) | 84 WebPrivatePtr(const PassRefPtr<T>& prp) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 | 120 |
91 private: | 121 private: |
92 #if WEBKIT_IMPLEMENTATION | 122 #if WEBKIT_IMPLEMENTATION |
93 void assign(T* p) | 123 void assign(T* p) |
94 { | 124 { |
95 // p is already ref'd for us by the caller | 125 // p is already ref'd for us by the caller |
96 if (m_ptr) | 126 if (m_ptr) |
97 m_ptr->deref(); | 127 m_ptr->deref(); |
98 m_ptr = p; | 128 m_ptr = p; |
99 } | 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); |
100 #endif | 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>&); |
101 | 140 |
102 T* m_ptr; | 141 T* m_ptr; |
103 }; | 142 }; |
104 | 143 |
105 } // namespace WebKit | 144 } // namespace WebKit |
106 | 145 |
107 #endif | 146 #endif |
OLD | NEW |