summaryrefslogtreecommitdiffstats
path: root/gpu/np_utils/np_object_pointer.h
diff options
context:
space:
mode:
Diffstat (limited to 'gpu/np_utils/np_object_pointer.h')
-rw-r--r--gpu/np_utils/np_object_pointer.h119
1 files changed, 119 insertions, 0 deletions
diff --git a/gpu/np_utils/np_object_pointer.h b/gpu/np_utils/np_object_pointer.h
new file mode 100644
index 0000000..44286a7
--- /dev/null
+++ b/gpu/np_utils/np_object_pointer.h
@@ -0,0 +1,119 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef GPU_NP_UTILS_NP_OBJECT_POINTER_H_
+#define GPU_NP_UTILS_NP_OBJECT_POINTER_H_
+
+#include "base/logging.h"
+#include "gpu/np_utils/np_browser.h"
+#include "gpu/np_utils/np_headers.h"
+
+namespace np_utils {
+
+// Smart pointer for NPObjects that automatically handles reference counting.
+template <typename NPObjectType>
+class NPObjectPointer {
+ public:
+ NPObjectPointer() : object_(NULL) {}
+
+ NPObjectPointer(const NPObjectPointer& rhs) : object_(rhs.object_) {
+ Retain();
+ }
+
+ explicit NPObjectPointer(NPObjectType* p) : object_(p) {
+ Retain();
+ }
+
+ template <typename RHS>
+ NPObjectPointer(const NPObjectPointer<RHS>& rhs) : object_(rhs.Get()) {
+ Retain();
+ }
+
+ ~NPObjectPointer() {
+ Release();
+ }
+
+ NPObjectPointer& operator=(const NPObjectPointer& rhs) {
+ if (object_ == rhs.Get())
+ return *this;
+
+ Release();
+ object_ = rhs.object_;
+ Retain();
+ return *this;
+ }
+
+ template <typename RHS>
+ NPObjectPointer& operator=(const NPObjectPointer<RHS>& rhs) {
+ if (object_ == rhs.Get())
+ return *this;
+
+ Release();
+ object_ = rhs.Get();
+ Retain();
+ return *this;
+ }
+
+ template <class RHS>
+ bool operator==(const NPObjectPointer<RHS>& rhs) const {
+ return object_ == rhs.Get();
+ }
+
+ template <class RHS>
+ bool operator!=(const NPObjectPointer<RHS>& rhs) const {
+ return object_ != rhs.Get();
+ }
+
+ // The NPObject convention for returning an NPObject pointer from a function
+ // is that the caller is responsible for releasing the reference count.
+ static NPObjectPointer FromReturned(NPObjectType* p) {
+ NPObjectPointer pointer(p);
+ pointer.Release();
+ return pointer;
+ }
+
+ // The NPObject convention for returning an NPObject pointer from a function
+ // is that the caller is responsible for releasing the reference count.
+ NPObjectType* ToReturned() const {
+ Retain();
+ return object_;
+ }
+
+ NPObjectType* Get() const {
+ return object_;
+ }
+
+ NPObjectType* operator->() const {
+ return object_;
+ }
+
+ NPObjectType& operator*() const {
+ return *object_;
+ }
+
+ private:
+ void Retain() const {
+ if (object_) {
+ NPBrowser::get()->RetainObject(object_);
+ }
+ }
+
+ void Release() const {
+ if (object_) {
+ NPBrowser::get()->ReleaseObject(object_);
+ }
+ }
+
+ NPObjectType* object_;
+};
+
+// For test diagnostics.
+template <typename NPObjectType>
+std::ostream& operator<<(std::ostream& stream,
+ const NPObjectPointer<NPObjectType>& pointer) {
+ return stream << pointer.Get();
+}
+} // namespace np_utils
+
+#endif // GPU_NP_UTILS_NP_OBJECT_POINTER_H_