clover: Fix clover::keys and ::values to deal with r-value references properly.
[mesa.git] / src / gallium / state_trackers / clover / util / pointer.hpp
1 //
2 // Copyright 2013 Francisco Jerez
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22
23 #ifndef CLOVER_UTIL_POINTER_HPP
24 #define CLOVER_UTIL_POINTER_HPP
25
26 #include <atomic>
27
28 namespace clover {
29 ///
30 /// Base class for objects that support reference counting.
31 ///
32 class ref_counter {
33 public:
34 ref_counter() : _ref_count(1) {}
35
36 unsigned
37 ref_count() {
38 return _ref_count;
39 }
40
41 void
42 retain() {
43 _ref_count++;
44 }
45
46 bool
47 release() {
48 return (--_ref_count) == 0;
49 }
50
51 private:
52 std::atomic<unsigned> _ref_count;
53 };
54
55 ///
56 /// Intrusive smart pointer for objects that implement the
57 /// clover::ref_counter interface.
58 ///
59 template<typename T>
60 class ref_ptr {
61 public:
62 ref_ptr(T *q = NULL) : p(NULL) {
63 reset(q);
64 }
65
66 ref_ptr(const ref_ptr<T> &ref) : p(NULL) {
67 reset(ref.p);
68 }
69
70 ~ref_ptr() {
71 reset(NULL);
72 }
73
74 void
75 reset(T *q = NULL) {
76 if (q)
77 q->retain();
78 if (p && p->release())
79 delete p;
80 p = q;
81 }
82
83 ref_ptr &
84 operator=(const ref_ptr &ref) {
85 reset(ref.p);
86 return *this;
87 }
88
89 T &
90 operator*() const {
91 return *p;
92 }
93
94 T *
95 operator->() const {
96 return p;
97 }
98
99 explicit operator bool() const {
100 return p;
101 }
102
103 private:
104 T *p;
105 };
106
107 ///
108 /// Transfer the caller's ownership of a reference-counted object
109 /// to a clover::ref_ptr smart pointer.
110 ///
111 template<typename T>
112 inline ref_ptr<T>
113 transfer(T *p) {
114 ref_ptr<T> ref { p };
115 p->release();
116 return ref;
117 }
118
119 ///
120 /// Class that implements the usual pointer interface but in fact
121 /// contains the object it seems to be pointing to.
122 ///
123 template<typename T>
124 class pseudo_ptr {
125 public:
126 pseudo_ptr(T x) : x(x) {
127 }
128
129 pseudo_ptr(const pseudo_ptr &p) : x(p.x) {
130 }
131
132 pseudo_ptr &
133 operator=(const pseudo_ptr &p) {
134 x = p.x;
135 return *this;
136 }
137
138 T &
139 operator*() {
140 return x;
141 }
142
143 T *
144 operator->() {
145 return &x;
146 }
147
148 explicit operator bool() const {
149 return true;
150 }
151
152 private:
153 T x;
154 };
155 }
156
157 #endif