]> git.sesse.net Git - casparcg/blob - dependencies64/cef/windows/include/internal/cef_ptr.h
7883c98cb88b3a681eb2c7975156ca0f0375d2f1
[casparcg] / dependencies64 / cef / windows / include / internal / cef_ptr.h
1 // Copyright (c) 2014 Marshall A. Greenblatt. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //    * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //    * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //    * Neither the name of Google Inc. nor the name Chromium Embedded
14 // Framework nor the names of its contributors may be used to endorse
15 // or promote products derived from this software without specific prior
16 // 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 CEF_INCLUDE_INTERNAL_CEF_PTR_H_
32 #define CEF_INCLUDE_INTERNAL_CEF_PTR_H_
33 #pragma once
34
35 #include "include/base/cef_build.h"
36 #include "include/base/cef_ref_counted.h"
37
38 #if defined(USING_CHROMIUM_INCLUDES)
39 #include <memory>  // For std::unique_ptr.
40 #else
41 #include "include/base/cef_scoped_ptr.h"
42 #endif
43
44 ///
45 // Smart pointer implementation that is an alias of scoped_refptr from
46 // include/base/cef_ref_counted.h.
47 // <p>
48 // A smart pointer class for reference counted objects.  Use this class instead
49 // of calling AddRef and Release manually on a reference counted object to
50 // avoid common memory leaks caused by forgetting to Release an object
51 // reference.  Sample usage:
52 // <pre>
53 //   class MyFoo : public CefBaseRefCounted {
54 //    ...
55 //   };
56 //
57 //   void some_function() {
58 //     // The MyFoo object that |foo| represents starts with a single
59 //     // reference.
60 //     CefRefPtr&lt;MyFoo&gt; foo = new MyFoo();
61 //     foo-&gt;Method(param);
62 //     // |foo| is released when this function returns
63 //   }
64 //
65 //   void some_other_function() {
66 //     CefRefPtr&lt;MyFoo&gt; foo = new MyFoo();
67 //     ...
68 //     foo = NULL;  // explicitly releases |foo|
69 //     ...
70 //     if (foo)
71 //       foo-&gt;Method(param);
72 //   }
73 // </pre>
74 // The above examples show how CefRefPtr&lt;T&gt; acts like a pointer to T.
75 // Given two CefRefPtr&lt;T&gt; classes, it is also possible to exchange
76 // references between the two objects, like so:
77 // <pre>
78 //   {
79 //     CefRefPtr&lt;MyFoo&gt; a = new MyFoo();
80 //     CefRefPtr&lt;MyFoo&gt; b;
81 //
82 //     b.swap(a);
83 //     // now, |b| references the MyFoo object, and |a| references NULL.
84 //   }
85 // </pre>
86 // To make both |a| and |b| in the above example reference the same MyFoo
87 // object, simply use the assignment operator:
88 // <pre>
89 //   {
90 //     CefRefPtr&lt;MyFoo&gt; a = new MyFoo();
91 //     CefRefPtr&lt;MyFoo&gt; b;
92 //
93 //     b = a;
94 //     // now, |a| and |b| each own a reference to the same MyFoo object.
95 //     // the reference count of the underlying MyFoo object will be 2.
96 //   }
97 // </pre>
98 // Reference counted objects can also be passed as function parameters and
99 // used as function return values:
100 // <pre>
101 //   void some_func_with_param(CefRefPtr&lt;MyFoo&gt; param) {
102 //     // A reference is added to the MyFoo object that |param| represents
103 //     // during the scope of some_func_with_param() and released when
104 //     // some_func_with_param() goes out of scope.
105 //   }
106 //
107 //   CefRefPtr&lt;MyFoo&gt; some_func_with_retval() {
108 //     // The MyFoo object that |foox| represents starts with a single
109 //     // reference.
110 //     CefRefPtr&lt;MyFoo&gt; foox = new MyFoo();
111 //
112 //     // Creating the return value adds an additional reference.
113 //     return foox;
114 //
115 //     // When some_func_with_retval() goes out of scope the original |foox|
116 //     // reference is released.
117 //   }
118 //
119 //   void and_another_function() {
120 //     CefRefPtr&lt;MyFoo&gt; foo = new MyFoo();
121 //
122 //     // pass |foo| as a parameter.
123 //     some_function(foo);
124 //
125 //     CefRefPtr&lt;MyFoo&gt; foo2 = some_func_with_retval();
126 //     // Now, since we kept a reference to the some_func_with_retval() return
127 //     // value, |foo2| is the only class pointing to the MyFoo object created
128 //     in some_func_with_retval(), and it has a reference count of 1.
129 //
130 //     some_func_with_retval();
131 //     // Now, since we didn't keep a reference to the some_func_with_retval()
132 //     // return value, the MyFoo object created in some_func_with_retval()
133 //     // will automatically be released.
134 //   }
135 // </pre>
136 // And in standard containers:
137 // <pre>
138 //   {
139 //      // Create a vector that holds MyFoo objects.
140 //      std::vector&lt;CefRefPtr&lt;MyFoo&gt; &gt; MyFooVec;
141 //
142 //     // The MyFoo object that |foo| represents starts with a single
143 //     // reference.
144 //     CefRefPtr&lt;MyFoo&gt; foo = new MyFoo();
145 //
146 //     // When the MyFoo object is added to |MyFooVec| the reference count
147 //     // is increased to 2.
148 //     MyFooVec.push_back(foo);
149 //   }
150 // </pre>
151 // </p>
152 ///
153 #if defined(HAS_CPP11_TEMPLATE_ALIAS_SUPPORT)
154 template <class T>
155 using CefRefPtr = scoped_refptr<T>;
156 #else
157 // When template aliases are not supported use a define instead of subclassing
158 // because it's otherwise hard to get the constructors to behave correctly.
159 #define CefRefPtr scoped_refptr
160 #endif
161
162
163 ///
164 // A CefOwnPtr<T> is like a T*, except that the destructor of CefOwnPtr<T>
165 // automatically deletes the pointer it holds (if any). That is, CefOwnPtr<T>
166 // owns the T object that it points to. Like a T*, a CefOwnPtr<T> may hold
167 // either NULL or a pointer to a T object. Also like T*, CefOwnPtr<T> is
168 // thread-compatible, and once you dereference it, you get the thread safety
169 // guarantees of T.
170 ///
171 #if defined(USING_CHROMIUM_INCLUDES)
172 // Implementation-side code uses std::unique_ptr instead of scoped_ptr.
173 template <class T, class D = std::default_delete<T>>
174 using CefOwnPtr = std::unique_ptr<T, D>;
175 #elif defined(HAS_CPP11_TEMPLATE_ALIAS_SUPPORT)
176 template <class T, class D = base::DefaultDeleter<T>>
177 using CefOwnPtr = scoped_ptr<T, D>;
178 #else
179 // When template aliases are not supported use a define instead of subclassing
180 // because it's otherwise hard to get the constructors to behave correctly.
181 #define CefOwnPtr scoped_ptr
182 #endif
183
184
185 ///
186 // A CefRawPtr<T> is the same as T*
187 ///
188 #if defined(HAS_CPP11_TEMPLATE_ALIAS_SUPPORT)
189 #define CEF_RAW_PTR_GET(r) r
190 template <class T>
191 using CefRawPtr = T*;
192 #else
193 // Simple wrapper implementation that behaves as much like T* as possible.
194 // CEF_RAW_PTR_GET is required for VS2008 compatibility (Issue #2155).
195 #define CEF_RAW_PTR_GET(r) r.get()
196 template <class T>
197 class CefRawPtr {
198  public:
199   CefRawPtr() : ptr_(nullptr) {}
200   CefRawPtr(T* p) : ptr_(p) {}
201   CefRawPtr(const CefRawPtr& r) : ptr_(r.ptr_) {}
202
203   template <typename U>
204   CefRawPtr(const CefRawPtr<U>& r) : ptr_(r.get()) {}
205
206   T* get() const { return ptr_; }
207
208   // Allow CefRawPtr to be used in boolean expression and comparison operations.
209   operator T*() const { return ptr_; }
210
211   T* operator->() const {
212     assert(ptr_ != NULL);
213     return ptr_;
214   }
215
216   CefRawPtr<T>& operator=(T* p) {
217     ptr_ = p;
218     return *this;
219   }
220
221   CefRawPtr<T>& operator=(const CefRawPtr<T>& r) {
222     return *this = r.ptr_;
223   }
224
225   template <typename U>
226   CefRawPtr<T>& operator=(const CefRawPtr<U>& r) {
227     return *this = r.get();
228   }
229
230  private:
231   T* ptr_;
232 };
233 #endif
234
235 #endif  // CEF_INCLUDE_INTERNAL_CEF_PTR_H_