Blender  V2.93
BLI_user_counter.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
23 #include <atomic>
24 
25 namespace blender {
26 
31 template<typename T> class UserCounter {
32  private:
33  T *data_ = nullptr;
34 
35  public:
36  UserCounter() = default;
37 
39  {
40  }
41 
42  UserCounter(const UserCounter &other) : data_(other.data_)
43  {
44  this->user_add(data_);
45  }
46 
47  UserCounter(UserCounter &&other) : data_(other.data_)
48  {
49  other.data_ = nullptr;
50  }
51 
53  {
54  this->user_remove(data_);
55  }
56 
58  {
59  if (this == &other) {
60  return *this;
61  }
62 
63  this->user_remove(data_);
64  data_ = other.data_;
65  this->user_add(data_);
66  return *this;
67  }
68 
70  {
71  if (this == &other) {
72  return *this;
73  }
74 
75  this->user_remove(data_);
76  data_ = other.data_;
77  other.data_ = nullptr;
78  return *this;
79  }
80 
82  {
83  BLI_assert(data_ != nullptr);
84  return data_;
85  }
86 
88  {
89  BLI_assert(data_ != nullptr);
90  return *data_;
91  }
92 
93  operator bool() const
94  {
95  return data_ != nullptr;
96  }
97 
98  T *get()
99  {
100  return data_;
101  }
102 
103  const T *get() const
104  {
105  return data_;
106  }
107 
109  {
110  T *data = data_;
111  data_ = nullptr;
112  return data;
113  }
114 
115  void reset()
116  {
117  this->user_remove(data_);
118  data_ = nullptr;
119  }
120 
121  bool has_value() const
122  {
123  return data_ != nullptr;
124  }
125 
126  uint64_t hash() const
127  {
128  return get_default_hash(data_);
129  }
130 
131  friend bool operator==(const UserCounter &a, const UserCounter &b)
132  {
133  return a.data_ == b.data_;
134  }
135 
136  friend std::ostream &operator<<(std::ostream &stream, const UserCounter &value)
137  {
138  stream << value.data_;
139  return stream;
140  }
141 
142  private:
143  static void user_add(T *data)
144  {
145  if (data != nullptr) {
146  data->user_add();
147  }
148  }
149 
150  static void user_remove(T *data)
151  {
152  if (data != nullptr) {
153  data->user_remove();
154  }
155  }
156 };
157 
158 } // namespace blender
#define BLI_assert(a)
Definition: BLI_assert.h:58
UserCounter(const UserCounter &other)
uint64_t hash() const
friend bool operator==(const UserCounter &a, const UserCounter &b)
UserCounter & operator=(const UserCounter &other)
UserCounter(UserCounter &&other)
UserCounter & operator=(UserCounter &&other)
friend std::ostream & operator<<(std::ostream &stream, const UserCounter &value)
const T * get() const
T * data_
#define T
static unsigned a[3]
Definition: RandGen.cpp:92
uint64_t get_default_hash(const T &v)
Definition: BLI_hash.hh:209
unsigned __int64 uint64_t
Definition: stdint.h:93