Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BKE_anonymous_attribute.hh « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3f1835052b8c18c04023994054533acb55f85e69 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#pragma once

#include <atomic>
#include <string>

#include "BLI_string_ref.hh"

#include "BKE_anonymous_attribute.h"

namespace blender::bke {

template<bool IsStrongReference> class OwnedAnonymousAttributeID {
 private:
  const AnonymousAttributeID *data_ = nullptr;

 public:
  OwnedAnonymousAttributeID() = default;

  explicit OwnedAnonymousAttributeID(StringRefNull debug_name)
  {
    if constexpr (IsStrongReference) {
      data_ = BKE_anonymous_attribute_id_new_strong(debug_name.c_str());
    }
    else {
      data_ = BKE_anonymous_attribute_id_new_weak(debug_name.c_str());
    }
  }

  /* This transfers ownership, so no incref is necessary. */
  explicit OwnedAnonymousAttributeID(const AnonymousAttributeID *anonymous_id)
      : data_(anonymous_id)
  {
  }

  template<bool OtherIsStrong>
  OwnedAnonymousAttributeID(const OwnedAnonymousAttributeID<OtherIsStrong> &other)
  {
    data_ = other.data_;
    this->incref();
  }

  template<bool OtherIsStrong>
  OwnedAnonymousAttributeID(OwnedAnonymousAttributeID<OtherIsStrong> &&other)
  {
    data_ = other.data_;
    this->incref();
    other.decref();
    other.data_ = nullptr;
  }

  ~OwnedAnonymousAttributeID()
  {
    this->decref();
  }

  template<bool OtherIsStrong>
  OwnedAnonymousAttributeID &operator=(const OwnedAnonymousAttributeID<OtherIsStrong> &other)
  {
    if (this == &other) {
      return *this;
    }
    this->~OwnedAnonymousAttributeID();
    new (this) OwnedAnonymousAttributeID(other);
    return *this;
  }

  template<bool OtherIsStrong>
  OwnedAnonymousAttributeID &operator=(OwnedAnonymousAttributeID<OtherIsStrong> &&other)
  {
    if (this == &other) {
      return *this;
    }
    this->~OwnedAnonymousAttributeID();
    new (this) OwnedAnonymousAttributeID(std::move(other));
    return *this;
  }

  operator bool() const
  {
    return data_ != nullptr;
  }

  StringRefNull debug_name() const
  {
    BLI_assert(data_ != nullptr);
    return BKE_anonymous_attribute_id_debug_name(data_);
  }

  bool has_strong_references() const
  {
    BLI_assert(data_ != nullptr);
    return BKE_anonymous_attribute_id_has_strong_references(data_);
  }

  const AnonymousAttributeID *extract()
  {
    const AnonymousAttributeID *extracted_data = data_;
    /* Don't decref because the caller becomes the new owner. */
    data_ = nullptr;
    return extracted_data;
  }

 private:
  void incref()
  {
    if (data_ == nullptr) {
      return;
    }
    if constexpr (IsStrongReference) {
      BKE_anonymous_attribute_id_increment_strong(data_);
    }
    else {
      BKE_anonymous_attribute_id_increment_weak(data_);
    }
  }

  void decref()
  {
    if (data_ == nullptr) {
      return;
    }
    if constexpr (IsStrongReference) {
      BKE_anonymous_attribute_id_decrement_strong(data_);
    }
    else {
      BKE_anonymous_attribute_id_decrement_weak(data_);
    }
  }
};

using StrongAnonymousAttributeID = OwnedAnonymousAttributeID<true>;
using WeakAnonymousAttributeID = OwnedAnonymousAttributeID<false>;

class AttributeIDRef {
 private:
  StringRef name_;
  const AnonymousAttributeID *anonymous_id_ = nullptr;

 public:
  AttributeIDRef() = default;

  AttributeIDRef(StringRef name) : name_(name)
  {
  }

  AttributeIDRef(StringRefNull name) : name_(name)
  {
  }

  AttributeIDRef(const char *name) : name_(name)
  {
  }

  AttributeIDRef(const std::string &name) : name_(name)
  {
  }

  /* The anonymous id is only borrowed, the caller has to keep a reference to it. */
  AttributeIDRef(const AnonymousAttributeID *anonymous_id) : anonymous_id_(anonymous_id)
  {
  }

  operator bool() const
  {
    return this->is_named() || this->is_anonymous();
  }

  bool is_named() const
  {
    return !name_.is_empty();
  }

  bool is_anonymous() const
  {
    return anonymous_id_ != nullptr;
  }

  StringRef name() const
  {
    BLI_assert(this->is_named());
    return name_;
  }

  const AnonymousAttributeID &anonymous_id() const
  {
    BLI_assert(this->is_anonymous());
    return *anonymous_id_;
  }
};

}  // namespace blender::bke