summaryrefslogtreecommitdiffstats
path: root/o3d/gpu/np_utils/np_object_pointer.h
blob: a8987be9f676fb66acc2a2e688c48c0653d9512d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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 gpu_plugin {

// 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 gpu_plugin

#endif  // GPU_NP_UTILS_NP_OBJECT_POINTER_H_