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

FN_attributes_ref.hh « functions « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3c31665e0b59c5260d08b1ac73aa78d70caf5628 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * 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.
 */

#ifndef __FN_ATTRIBUTES_REF_HH__
#define __FN_ATTRIBUTES_REF_HH__

/** \file
 * \ingroup fn
 *
 * An AttributesRef references multiple arrays of equal length. Each array has a corresponding name
 * and index.
 */

#include <optional>

#include "FN_spans.hh"

#include "BLI_linear_allocator.hh"
#include "BLI_map.hh"
#include "BLI_utility_mixins.hh"
#include "BLI_vector_set.hh"

namespace blender::fn {

class AttributesInfo;

class AttributesInfoBuilder : NonCopyable, NonMovable {
 private:
  LinearAllocator<> allocator_;
  VectorSet<std::string> names_;
  Vector<const CPPType *> types_;
  Vector<void *> defaults_;

  friend AttributesInfo;

 public:
  AttributesInfoBuilder() = default;
  ~AttributesInfoBuilder();

  template<typename T> void add(StringRef name, const T &default_value)
  {
    this->add(name, CPPType::get<T>(), (const void *)&default_value);
  }

  void add(StringRef name, const CPPType &type, const void *default_value = nullptr);
};

/**
 * Stores which attributes are in an AttributesRef. Every attribute has a unique index, a unique
 * name, a type and a default value.
 */
class AttributesInfo : NonCopyable, NonMovable {
 private:
  LinearAllocator<> allocator_;
  Map<StringRefNull, uint> index_by_name_;
  Vector<StringRefNull> name_by_index_;
  Vector<const CPPType *> type_by_index_;
  Vector<void *> defaults_;

 public:
  AttributesInfo() = default;
  AttributesInfo(const AttributesInfoBuilder &builder);
  ~AttributesInfo();

  uint size() const
  {
    return name_by_index_.size();
  }

  IndexRange index_range() const
  {
    return name_by_index_.index_range();
  }

  StringRefNull name_of(uint index) const
  {
    return name_by_index_[index];
  }

  uint index_of(StringRef name) const
  {
    return index_by_name_.lookup_as(name);
  }

  const void *default_of(uint index) const
  {
    return defaults_[index];
  }

  const void *default_of(StringRef name) const
  {
    return this->default_of(this->index_of(name));
  }

  template<typename T> const T &default_of(uint index) const
  {
    BLI_assert(type_by_index_[index]->is<T>());
    return *(T *)defaults_[index];
  }

  template<typename T> const T &default_of(StringRef name) const
  {
    return this->default_of<T>(this->index_of(name));
  }

  const CPPType &type_of(uint index) const
  {
    return *type_by_index_[index];
  }

  const CPPType &type_of(StringRef name) const
  {
    return this->type_of(this->index_of(name));
  }

  bool has_attribute(StringRef name, const CPPType &type) const
  {
    return this->try_index_of(name, type) >= 0;
  }

  int try_index_of(StringRef name) const
  {
    return (int)index_by_name_.lookup_default_as(name, -1);
  }

  int try_index_of(StringRef name, const CPPType &type) const
  {
    int index = this->try_index_of(name);
    if (index == -1) {
      return -1;
    }
    else if (this->type_of((uint)index) == type) {
      return index;
    }
    else {
      return -1;
    }
  }
};

/**
 * References multiple arrays that match the description of an AttributesInfo instance. This class
 * is supposed to be relatively cheap to copy. It does not own any of the arrays itself.
 */
class MutableAttributesRef {
 private:
  const AttributesInfo *info_;
  Span<void *> buffers_;
  IndexRange range_;

 public:
  MutableAttributesRef(const AttributesInfo &info, Span<void *> buffers, uint size)
      : MutableAttributesRef(info, buffers, IndexRange(size))
  {
  }

  MutableAttributesRef(const AttributesInfo &info, Span<void *> buffers, IndexRange range)
      : info_(&info), buffers_(buffers), range_(range)
  {
  }

  uint size() const
  {
    return range_.size();
  }

  const AttributesInfo &info() const
  {
    return *info_;
  }

  GMutableSpan get(uint index) const
  {
    const CPPType &type = info_->type_of(index);
    void *ptr = POINTER_OFFSET(buffers_[index], type.size() * range_.start());
    return GMutableSpan(type, ptr, range_.size());
  }

  GMutableSpan get(StringRef name) const
  {
    return this->get(info_->index_of(name));
  }

  template<typename T> MutableSpan<T> get(uint index) const
  {
    BLI_assert(info_->type_of(index).is<T>());
    return MutableSpan<T>((T *)buffers_[index] + range_.start(), range_.size());
  }

  template<typename T> MutableSpan<T> get(StringRef name) const
  {
    return this->get<T>(info_->index_of(name));
  }

  std::optional<GMutableSpan> try_get(StringRef name, const CPPType &type) const
  {
    int index = info_->try_index_of(name, type);
    if (index == -1) {
      return {};
    }
    else {
      return this->get((uint)index);
    }
  }

  template<typename T> std::optional<MutableSpan<T>> try_get(StringRef name) const
  {
    int index = info_->try_index_of(name);
    if (index == -1) {
      return {};
    }
    else if (info_->type_of((uint)index).is<T>()) {
      return this->get<T>((uint)index);
    }
    else {
      return {};
    }
  }

  MutableAttributesRef slice(IndexRange range) const
  {
    return this->slice(range.start(), range.size());
  }

  MutableAttributesRef slice(uint start, uint size) const
  {
    return MutableAttributesRef(*info_, buffers_, range_.slice(start, size));
  }
};

}  // namespace blender::fn

#endif /* __FN_ATTRIBUTES_REF_HH__ */