1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_MEMORY_SCOPED_GENERIC_OBJ_H_
6 #define BASE_MEMORY_SCOPED_GENERIC_OBJ_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
11 // ScopedGenericObj<> is patterned after scoped_ptr_malloc<>, except
12 // that it assumes the template argument is typedef'ed to a pointer
13 // type. It does not support retain/release semantics. It takes as its
14 // second template argument a functor which frees the object.
16 // Example (Mac-specific):
18 // class ScopedDestroyRendererInfo {
20 // void operator()(CGLRendererInfoObj x) const {
21 // CGLDestroyRendererInfo(x);
27 // CGLRendererInfoObj renderer_info = NULL;
29 // ScopedGenericObj<CGLRendererInfoObj, ScopedDestroyRendererInfo>
30 // scoper(renderer_info);
32 template<class C
, class FreeProc
>
33 class ScopedGenericObj
{
37 typedef C element_type
;
39 // Constructor. Defaults to initializing with NULL.
40 // There is no way to create an uninitialized ScopedGenericObj.
41 // The input parameter must be allocated with an allocator that matches the
43 explicit ScopedGenericObj(C p
= C()): obj_(p
) {}
45 // Destructor. If there is a C object, call the Free functor.
50 // Reset. Calls the Free functor on the current owned object, if any.
51 // Then takes ownership of a new object, if given.
52 // this->reset(this->get()) works.
53 void reset(C p
= C()) {
69 // Comparison operators.
70 // These return whether a ScopedGenericObj and a plain pointer refer
71 // to the same object, not just to two different but equal objects.
72 // For compatibility with the boost-derived implementation, these
73 // take non-const arguments.
74 bool operator==(C p
) const {
78 bool operator!=(C p
) const {
82 // Swap two ScopedGenericObjs.
83 void swap(ScopedGenericObj
& b
) {
90 // The return value is the current pointer held by this object.
91 // If this object holds a NULL pointer, the return value is NULL.
92 // After this operation, this object will hold a NULL pointer,
93 // and will not own the object any more.
94 C
release() WARN_UNUSED_RESULT
{
103 // no reason to use these: each ScopedGenericObj should have its own object.
104 template <class C2
, class GP
>
105 bool operator==(ScopedGenericObj
<C2
, GP
> const& p
) const;
106 template <class C2
, class GP
>
107 bool operator!=(ScopedGenericObj
<C2
, GP
> const& p
) const;
109 // Disallow evil constructors.
110 ScopedGenericObj(const ScopedGenericObj
&);
111 void operator=(const ScopedGenericObj
&);
114 template<class C
, class FP
> inline
115 void swap(ScopedGenericObj
<C
, FP
>& a
, ScopedGenericObj
<C
, FP
>& b
) {
119 template<class C
, class FP
> inline
120 bool operator==(C
* p
, const ScopedGenericObj
<C
, FP
>& b
) {
124 template<class C
, class FP
> inline
125 bool operator!=(C
* p
, const ScopedGenericObj
<C
, FP
>& b
) {
129 #endif // BASE_MEMORY_SCOPED_GENERIC_OBJ_H_