OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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 GPU_NP_UTILS_NP_OBJECT_POINTER_H_ | |
6 #define GPU_NP_UTILS_NP_OBJECT_POINTER_H_ | |
7 | |
8 #include "base/logging.h" | |
9 #include "gpu/np_utils/np_browser.h" | |
10 #include "gpu/np_utils/np_headers.h" | |
11 | |
12 namespace np_utils { | |
13 | |
14 // Smart pointer for NPObjects that automatically handles reference counting. | |
15 template <typename NPObjectType> | |
16 class NPObjectPointer { | |
17 public: | |
18 NPObjectPointer() : object_(NULL) {} | |
19 | |
20 NPObjectPointer(const NPObjectPointer& rhs) : object_(rhs.object_) { | |
21 Retain(); | |
22 } | |
23 | |
24 explicit NPObjectPointer(NPObjectType* p) : object_(p) { | |
25 Retain(); | |
26 } | |
27 | |
28 template <typename RHS> | |
29 NPObjectPointer(const NPObjectPointer<RHS>& rhs) : object_(rhs.Get()) { | |
30 Retain(); | |
31 } | |
32 | |
33 ~NPObjectPointer() { | |
34 Release(); | |
35 } | |
36 | |
37 NPObjectPointer& operator=(const NPObjectPointer& rhs) { | |
38 if (object_ == rhs.Get()) | |
39 return *this; | |
40 | |
41 Release(); | |
42 object_ = rhs.object_; | |
43 Retain(); | |
44 return *this; | |
45 } | |
46 | |
47 template <typename RHS> | |
48 NPObjectPointer& operator=(const NPObjectPointer<RHS>& rhs) { | |
49 if (object_ == rhs.Get()) | |
50 return *this; | |
51 | |
52 Release(); | |
53 object_ = rhs.Get(); | |
54 Retain(); | |
55 return *this; | |
56 } | |
57 | |
58 template <class RHS> | |
59 bool operator==(const NPObjectPointer<RHS>& rhs) const { | |
60 return object_ == rhs.Get(); | |
61 } | |
62 | |
63 template <class RHS> | |
64 bool operator!=(const NPObjectPointer<RHS>& rhs) const { | |
65 return object_ != rhs.Get(); | |
66 } | |
67 | |
68 // The NPObject convention for returning an NPObject pointer from a function | |
69 // is that the caller is responsible for releasing the reference count. | |
70 static NPObjectPointer FromReturned(NPObjectType* p) { | |
71 NPObjectPointer pointer(p); | |
72 pointer.Release(); | |
73 return pointer; | |
74 } | |
75 | |
76 // The NPObject convention for returning an NPObject pointer from a function | |
77 // is that the caller is responsible for releasing the reference count. | |
78 NPObjectType* ToReturned() const { | |
79 Retain(); | |
80 return object_; | |
81 } | |
82 | |
83 NPObjectType* Get() const { | |
84 return object_; | |
85 } | |
86 | |
87 NPObjectType* operator->() const { | |
88 return object_; | |
89 } | |
90 | |
91 NPObjectType& operator*() const { | |
92 return *object_; | |
93 } | |
94 | |
95 private: | |
96 void Retain() const { | |
97 if (object_) { | |
98 NPBrowser::get()->RetainObject(object_); | |
99 } | |
100 } | |
101 | |
102 void Release() const { | |
103 if (object_) { | |
104 NPBrowser::get()->ReleaseObject(object_); | |
105 } | |
106 } | |
107 | |
108 NPObjectType* object_; | |
109 }; | |
110 | |
111 // For test diagnostics. | |
112 template <typename NPObjectType> | |
113 std::ostream& operator<<(std::ostream& stream, | |
114 const NPObjectPointer<NPObjectType>& pointer) { | |
115 return stream << pointer.Get(); | |
116 } | |
117 } // namespace np_utils | |
118 | |
119 #endif // GPU_NP_UTILS_NP_OBJECT_POINTER_H_ | |
OLD | NEW |