| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef BASE_MEMORY_SCOPED_GENERIC_OBJ_H_ | |
| 6 #define BASE_MEMORY_SCOPED_GENERIC_OBJ_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/compiler_specific.h" | |
| 10 | |
| 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. | |
| 15 // | |
| 16 // Example (Mac-specific): | |
| 17 // | |
| 18 // class ScopedDestroyRendererInfo { | |
| 19 // public: | |
| 20 // void operator()(CGLRendererInfoObj x) const { | |
| 21 // CGLDestroyRendererInfo(x); | |
| 22 // } | |
| 23 // }; | |
| 24 // | |
| 25 // ... | |
| 26 // | |
| 27 // CGLRendererInfoObj renderer_info = NULL; | |
| 28 // ... | |
| 29 // ScopedGenericObj<CGLRendererInfoObj, ScopedDestroyRendererInfo> | |
| 30 // scoper(renderer_info); | |
| 31 | |
| 32 template<class C, class FreeProc> | |
| 33 class ScopedGenericObj { | |
| 34 public: | |
| 35 | |
| 36 // The element type | |
| 37 typedef C element_type; | |
| 38 | |
| 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 | |
| 42 // Free functor. | |
| 43 explicit ScopedGenericObj(C p = C()): obj_(p) {} | |
| 44 | |
| 45 // Destructor. If there is a C object, call the Free functor. | |
| 46 ~ScopedGenericObj() { | |
| 47 reset(); | |
| 48 } | |
| 49 | |
| 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()) { | |
| 54 if (obj_ != p) { | |
| 55 FreeProc free_proc; | |
| 56 free_proc(obj_); | |
| 57 obj_ = p; | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 operator C() const { | |
| 62 return obj_; | |
| 63 } | |
| 64 | |
| 65 C get() const { | |
| 66 return obj_; | |
| 67 } | |
| 68 | |
| 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 { | |
| 75 return obj_ == p; | |
| 76 } | |
| 77 | |
| 78 bool operator!=(C p) const { | |
| 79 return obj_ != p; | |
| 80 } | |
| 81 | |
| 82 // Swap two ScopedGenericObjs. | |
| 83 void swap(ScopedGenericObj& b) { | |
| 84 C tmp = b.obj_; | |
| 85 b.obj_ = obj_; | |
| 86 obj_ = tmp; | |
| 87 } | |
| 88 | |
| 89 // Release a pointer. | |
| 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 { | |
| 95 C tmp = obj_; | |
| 96 obj_ = NULL; | |
| 97 return tmp; | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 C obj_; | |
| 102 | |
| 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; | |
| 108 | |
| 109 // Disallow evil constructors. | |
| 110 ScopedGenericObj(const ScopedGenericObj&); | |
| 111 void operator=(const ScopedGenericObj&); | |
| 112 }; | |
| 113 | |
| 114 template<class C, class FP> inline | |
| 115 void swap(ScopedGenericObj<C, FP>& a, ScopedGenericObj<C, FP>& b) { | |
| 116 a.swap(b); | |
| 117 } | |
| 118 | |
| 119 template<class C, class FP> inline | |
| 120 bool operator==(C* p, const ScopedGenericObj<C, FP>& b) { | |
| 121 return p == b.get(); | |
| 122 } | |
| 123 | |
| 124 template<class C, class FP> inline | |
| 125 bool operator!=(C* p, const ScopedGenericObj<C, FP>& b) { | |
| 126 return p != b.get(); | |
| 127 } | |
| 128 | |
| 129 #endif // BASE_MEMORY_SCOPED_GENERIC_OBJ_H_ | |
| OLD | NEW |