]> git.sesse.net Git - casparcg/blob - dependencies64/cef/include/base/cef_scoped_ptr.h
* Merged html producer and updated to latest CEF version (does not have satisfactory...
[casparcg] / dependencies64 / cef / include / base / cef_scoped_ptr.h
1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2012
2 // 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 name Chromium Embedded
15 // Framework nor the names of its contributors may be used to endorse
16 // or promote products derived from this software without specific prior
17 // written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // Scopers help you manage ownership of a pointer, helping you easily manage a
32 // pointer within a scope, and automatically destroying the pointer at the end
33 // of a scope.  There are two main classes you will use, which correspond to the
34 // operators new/delete and new[]/delete[].
35 //
36 // Example usage (scoped_ptr<T>):
37 //   {
38 //     scoped_ptr<Foo> foo(new Foo("wee"));
39 //   }  // foo goes out of scope, releasing the pointer with it.
40 //
41 //   {
42 //     scoped_ptr<Foo> foo;          // No pointer managed.
43 //     foo.reset(new Foo("wee"));    // Now a pointer is managed.
44 //     foo.reset(new Foo("wee2"));   // Foo("wee") was destroyed.
45 //     foo.reset(new Foo("wee3"));   // Foo("wee2") was destroyed.
46 //     foo->Method();                // Foo::Method() called.
47 //     foo.get()->Method();          // Foo::Method() called.
48 //     SomeFunc(foo.release());      // SomeFunc takes ownership, foo no longer
49 //                                   // manages a pointer.
50 //     foo.reset(new Foo("wee4"));   // foo manages a pointer again.
51 //     foo.reset();                  // Foo("wee4") destroyed, foo no longer
52 //                                   // manages a pointer.
53 //   }  // foo wasn't managing a pointer, so nothing was destroyed.
54 //
55 // Example usage (scoped_ptr<T[]>):
56 //   {
57 //     scoped_ptr<Foo[]> foo(new Foo[100]);
58 //     foo.get()->Method();  // Foo::Method on the 0th element.
59 //     foo[10].Method();     // Foo::Method on the 10th element.
60 //   }
61 //
62 // These scopers also implement part of the functionality of C++11 unique_ptr
63 // in that they are "movable but not copyable."  You can use the scopers in
64 // the parameter and return types of functions to signify ownership transfer
65 // in to and out of a function.  When calling a function that has a scoper
66 // as the argument type, it must be called with the result of an analogous
67 // scoper's Pass() function or another function that generates a temporary;
68 // passing by copy will NOT work.  Here is an example using scoped_ptr:
69 //
70 //   void TakesOwnership(scoped_ptr<Foo> arg) {
71 //     // Do something with arg
72 //   }
73 //   scoped_ptr<Foo> CreateFoo() {
74 //     // No need for calling Pass() because we are constructing a temporary
75 //     // for the return value.
76 //     return scoped_ptr<Foo>(new Foo("new"));
77 //   }
78 //   scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) {
79 //     return arg.Pass();
80 //   }
81 //
82 //   {
83 //     scoped_ptr<Foo> ptr(new Foo("yay"));  // ptr manages Foo("yay").
84 //     TakesOwnership(ptr.Pass());           // ptr no longer owns Foo("yay").
85 //     scoped_ptr<Foo> ptr2 = CreateFoo();   // ptr2 owns the return Foo.
86 //     scoped_ptr<Foo> ptr3 =                // ptr3 now owns what was in ptr2.
87 //         PassThru(ptr2.Pass());            // ptr2 is correspondingly NULL.
88 //   }
89 //
90 // Notice that if you do not call Pass() when returning from PassThru(), or
91 // when invoking TakesOwnership(), the code will not compile because scopers
92 // are not copyable; they only implement move semantics which require calling
93 // the Pass() function to signify a destructive transfer of state. CreateFoo()
94 // is different though because we are constructing a temporary on the return
95 // line and thus can avoid needing to call Pass().
96 //
97 // Pass() properly handles upcast in initialization, i.e. you can use a
98 // scoped_ptr<Child> to initialize a scoped_ptr<Parent>:
99 //
100 //   scoped_ptr<Foo> foo(new Foo());
101 //   scoped_ptr<FooParent> parent(foo.Pass());
102 //
103 // PassAs<>() should be used to upcast return value in return statement:
104 //
105 //   scoped_ptr<Foo> CreateFoo() {
106 //     scoped_ptr<FooChild> result(new FooChild());
107 //     return result.PassAs<Foo>();
108 //   }
109 //
110 // Note that PassAs<>() is implemented only for scoped_ptr<T>, but not for
111 // scoped_ptr<T[]>. This is because casting array pointers may not be safe.
112
113 #ifndef CEF_INCLUDE_BASE_CEF_MEMORY_SCOPED_PTR_H_
114 #define CEF_INCLUDE_BASE_CEF_MEMORY_SCOPED_PTR_H_
115 #pragma once
116
117 #if defined(BASE_MEMORY_SCOPED_PTR_H_)
118 // Do nothing if the Chromium header has already been included.
119 // This can happen in cases where Chromium code is used directly by the
120 // client application. When using Chromium code directly always include
121 // the Chromium header first to avoid type conflicts.
122 #elif defined(BUILDING_CEF_SHARED)
123 // When building CEF include the Chromium header directly.
124 #include "base/memory/scoped_ptr.h"
125 #else  // !BUILDING_CEF_SHARED
126 // The following is substantially similar to the Chromium implementation.
127 // If the Chromium implementation diverges the below implementation should be
128 // updated to match.
129
130 // This is an implementation designed to match the anticipated future TR2
131 // implementation of the scoped_ptr class.
132
133 #include <assert.h>
134 #include <stddef.h>
135 #include <stdlib.h>
136
137 #include <algorithm>  // For std::swap().
138
139 #include "include/base/cef_basictypes.h"
140 #include "include/base/cef_build.h"
141 #include "include/base/cef_macros.h"
142 #include "include/base/cef_move.h"
143 #include "include/base/cef_template_util.h"
144
145 namespace base {
146
147 namespace subtle {
148 class RefCountedBase;
149 class RefCountedThreadSafeBase;
150 }  // namespace subtle
151
152 // Function object which deletes its parameter, which must be a pointer.
153 // If C is an array type, invokes 'delete[]' on the parameter; otherwise,
154 // invokes 'delete'. The default deleter for scoped_ptr<T>.
155 template <class T>
156 struct DefaultDeleter {
157   DefaultDeleter() {}
158   template <typename U> DefaultDeleter(const DefaultDeleter<U>& other) {
159     // IMPLEMENTATION NOTE: C++11 20.7.1.1.2p2 only provides this constructor
160     // if U* is implicitly convertible to T* and U is not an array type.
161     //
162     // Correct implementation should use SFINAE to disable this
163     // constructor. However, since there are no other 1-argument constructors,
164     // using a COMPILE_ASSERT() based on is_convertible<> and requiring
165     // complete types is simpler and will cause compile failures for equivalent
166     // misuses.
167     //
168     // Note, the is_convertible<U*, T*> check also ensures that U is not an
169     // array. T is guaranteed to be a non-array, so any U* where U is an array
170     // cannot convert to T*.
171     enum { T_must_be_complete = sizeof(T) };
172     enum { U_must_be_complete = sizeof(U) };
173     COMPILE_ASSERT((base::is_convertible<U*, T*>::value),
174                    U_ptr_must_implicitly_convert_to_T_ptr);
175   }
176   inline void operator()(T* ptr) const {
177     enum { type_must_be_complete = sizeof(T) };
178     delete ptr;
179   }
180 };
181
182 // Specialization of DefaultDeleter for array types.
183 template <class T>
184 struct DefaultDeleter<T[]> {
185   inline void operator()(T* ptr) const {
186     enum { type_must_be_complete = sizeof(T) };
187     delete[] ptr;
188   }
189
190  private:
191   // Disable this operator for any U != T because it is undefined to execute
192   // an array delete when the static type of the array mismatches the dynamic
193   // type.
194   //
195   // References:
196   //   C++98 [expr.delete]p3
197   //   http://cplusplus.github.com/LWG/lwg-defects.html#938
198   template <typename U> void operator()(U* array) const;
199 };
200
201 template <class T, int n>
202 struct DefaultDeleter<T[n]> {
203   // Never allow someone to declare something like scoped_ptr<int[10]>.
204   COMPILE_ASSERT(sizeof(T) == -1, do_not_use_array_with_size_as_type);
205 };
206
207 // Function object which invokes 'free' on its parameter, which must be
208 // a pointer. Can be used to store malloc-allocated pointers in scoped_ptr:
209 //
210 // scoped_ptr<int, base::FreeDeleter> foo_ptr(
211 //     static_cast<int*>(malloc(sizeof(int))));
212 struct FreeDeleter {
213   inline void operator()(void* ptr) const {
214     free(ptr);
215   }
216 };
217
218 namespace cef_internal {
219
220 template <typename T> struct IsNotRefCounted {
221   enum {
222     value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value &&
223         !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>::
224             value
225   };
226 };
227
228 // Minimal implementation of the core logic of scoped_ptr, suitable for
229 // reuse in both scoped_ptr and its specializations.
230 template <class T, class D>
231 class scoped_ptr_impl {
232  public:
233   explicit scoped_ptr_impl(T* p) : data_(p) { }
234
235   // Initializer for deleters that have data parameters.
236   scoped_ptr_impl(T* p, const D& d) : data_(p, d) {}
237
238   // Templated constructor that destructively takes the value from another
239   // scoped_ptr_impl.
240   template <typename U, typename V>
241   scoped_ptr_impl(scoped_ptr_impl<U, V>* other)
242       : data_(other->release(), other->get_deleter()) {
243     // We do not support move-only deleters.  We could modify our move
244     // emulation to have base::subtle::move() and base::subtle::forward()
245     // functions that are imperfect emulations of their C++11 equivalents,
246     // but until there's a requirement, just assume deleters are copyable.
247   }
248
249   template <typename U, typename V>
250   void TakeState(scoped_ptr_impl<U, V>* other) {
251     // See comment in templated constructor above regarding lack of support
252     // for move-only deleters.
253     reset(other->release());
254     get_deleter() = other->get_deleter();
255   }
256
257   ~scoped_ptr_impl() {
258     if (data_.ptr != NULL) {
259       // Not using get_deleter() saves one function call in non-optimized
260       // builds.
261       static_cast<D&>(data_)(data_.ptr);
262     }
263   }
264
265   void reset(T* p) {
266     // This is a self-reset, which is no longer allowed: http://crbug.com/162971
267     if (p != NULL && p == data_.ptr)
268       abort();
269
270     // Note that running data_.ptr = p can lead to undefined behavior if
271     // get_deleter()(get()) deletes this. In order to prevent this, reset()
272     // should update the stored pointer before deleting its old value.
273     //
274     // However, changing reset() to use that behavior may cause current code to
275     // break in unexpected ways. If the destruction of the owned object
276     // dereferences the scoped_ptr when it is destroyed by a call to reset(),
277     // then it will incorrectly dispatch calls to |p| rather than the original
278     // value of |data_.ptr|.
279     //
280     // During the transition period, set the stored pointer to NULL while
281     // deleting the object. Eventually, this safety check will be removed to
282     // prevent the scenario initially described from occuring and
283     // http://crbug.com/176091 can be closed.
284     T* old = data_.ptr;
285     data_.ptr = NULL;
286     if (old != NULL)
287       static_cast<D&>(data_)(old);
288     data_.ptr = p;
289   }
290
291   T* get() const { return data_.ptr; }
292
293   D& get_deleter() { return data_; }
294   const D& get_deleter() const { return data_; }
295
296   void swap(scoped_ptr_impl& p2) {
297     // Standard swap idiom: 'using std::swap' ensures that std::swap is
298     // present in the overload set, but we call swap unqualified so that
299     // any more-specific overloads can be used, if available.
300     using std::swap;
301     swap(static_cast<D&>(data_), static_cast<D&>(p2.data_));
302     swap(data_.ptr, p2.data_.ptr);
303   }
304
305   T* release() {
306     T* old_ptr = data_.ptr;
307     data_.ptr = NULL;
308     return old_ptr;
309   }
310
311  private:
312   // Needed to allow type-converting constructor.
313   template <typename U, typename V> friend class scoped_ptr_impl;
314
315   // Use the empty base class optimization to allow us to have a D
316   // member, while avoiding any space overhead for it when D is an
317   // empty class.  See e.g. http://www.cantrip.org/emptyopt.html for a good
318   // discussion of this technique.
319   struct Data : public D {
320     explicit Data(T* ptr_in) : ptr(ptr_in) {}
321     Data(T* ptr_in, const D& other) : D(other), ptr(ptr_in) {}
322     T* ptr;
323   };
324
325   Data data_;
326
327   DISALLOW_COPY_AND_ASSIGN(scoped_ptr_impl);
328 };
329
330 }  // namespace cef_internal
331
332 }  // namespace base
333
334 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
335 // automatically deletes the pointer it holds (if any).
336 // That is, scoped_ptr<T> owns the T object that it points to.
337 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
338 // Also like T*, scoped_ptr<T> is thread-compatible, and once you
339 // dereference it, you get the thread safety guarantees of T.
340 //
341 // The size of scoped_ptr is small. On most compilers, when using the
342 // DefaultDeleter, sizeof(scoped_ptr<T>) == sizeof(T*). Custom deleters will
343 // increase the size proportional to whatever state they need to have. See
344 // comments inside scoped_ptr_impl<> for details.
345 //
346 // Current implementation targets having a strict subset of  C++11's
347 // unique_ptr<> features. Known deficiencies include not supporting move-only
348 // deleteres, function pointers as deleters, and deleters with reference
349 // types.
350 template <class T, class D = base::DefaultDeleter<T> >
351 class scoped_ptr {
352   MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue)
353
354   COMPILE_ASSERT(base::cef_internal::IsNotRefCounted<T>::value,
355                  T_is_refcounted_type_and_needs_scoped_refptr);
356
357  public:
358   // The element and deleter types.
359   typedef T element_type;
360   typedef D deleter_type;
361
362   // Constructor.  Defaults to initializing with NULL.
363   scoped_ptr() : impl_(NULL) { }
364
365   // Constructor.  Takes ownership of p.
366   explicit scoped_ptr(element_type* p) : impl_(p) { }
367
368   // Constructor.  Allows initialization of a stateful deleter.
369   scoped_ptr(element_type* p, const D& d) : impl_(p, d) { }
370
371   // Constructor.  Allows construction from a scoped_ptr rvalue for a
372   // convertible type and deleter.
373   //
374   // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this constructor distinct
375   // from the normal move constructor. By C++11 20.7.1.2.1.21, this constructor
376   // has different post-conditions if D is a reference type. Since this
377   // implementation does not support deleters with reference type,
378   // we do not need a separate move constructor allowing us to avoid one
379   // use of SFINAE. You only need to care about this if you modify the
380   // implementation of scoped_ptr.
381   template <typename U, typename V>
382   scoped_ptr(scoped_ptr<U, V> other) : impl_(&other.impl_) {
383     COMPILE_ASSERT(!base::is_array<U>::value, U_cannot_be_an_array);
384   }
385
386   // Constructor.  Move constructor for C++03 move emulation of this type.
387   scoped_ptr(RValue rvalue) : impl_(&rvalue.object->impl_) { }
388
389   // operator=.  Allows assignment from a scoped_ptr rvalue for a convertible
390   // type and deleter.
391   //
392   // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this operator= distinct from
393   // the normal move assignment operator. By C++11 20.7.1.2.3.4, this templated
394   // form has different requirements on for move-only Deleters. Since this
395   // implementation does not support move-only Deleters, we do not need a
396   // separate move assignment operator allowing us to avoid one use of SFINAE.
397   // You only need to care about this if you modify the implementation of
398   // scoped_ptr.
399   template <typename U, typename V>
400   scoped_ptr& operator=(scoped_ptr<U, V> rhs) {
401     COMPILE_ASSERT(!base::is_array<U>::value, U_cannot_be_an_array);
402     impl_.TakeState(&rhs.impl_);
403     return *this;
404   }
405
406   // Reset.  Deletes the currently owned object, if any.
407   // Then takes ownership of a new object, if given.
408   void reset(element_type* p = NULL) { impl_.reset(p); }
409
410   // Accessors to get the owned object.
411   // operator* and operator-> will assert() if there is no current object.
412   element_type& operator*() const {
413     assert(impl_.get() != NULL);
414     return *impl_.get();
415   }
416   element_type* operator->() const  {
417     assert(impl_.get() != NULL);
418     return impl_.get();
419   }
420   element_type* get() const { return impl_.get(); }
421
422   // Access to the deleter.
423   deleter_type& get_deleter() { return impl_.get_deleter(); }
424   const deleter_type& get_deleter() const { return impl_.get_deleter(); }
425
426   // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
427   // implicitly convertible to a real bool (which is dangerous).
428   //
429   // Note that this trick is only safe when the == and != operators
430   // are declared explicitly, as otherwise "scoped_ptr1 ==
431   // scoped_ptr2" will compile but do the wrong thing (i.e., convert
432   // to Testable and then do the comparison).
433  private:
434   typedef base::cef_internal::scoped_ptr_impl<element_type, deleter_type>
435       scoped_ptr::*Testable;
436
437  public:
438   operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; }
439
440   // Comparison operators.
441   // These return whether two scoped_ptr refer to the same object, not just to
442   // two different but equal objects.
443   bool operator==(const element_type* p) const { return impl_.get() == p; }
444   bool operator!=(const element_type* p) const { return impl_.get() != p; }
445
446   // Swap two scoped pointers.
447   void swap(scoped_ptr& p2) {
448     impl_.swap(p2.impl_);
449   }
450
451   // Release a pointer.
452   // The return value is the current pointer held by this object.
453   // If this object holds a NULL pointer, the return value is NULL.
454   // After this operation, this object will hold a NULL pointer,
455   // and will not own the object any more.
456   element_type* release() WARN_UNUSED_RESULT {
457     return impl_.release();
458   }
459
460   // C++98 doesn't support functions templates with default parameters which
461   // makes it hard to write a PassAs() that understands converting the deleter
462   // while preserving simple calling semantics.
463   //
464   // Until there is a use case for PassAs() with custom deleters, just ignore
465   // the custom deleter.
466   template <typename PassAsType>
467   scoped_ptr<PassAsType> PassAs() {
468     return scoped_ptr<PassAsType>(Pass());
469   }
470
471  private:
472   // Needed to reach into |impl_| in the constructor.
473   template <typename U, typename V> friend class scoped_ptr;
474   base::cef_internal::scoped_ptr_impl<element_type, deleter_type> impl_;
475
476   // Forbidden for API compatibility with std::unique_ptr.
477   explicit scoped_ptr(int disallow_construction_from_null);
478
479   // Forbid comparison of scoped_ptr types.  If U != T, it totally
480   // doesn't make sense, and if U == T, it still doesn't make sense
481   // because you should never have the same object owned by two different
482   // scoped_ptrs.
483   template <class U> bool operator==(scoped_ptr<U> const& p2) const;
484   template <class U> bool operator!=(scoped_ptr<U> const& p2) const;
485 };
486
487 template <class T, class D>
488 class scoped_ptr<T[], D> {
489   MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue)
490
491  public:
492   // The element and deleter types.
493   typedef T element_type;
494   typedef D deleter_type;
495
496   // Constructor.  Defaults to initializing with NULL.
497   scoped_ptr() : impl_(NULL) { }
498
499   // Constructor. Stores the given array. Note that the argument's type
500   // must exactly match T*. In particular:
501   // - it cannot be a pointer to a type derived from T, because it is
502   //   inherently unsafe in the general case to access an array through a
503   //   pointer whose dynamic type does not match its static type (eg., if
504   //   T and the derived types had different sizes access would be
505   //   incorrectly calculated). Deletion is also always undefined
506   //   (C++98 [expr.delete]p3). If you're doing this, fix your code.
507   // - it cannot be NULL, because NULL is an integral expression, not a
508   //   pointer to T. Use the no-argument version instead of explicitly
509   //   passing NULL.
510   // - it cannot be const-qualified differently from T per unique_ptr spec
511   //   (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting
512   //   to work around this may use implicit_cast<const T*>().
513   //   However, because of the first bullet in this comment, users MUST
514   //   NOT use implicit_cast<Base*>() to upcast the static type of the array.
515   explicit scoped_ptr(element_type* array) : impl_(array) { }
516
517   // Constructor.  Move constructor for C++03 move emulation of this type.
518   scoped_ptr(RValue rvalue) : impl_(&rvalue.object->impl_) { }
519
520   // operator=.  Move operator= for C++03 move emulation of this type.
521   scoped_ptr& operator=(RValue rhs) {
522     impl_.TakeState(&rhs.object->impl_);
523     return *this;
524   }
525
526   // Reset.  Deletes the currently owned array, if any.
527   // Then takes ownership of a new object, if given.
528   void reset(element_type* array = NULL) { impl_.reset(array); }
529
530   // Accessors to get the owned array.
531   element_type& operator[](size_t i) const {
532     assert(impl_.get() != NULL);
533     return impl_.get()[i];
534   }
535   element_type* get() const { return impl_.get(); }
536
537   // Access to the deleter.
538   deleter_type& get_deleter() { return impl_.get_deleter(); }
539   const deleter_type& get_deleter() const { return impl_.get_deleter(); }
540
541   // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
542   // implicitly convertible to a real bool (which is dangerous).
543  private:
544   typedef base::cef_internal::scoped_ptr_impl<element_type, deleter_type>
545       scoped_ptr::*Testable;
546
547  public:
548   operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; }
549
550   // Comparison operators.
551   // These return whether two scoped_ptr refer to the same object, not just to
552   // two different but equal objects.
553   bool operator==(element_type* array) const { return impl_.get() == array; }
554   bool operator!=(element_type* array) const { return impl_.get() != array; }
555
556   // Swap two scoped pointers.
557   void swap(scoped_ptr& p2) {
558     impl_.swap(p2.impl_);
559   }
560
561   // Release a pointer.
562   // The return value is the current pointer held by this object.
563   // If this object holds a NULL pointer, the return value is NULL.
564   // After this operation, this object will hold a NULL pointer,
565   // and will not own the object any more.
566   element_type* release() WARN_UNUSED_RESULT {
567     return impl_.release();
568   }
569
570  private:
571   // Force element_type to be a complete type.
572   enum { type_must_be_complete = sizeof(element_type) };
573
574   // Actually hold the data.
575   base::cef_internal::scoped_ptr_impl<element_type, deleter_type> impl_;
576
577   // Disable initialization from any type other than element_type*, by
578   // providing a constructor that matches such an initialization, but is
579   // private and has no definition. This is disabled because it is not safe to
580   // call delete[] on an array whose static type does not match its dynamic
581   // type.
582   template <typename U> explicit scoped_ptr(U* array);
583   explicit scoped_ptr(int disallow_construction_from_null);
584
585   // Disable reset() from any type other than element_type*, for the same
586   // reasons as the constructor above.
587   template <typename U> void reset(U* array);
588   void reset(int disallow_reset_from_null);
589
590   // Forbid comparison of scoped_ptr types.  If U != T, it totally
591   // doesn't make sense, and if U == T, it still doesn't make sense
592   // because you should never have the same object owned by two different
593   // scoped_ptrs.
594   template <class U> bool operator==(scoped_ptr<U> const& p2) const;
595   template <class U> bool operator!=(scoped_ptr<U> const& p2) const;
596 };
597
598 // Free functions
599 template <class T, class D>
600 void swap(scoped_ptr<T, D>& p1, scoped_ptr<T, D>& p2) {
601   p1.swap(p2);
602 }
603
604 template <class T, class D>
605 bool operator==(T* p1, const scoped_ptr<T, D>& p2) {
606   return p1 == p2.get();
607 }
608
609 template <class T, class D>
610 bool operator!=(T* p1, const scoped_ptr<T, D>& p2) {
611   return p1 != p2.get();
612 }
613
614 // A function to convert T* into scoped_ptr<T>
615 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation
616 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
617 template <typename T>
618 scoped_ptr<T> make_scoped_ptr(T* ptr) {
619   return scoped_ptr<T>(ptr);
620 }
621
622 #endif  // !BUILDING_CEF_SHARED
623
624 #endif  // CEF_INCLUDE_BASE_CEF_MEMORY_SCOPED_PTR_H_