Blender V5.3
FN_multi_function_params.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
15
16#include <variant>
17
21#include "BLI_resource_scope.hh"
22
24
26
27class ParamsBuilder {
28 private:
29 std::unique_ptr<ResourceScope> scope_;
30 const Signature *signature_;
31 const IndexMask &mask_;
32 int64_t min_array_size_;
34 actual_params_;
35
36 friend class Params;
37
38 ParamsBuilder(const Signature &signature, const IndexMask &mask);
39
40 public:
46
47 template<typename T> void add_readonly_single_input_value(T value, StringRef expected_name = "");
48 template<typename T>
49 void add_readonly_single_input(const T *value, StringRef expected_name = "");
50 void add_readonly_single_input(const GSpan span, StringRef expected_name = "");
51 void add_readonly_single_input(GPointer value, StringRef expected_name = "");
52 void add_readonly_single_input(GVArray varray, StringRef expected_name = "");
53
54 void add_readonly_vector_input(const GVectorArray &vector_array, StringRef expected_name = "");
55 void add_readonly_vector_input(const GSpan single_vector, StringRef expected_name = "");
56 void add_readonly_vector_input(const GVVectorArray &ref, StringRef expected_name = "");
57
58 template<typename T>
59 void add_uninitialized_single_output(T *value, StringRef expected_name = "");
60 void add_uninitialized_single_output(GMutableSpan ref, StringRef expected_name = "");
61
62 void add_ignored_single_output(StringRef expected_name = "");
63
64 void add_vector_output(GVectorArray &vector_array, StringRef expected_name = "");
65
66 void add_single_mutable(GMutableSpan ref, StringRef expected_name = "");
67
68 void add_vector_mutable(GVectorArray &vector_array, StringRef expected_name = "");
69
70 int next_param_index() const;
71
72 GMutableSpan computed_array(int param_index);
73
74 GVectorArray &computed_vector_array(int param_index);
75
76 private:
77 void assert_current_param_type(ParamType param_type, StringRef expected_name = "");
78 void assert_current_param_name(StringRef expected_name);
79
80 ResourceScope &resource_scope();
81
82 void add_unused_output_for_unsupporting_function(const CPPType &type);
83};
84
85class Params {
86 private:
87 ParamsBuilder *builder_;
88
89 public:
90 Params(ParamsBuilder &builder) : builder_(&builder) {}
91
92 template<typename T> VArray<T> readonly_single_input(int param_index, StringRef name = "");
93 const GVArray &readonly_single_input(int param_index, StringRef name = "");
94
101 bool single_output_is_required(int param_index, StringRef name = "");
102
103 template<typename T>
104 MutableSpan<T> uninitialized_single_output(int param_index, StringRef name = "");
105 GMutableSpan uninitialized_single_output(int param_index, StringRef name = "");
106
111 template<typename T>
114
115 template<typename T>
116 const VVectorArray<T> &readonly_vector_input(int param_index, StringRef name = "");
117 const GVVectorArray &readonly_vector_input(int param_index, StringRef name = "");
118
119 template<typename T>
120 GVectorArray_TypedMutableRef<T> vector_output(int param_index, StringRef name = "");
121 GVectorArray &vector_output(int param_index, StringRef name = "");
122
123 template<typename T> MutableSpan<T> single_mutable(int param_index, StringRef name = "");
124 GMutableSpan single_mutable(int param_index, StringRef name = "");
125
126 template<typename T>
127 GVectorArray_TypedMutableRef<T> vector_mutable(int param_index, StringRef name = "");
128 GVectorArray &vector_mutable(int param_index, StringRef name = "");
129
130 private:
131 void assert_correct_param(int param_index, StringRef name, ParamType param_type);
132 void assert_correct_param(int param_index, StringRef name, ParamCategory category);
133};
134
135/* -------------------------------------------------------------------- */
138
139inline ParamsBuilder::ParamsBuilder(const Signature &signature, const IndexMask &mask)
140 : signature_(&signature), mask_(mask), min_array_size_(mask.min_array_size())
141{
142 actual_params_.reserve(signature.params.size());
143}
144
145template<typename T>
147{
148 this->assert_current_param_type(ParamType::ForSingleInput(CPPType::get<T>()), expected_name);
149 actual_params_.append_unchecked_as(std::in_place_type<GVArray>,
152 min_array_size_,
153 &value);
154}
155
156template<typename T>
157inline void ParamsBuilder::add_readonly_single_input(const T *value, StringRef expected_name)
158{
159 this->assert_current_param_type(ParamType::ForSingleInput(CPPType::get<T>()), expected_name);
160 actual_params_.append_unchecked_as(std::in_place_type<GVArray>,
163 min_array_size_,
164 value);
165}
166
167inline void ParamsBuilder::add_readonly_single_input(const GSpan span, StringRef expected_name)
168{
169 this->assert_current_param_type(ParamType::ForSingleInput(span.type()), expected_name);
170 BLI_assert(span.size() >= min_array_size_);
171 actual_params_.append_unchecked_as(std::in_place_type<GVArray>, varray_tag::span{}, span);
172}
173
175{
176 this->assert_current_param_type(ParamType::ForSingleInput(*value.type()), expected_name);
177 actual_params_.append_unchecked_as(std::in_place_type<GVArray>,
179 *value.type(),
180 min_array_size_,
181 value.get());
182}
183
185{
186 this->assert_current_param_type(ParamType::ForSingleInput(varray.type()), expected_name);
187 BLI_assert(varray.size() >= min_array_size_);
188 actual_params_.append_unchecked_as(std::in_place_type<GVArray>, std::move(varray));
189}
190
192 StringRef expected_name)
193{
195 this->resource_scope().construct<GVVectorArray_For_GVectorArray>(vector_array),
196 expected_name);
197}
198
199inline void ParamsBuilder::add_readonly_vector_input(const GSpan single_vector,
200 StringRef expected_name)
201{
202 this->add_readonly_vector_input(this->resource_scope().construct<GVVectorArray_For_SingleGSpan>(
203 single_vector, min_array_size_),
204 expected_name);
205}
206
208 StringRef expected_name)
209{
210 this->assert_current_param_type(ParamType::ForVectorInput(ref.type()), expected_name);
211 BLI_assert(ref.size() >= min_array_size_);
212 actual_params_.append_unchecked_as(std::in_place_type<const GVVectorArray *>, &ref);
213}
214
215template<typename T>
217{
218 this->add_uninitialized_single_output(GMutableSpan(CPPType::get<T>(), value, 1), expected_name);
219}
220
222 StringRef expected_name)
223{
224 this->assert_current_param_type(ParamType::ForSingleOutput(ref.type()), expected_name);
225 BLI_assert(ref.size() >= min_array_size_);
226 actual_params_.append_unchecked_as(std::in_place_type<GMutableSpan>, ref);
227}
228
230{
231 this->assert_current_param_name(expected_name);
232 const int param_index = this->next_param_index();
233 const ParamType &param_type = signature_->params[param_index].type;
235 const DataType data_type = param_type.data_type();
236 const CPPType &type = data_type.single_type();
237
238 if (flag_is_set(signature_->params[param_index].flag, ParamFlag::SupportsUnusedOutput)) {
239 /* An empty span indicates that this is ignored. */
240 const GMutableSpan dummy_span{type};
241 actual_params_.append_unchecked_as(std::in_place_type<GMutableSpan>, dummy_span);
242 }
243 else {
244 this->add_unused_output_for_unsupporting_function(type);
245 }
246}
247
248inline void ParamsBuilder::add_vector_output(GVectorArray &vector_array, StringRef expected_name)
249{
250 this->assert_current_param_type(ParamType::ForVectorOutput(vector_array.type()), expected_name);
251 BLI_assert(vector_array.size() >= min_array_size_);
252 actual_params_.append_unchecked_as(std::in_place_type<GVectorArray *>, &vector_array);
253}
254
256{
257 this->assert_current_param_type(ParamType::ForMutableSingle(ref.type()), expected_name);
258 BLI_assert(ref.size() >= min_array_size_);
259 actual_params_.append_unchecked_as(std::in_place_type<GMutableSpan>, ref);
260}
261
262inline void ParamsBuilder::add_vector_mutable(GVectorArray &vector_array, StringRef expected_name)
263{
264 this->assert_current_param_type(ParamType::ForMutableVector(vector_array.type()), expected_name);
265 BLI_assert(vector_array.size() >= min_array_size_);
266 actual_params_.append_unchecked_as(std::in_place_type<GVectorArray *>, &vector_array);
267}
268
270{
271 return actual_params_.size();
272}
273
275{
276 BLI_assert(ELEM(signature_->params[param_index].type.category(),
279 return std::get<GMutableSpan>(actual_params_[param_index]);
280}
281
283{
284 BLI_assert(ELEM(signature_->params[param_index].type.category(),
287 return *std::get<GVectorArray *>(actual_params_[param_index]);
288}
289
290inline void ParamsBuilder::assert_current_param_type(ParamType param_type, StringRef expected_name)
291{
292 UNUSED_VARS_NDEBUG(param_type, expected_name);
293#ifndef NDEBUG
294 int param_index = this->next_param_index();
295
296 if (expected_name != "") {
297 StringRef actual_name = signature_->params[param_index].name;
298 BLI_assert(actual_name == expected_name);
299 }
300
301 ParamType expected_type = signature_->params[param_index].type;
302 BLI_assert(expected_type == param_type);
303#endif
304}
305
306inline void ParamsBuilder::assert_current_param_name(StringRef expected_name)
307{
308 UNUSED_VARS_NDEBUG(expected_name);
309#ifndef NDEBUG
310 if (expected_name.is_empty()) {
311 return;
312 }
313 const int param_index = this->next_param_index();
314 StringRef actual_name = signature_->params[param_index].name;
315 BLI_assert(actual_name == expected_name);
316#endif
317}
318
319inline ResourceScope &ParamsBuilder::resource_scope()
320{
321 if (!scope_) {
322 scope_ = std::make_unique<ResourceScope>();
323 }
324 return *scope_;
325}
326
328
329/* -------------------------------------------------------------------- */
332
333template<typename T>
335{
336 const GVArray &varray = this->readonly_single_input(param_index, name);
337 return varray.typed<T>();
338}
339
340inline const GVArray &Params::readonly_single_input(int param_index, StringRef name)
341{
342 this->assert_correct_param(param_index, name, ParamCategory::SingleInput);
343 return std::get<GVArray>(builder_->actual_params_[param_index]);
344}
345
346inline bool Params::single_output_is_required(int param_index, StringRef name)
347{
348 this->assert_correct_param(param_index, name, ParamCategory::SingleOutput);
349 return !std::get<GMutableSpan>(builder_->actual_params_[param_index]).is_empty();
350}
351
352template<typename T>
354{
355 return this->uninitialized_single_output(param_index, name).typed<T>();
356}
357
359{
360 this->assert_correct_param(param_index, name, ParamCategory::SingleOutput);
361 BLI_assert(!flag_is_set(builder_->signature_->params[param_index].flag,
363 GMutableSpan span = std::get<GMutableSpan>(builder_->actual_params_[param_index]);
364 BLI_assert(span.size() >= builder_->min_array_size_);
365 return span;
366}
367
368template<typename T>
370 StringRef name)
371{
372 return this->uninitialized_single_output_if_required(param_index, name).typed<T>();
373}
374
376 StringRef name)
377{
378 this->assert_correct_param(param_index, name, ParamCategory::SingleOutput);
379 BLI_assert(flag_is_set(builder_->signature_->params[param_index].flag,
381 return std::get<GMutableSpan>(builder_->actual_params_[param_index]);
382}
383
384template<typename T>
385inline const VVectorArray<T> &Params::readonly_vector_input(int param_index, StringRef name)
386{
387 const GVVectorArray &vector_array = this->readonly_vector_input(param_index, name);
388 return builder_->resource_scope().construct<VVectorArray_For_GVVectorArray<T>>(vector_array);
389}
390
391inline const GVVectorArray &Params::readonly_vector_input(int param_index, StringRef name)
392{
393 this->assert_correct_param(param_index, name, ParamCategory::VectorInput);
394 return *std::get<const GVVectorArray *>(builder_->actual_params_[param_index]);
395}
396
397template<typename T>
399{
400 return {this->vector_output(param_index, name)};
401}
402
403inline GVectorArray &Params::vector_output(int param_index, StringRef name)
404{
405 this->assert_correct_param(param_index, name, ParamCategory::VectorOutput);
406 return *std::get<GVectorArray *>(builder_->actual_params_[param_index]);
407}
408
409template<typename T> inline MutableSpan<T> Params::single_mutable(int param_index, StringRef name)
410{
411 return this->single_mutable(param_index, name).typed<T>();
412}
413
414inline GMutableSpan Params::single_mutable(int param_index, StringRef name)
415{
416 this->assert_correct_param(param_index, name, ParamCategory::SingleMutable);
417 return std::get<GMutableSpan>(builder_->actual_params_[param_index]);
418}
419
420template<typename T>
422{
423 return {this->vector_mutable(param_index, name)};
424}
425
426inline GVectorArray &Params::vector_mutable(int param_index, StringRef name)
427{
428 this->assert_correct_param(param_index, name, ParamCategory::VectorMutable);
429 return *std::get<GVectorArray *>(builder_->actual_params_[param_index]);
430}
431
432inline void Params::assert_correct_param(int param_index, StringRef name, ParamType param_type)
433{
434 UNUSED_VARS_NDEBUG(param_index, name, param_type);
435#ifndef NDEBUG
436 BLI_assert(builder_->signature_->params[param_index].type == param_type);
437 if (name.size() > 0) {
438 BLI_assert(builder_->signature_->params[param_index].name == name);
439 }
440#endif
441}
442
443inline void Params::assert_correct_param(int param_index, StringRef name, ParamCategory category)
444{
445 UNUSED_VARS_NDEBUG(param_index, name, category);
446#ifndef NDEBUG
447 BLI_assert(builder_->signature_->params[param_index].type.category() == category);
448 if (name.size() > 0) {
449 BLI_assert(builder_->signature_->params[param_index].name == name);
450 }
451#endif
452}
453
455
456} // namespace blender::fn::multi_function
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert(a)
#define ELEM(...)
static const CPPType & get()
const CPPType & type() const
const CPPType * type() const
const void * get() const
const CPPType & type() const
int64_t size() const
const CPPType & type() const
constexpr bool is_empty() const
constexpr int64_t size() const
static ParamType ForVectorInput(const CPPType &base_type)
static ParamType ForSingleOutput(const CPPType &type)
static ParamType ForVectorOutput(const CPPType &base_type)
static ParamType ForMutableSingle(const CPPType &type)
static ParamType ForMutableVector(const CPPType &base_type)
static ParamType ForSingleInput(const CPPType &type)
void add_single_mutable(GMutableSpan ref, StringRef expected_name="")
GVectorArray & computed_vector_array(int param_index)
void add_ignored_single_output(StringRef expected_name="")
void add_uninitialized_single_output(T *value, StringRef expected_name="")
void add_vector_output(GVectorArray &vector_array, StringRef expected_name="")
void add_vector_mutable(GVectorArray &vector_array, StringRef expected_name="")
ParamsBuilder(const class MultiFunction &fn, const IndexMask *mask)
void add_readonly_single_input(const T *value, StringRef expected_name="")
void add_readonly_vector_input(const GVectorArray &vector_array, StringRef expected_name="")
void add_readonly_single_input_value(T value, StringRef expected_name="")
MutableSpan< T > uninitialized_single_output_if_required(int param_index, StringRef name="")
const VVectorArray< T > & readonly_vector_input(int param_index, StringRef name="")
MutableSpan< T > uninitialized_single_output(int param_index, StringRef name="")
GVectorArray_TypedMutableRef< T > vector_mutable(int param_index, StringRef name="")
GVectorArray_TypedMutableRef< T > vector_output(int param_index, StringRef name="")
MutableSpan< T > single_mutable(int param_index, StringRef name="")
VArray< T > readonly_single_input(int param_index, StringRef name="")
bool single_output_is_required(int param_index, StringRef name="")
#define T
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90