Blender  V3.3
BLI_user_counter.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
9 #include <atomic>
10 
11 namespace blender {
12 
17 template<typename T> class UserCounter {
18  private:
19  T *data_ = nullptr;
20 
21  public:
22  UserCounter() = default;
23 
25  {
26  }
27 
28  UserCounter(const UserCounter &other) : data_(other.data_)
29  {
30  this->user_add(data_);
31  }
32 
33  UserCounter(UserCounter &&other) : data_(other.data_)
34  {
35  other.data_ = nullptr;
36  }
37 
39  {
40  this->user_remove(data_);
41  }
42 
44  {
45  if (this == &other) {
46  return *this;
47  }
48 
49  this->user_remove(data_);
50  data_ = other.data_;
51  this->user_add(data_);
52  return *this;
53  }
54 
56  {
57  if (this == &other) {
58  return *this;
59  }
60 
61  this->user_remove(data_);
62  data_ = other.data_;
63  other.data_ = nullptr;
64  return *this;
65  }
66 
68  {
69  BLI_assert(data_ != nullptr);
70  return data_;
71  }
72 
73  const T *operator->() const
74  {
75  BLI_assert(data_ != nullptr);
76  return data_;
77  }
78 
80  {
81  BLI_assert(data_ != nullptr);
82  return *data_;
83  }
84 
85  const T &operator*() const
86  {
87  BLI_assert(data_ != nullptr);
88  return *data_;
89  }
90 
91  operator bool() const
92  {
93  return data_ != nullptr;
94  }
95 
96  T *get()
97  {
98  return data_;
99  }
100 
101  const T *get() const
102  {
103  return data_;
104  }
105 
107  {
108  T *data = data_;
109  data_ = nullptr;
110  return data;
111  }
112 
113  void reset()
114  {
115  this->user_remove(data_);
116  data_ = nullptr;
117  }
118 
119  bool has_value() const
120  {
121  return data_ != nullptr;
122  }
123 
124  uint64_t hash() const
125  {
126  return get_default_hash(data_);
127  }
128 
129  friend bool operator==(const UserCounter &a, const UserCounter &b)
130  {
131  return a.data_ == b.data_;
132  }
133 
134  friend std::ostream &operator<<(std::ostream &stream, const UserCounter &value)
135  {
136  stream << value.data_;
137  return stream;
138  }
139 
140  private:
141  static void user_add(T *data)
142  {
143  if (data != nullptr) {
144  data->user_add();
145  }
146  }
147 
148  static void user_remove(T *data)
149  {
150  if (data != nullptr) {
151  data->user_remove();
152  }
153  }
154 };
155 
156 } // namespace blender
#define BLI_assert(a)
Definition: BLI_assert.h:46
const T & operator*() const
UserCounter(const UserCounter &other)
uint64_t hash() const
const T * operator->() 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_
Definition: eval_output.h:163
#define T
static unsigned a[3]
Definition: RandGen.cpp:78
uint64_t get_default_hash(const T &v)
Definition: BLI_hash.hh:218
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
unsigned __int64 uint64_t
Definition: stdint.h:90