OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #ifndef BASE_MEMORY_SCOPED_GENERIC_OBJ_H_ |
| 6 #define BASE_MEMORY_SCOPED_GENERIC_OBJ_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/compiler_specific.h" |
| 11 |
| 12 // ScopedGenericObj<> is patterned after scoped_ptr_malloc<>, except |
| 13 // that it assumes the template argument is typedef'ed to a pointer |
| 14 // type. It does not support retain/release semantics. It takes as its |
| 15 // second template argument a functor which frees the object. |
| 16 // |
| 17 // Example (Mac-specific): |
| 18 // |
| 19 // class ScopedDestroyRendererInfo { |
| 20 // public: |
| 21 // void operator()(CGLRendererInfoObj x) const { |
| 22 // CGLDestroyRendererInfo(x); |
| 23 // } |
| 24 // }; |
| 25 // |
| 26 // ... |
| 27 // |
| 28 // CGLRendererInfoObj renderer_info = NULL; |
| 29 // ... |
| 30 // ScopedGenericObj<CGLRendererInfoObj, ScopedDestroyRendererInfo> |
| 31 // scoper(renderer_info); |
| 32 |
| 33 template<class C, class FreeProc> |
| 34 class ScopedGenericObj { |
| 35 public: |
| 36 |
| 37 // The element type |
| 38 typedef C element_type; |
| 39 |
| 40 // Constructor. Defaults to initializing with NULL. |
| 41 // There is no way to create an uninitialized ScopedGenericObj. |
| 42 // The input parameter must be allocated with an allocator that matches the |
| 43 // Free functor. |
| 44 explicit ScopedGenericObj(C p = NULL): obj_(p) {} |
| 45 |
| 46 // Destructor. If there is a C object, call the Free functor. |
| 47 ~ScopedGenericObj() { |
| 48 reset(); |
| 49 } |
| 50 |
| 51 // Reset. Calls the Free functor on the current owned object, if any. |
| 52 // Then takes ownership of a new object, if given. |
| 53 // this->reset(this->get()) works. |
| 54 void reset(C p = NULL) { |
| 55 if (obj_ != p) { |
| 56 FreeProc free_proc; |
| 57 free_proc(obj_); |
| 58 obj_ = p; |
| 59 } |
| 60 } |
| 61 |
| 62 operator C() const { |
| 63 return obj_; |
| 64 } |
| 65 |
| 66 C get() const { |
| 67 return obj_; |
| 68 } |
| 69 |
| 70 // Comparison operators. |
| 71 // These return whether a ScopedGenericObj and a plain pointer refer |
| 72 // to the same object, not just to two different but equal objects. |
| 73 // For compatibility with the boost-derived implementation, these |
| 74 // take non-const arguments. |
| 75 bool operator==(C p) const { |
| 76 return obj_ == p; |
| 77 } |
| 78 |
| 79 bool operator!=(C p) const { |
| 80 return obj_ != p; |
| 81 } |
| 82 |
| 83 // Swap two ScopedGenericObjs. |
| 84 void swap(ScopedGenericObj& b) { |
| 85 C tmp = b.obj_; |
| 86 b.obj_ = obj_; |
| 87 obj_ = tmp; |
| 88 } |
| 89 |
| 90 // Release a pointer. |
| 91 // The return value is the current pointer held by this object. |
| 92 // If this object holds a NULL pointer, the return value is NULL. |
| 93 // After this operation, this object will hold a NULL pointer, |
| 94 // and will not own the object any more. |
| 95 C release() WARN_UNUSED_RESULT { |
| 96 C tmp = obj_; |
| 97 obj_ = NULL; |
| 98 return tmp; |
| 99 } |
| 100 |
| 101 private: |
| 102 C obj_; |
| 103 |
| 104 // no reason to use these: each ScopedGenericObj should have its own object. |
| 105 template <class C2, class GP> |
| 106 bool operator==(ScopedGenericObj<C2, GP> const& p) const; |
| 107 template <class C2, class GP> |
| 108 bool operator!=(ScopedGenericObj<C2, GP> const& p) const; |
| 109 |
| 110 // Disallow evil constructors. |
| 111 ScopedGenericObj(const ScopedGenericObj&); |
| 112 void operator=(const ScopedGenericObj&); |
| 113 }; |
| 114 |
| 115 template<class C, class FP> inline |
| 116 void swap(ScopedGenericObj<C, FP>& a, ScopedGenericObj<C, FP>& b) { |
| 117 a.swap(b); |
| 118 } |
| 119 |
| 120 template<class C, class FP> inline |
| 121 bool operator==(C* p, const ScopedGenericObj<C, FP>& b) { |
| 122 return p == b.get(); |
| 123 } |
| 124 |
| 125 template<class C, class FP> inline |
| 126 bool operator!=(C* p, const ScopedGenericObj<C, FP>& b) { |
| 127 return p != b.get(); |
| 128 } |
| 129 |
| 130 #endif // BASE_MEMORY_SCOPED_GENERIC_OBJ_H_ |
OLD | NEW |