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