Blender  V3.3
BKE_anonymous_attribute.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
5 #include <atomic>
6 #include <string>
7 
8 #include "BLI_hash.hh"
9 #include "BLI_string_ref.hh"
10 
12 
13 namespace blender::bke {
14 
20 template<bool IsStrongReference> class OwnedAnonymousAttributeID {
21  private:
22  const AnonymousAttributeID *data_ = nullptr;
23 
24  template<bool OtherIsStrongReference> friend class OwnedAnonymousAttributeID;
25 
26  public:
28 
31  {
32  if constexpr (IsStrongReference) {
34  }
35  else {
37  }
38  }
39 
44  explicit OwnedAnonymousAttributeID(const AnonymousAttributeID *anonymous_id)
45  : data_(anonymous_id)
46  {
47  }
48 
49  template<bool OtherIsStrong>
51  {
52  data_ = other.data_;
53  this->incref();
54  }
55 
56  template<bool OtherIsStrong>
58  {
59  data_ = other.data_;
60  this->incref();
61  other.decref();
62  other.data_ = nullptr;
63  }
64 
66  {
67  this->decref();
68  }
69 
70  template<bool OtherIsStrong>
72  {
73  if (this == &other) {
74  return *this;
75  }
77  new (this) OwnedAnonymousAttributeID(other);
78  return *this;
79  }
80 
81  template<bool OtherIsStrong>
83  {
84  if (this == &other) {
85  return *this;
86  }
88  new (this) OwnedAnonymousAttributeID(std::move(other));
89  return *this;
90  }
91 
92  operator bool() const
93  {
94  return data_ != nullptr;
95  }
96 
98  {
99  BLI_assert(data_ != nullptr);
101  }
102 
104  {
105  BLI_assert(data_ != nullptr);
107  }
108 
111  {
112  const AnonymousAttributeID *extracted_data = data_;
113  /* Don't decref because the caller becomes the new owner. */
114  data_ = nullptr;
115  return extracted_data;
116  }
117 
119  const AnonymousAttributeID *get() const
120  {
121  return data_;
122  }
123 
124  private:
125  void incref()
126  {
127  if (data_ == nullptr) {
128  return;
129  }
130  if constexpr (IsStrongReference) {
132  }
133  else {
135  }
136  }
137 
138  void decref()
139  {
140  if (data_ == nullptr) {
141  return;
142  }
143  if constexpr (IsStrongReference) {
145  }
146  else {
148  }
149  }
150 };
151 
154 
155 } // namespace blender::bke
void BKE_anonymous_attribute_id_decrement_weak(const AnonymousAttributeID *anonymous_id)
void BKE_anonymous_attribute_id_increment_strong(const AnonymousAttributeID *anonymous_id)
void BKE_anonymous_attribute_id_decrement_strong(const AnonymousAttributeID *anonymous_id)
AnonymousAttributeID * BKE_anonymous_attribute_id_new_strong(const char *debug_name)
const char * BKE_anonymous_attribute_id_debug_name(const AnonymousAttributeID *anonymous_id)
void BKE_anonymous_attribute_id_increment_weak(const AnonymousAttributeID *anonymous_id)
bool BKE_anonymous_attribute_id_has_strong_references(const AnonymousAttributeID *anonymous_id)
AnonymousAttributeID * BKE_anonymous_attribute_id_new_weak(const char *debug_name)
#define BLI_assert(a)
Definition: BLI_assert.h:46
constexpr const char * c_str() const
OwnedAnonymousAttributeID & operator=(OwnedAnonymousAttributeID< OtherIsStrong > &&other)
OwnedAnonymousAttributeID(const OwnedAnonymousAttributeID< OtherIsStrong > &other)
const AnonymousAttributeID * get() const
OwnedAnonymousAttributeID(const AnonymousAttributeID *anonymous_id)
OwnedAnonymousAttributeID & operator=(const OwnedAnonymousAttributeID< OtherIsStrong > &other)
OwnedAnonymousAttributeID(OwnedAnonymousAttributeID< OtherIsStrong > &&other)
T * data_
Definition: eval_output.h:163