Blender  V2.93
FN_multi_function_params.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
28 #include "BLI_resource_scope.hh"
29 
33 
34 namespace blender::fn {
35 
37  private:
38  ResourceScope scope_;
39  const MFSignature *signature_;
40  int64_t min_array_size_;
41  Vector<const GVArray *> virtual_arrays_;
42  Vector<GMutableSpan> mutable_spans_;
43  Vector<const GVVectorArray *> virtual_vector_arrays_;
44  Vector<GVectorArray *> vector_arrays_;
45 
46  friend class MFParams;
47 
48  public:
49  MFParamsBuilder(const MFSignature &signature, int64_t min_array_size)
50  : signature_(&signature), min_array_size_(min_array_size)
51  {
52  }
53 
54  MFParamsBuilder(const class MultiFunction &fn, int64_t min_array_size);
55 
56  template<typename T> void add_readonly_single_input(const T *value, StringRef expected_name = "")
57  {
59  __func__, CPPType::get<T>(), min_array_size_, value),
60  expected_name);
61  }
62  void add_readonly_single_input(const GSpan span, StringRef expected_name = "")
63  {
64  this->add_readonly_single_input(scope_.construct<GVArrayForGSpan>(__func__, span),
65  expected_name);
66  }
67  void add_readonly_single_input(const GVArray &ref, StringRef expected_name = "")
68  {
69  this->assert_current_param_type(MFParamType::ForSingleInput(ref.type()), expected_name);
70  BLI_assert(ref.size() >= min_array_size_);
71  virtual_arrays_.append(&ref);
72  }
73 
74  void add_readonly_vector_input(const GVectorArray &vector_array, StringRef expected_name = "")
75  {
77  scope_.construct<GVVectorArrayForGVectorArray>(__func__, vector_array), expected_name);
78  }
79  void add_readonly_vector_input(const GVVectorArray &ref, StringRef expected_name = "")
80  {
81  this->assert_current_param_type(MFParamType::ForVectorInput(ref.type()), expected_name);
82  BLI_assert(ref.size() >= min_array_size_);
83  virtual_vector_arrays_.append(&ref);
84  }
85 
86  template<typename T> void add_uninitialized_single_output(T *value, StringRef expected_name = "")
87  {
88  this->add_uninitialized_single_output(GMutableSpan(CPPType::get<T>(), value, 1),
89  expected_name);
90  }
92  {
93  this->assert_current_param_type(MFParamType::ForSingleOutput(ref.type()), expected_name);
94  BLI_assert(ref.size() >= min_array_size_);
95  mutable_spans_.append(ref);
96  }
97 
98  void add_vector_output(GVectorArray &vector_array, StringRef expected_name = "")
99  {
100  this->assert_current_param_type(MFParamType::ForVectorOutput(vector_array.type()),
101  expected_name);
102  BLI_assert(vector_array.size() >= min_array_size_);
103  vector_arrays_.append(&vector_array);
104  }
105 
106  void add_single_mutable(GMutableSpan ref, StringRef expected_name = "")
107  {
108  this->assert_current_param_type(MFParamType::ForMutableSingle(ref.type()), expected_name);
109  BLI_assert(ref.size() >= min_array_size_);
110  mutable_spans_.append(ref);
111  }
112 
113  void add_vector_mutable(GVectorArray &vector_array, StringRef expected_name = "")
114  {
115  this->assert_current_param_type(MFParamType::ForMutableVector(vector_array.type()),
116  expected_name);
117  BLI_assert(vector_array.size() >= min_array_size_);
118  vector_arrays_.append(&vector_array);
119  }
120 
121  GMutableSpan computed_array(int param_index)
122  {
123  BLI_assert(ELEM(signature_->param_types[param_index].category(),
126  int data_index = signature_->data_index(param_index);
127  return mutable_spans_[data_index];
128  }
129 
131  {
132  BLI_assert(ELEM(signature_->param_types[param_index].category(),
135  int data_index = signature_->data_index(param_index);
136  return *vector_arrays_[data_index];
137  }
138 
140  {
141  return scope_;
142  }
143 
144  private:
145  void assert_current_param_type(MFParamType param_type, StringRef expected_name = "")
146  {
147  UNUSED_VARS_NDEBUG(param_type, expected_name);
148 #ifdef DEBUG
149  int param_index = this->current_param_index();
150 
151  if (expected_name != "") {
152  StringRef actual_name = signature_->param_names[param_index];
153  BLI_assert(actual_name == expected_name);
154  }
155 
156  MFParamType expected_type = signature_->param_types[param_index];
157  BLI_assert(expected_type == param_type);
158 #endif
159  }
160 
161  int current_param_index() const
162  {
163  return virtual_arrays_.size() + mutable_spans_.size() + virtual_vector_arrays_.size() +
164  vector_arrays_.size();
165  }
166 };
167 
168 class MFParams {
169  private:
170  MFParamsBuilder *builder_;
171 
172  public:
173  MFParams(MFParamsBuilder &builder) : builder_(&builder)
174  {
175  }
176 
177  template<typename T> const VArray<T> &readonly_single_input(int param_index, StringRef name = "")
178  {
179  const GVArray &array = this->readonly_single_input(param_index, name);
180  return builder_->scope_.construct<VArrayForGVArray<T>>(__func__, array);
181  }
182  const GVArray &readonly_single_input(int param_index, StringRef name = "")
183  {
184  this->assert_correct_param(param_index, name, MFParamType::SingleInput);
185  int data_index = builder_->signature_->data_index(param_index);
186  return *builder_->virtual_arrays_[data_index];
187  }
188 
189  template<typename T>
191  {
192  return this->uninitialized_single_output(param_index, name).typed<T>();
193  }
195  {
196  this->assert_correct_param(param_index, name, MFParamType::SingleOutput);
197  int data_index = builder_->signature_->data_index(param_index);
198  return builder_->mutable_spans_[data_index];
199  }
200 
201  template<typename T>
202  const VVectorArray<T> &readonly_vector_input(int param_index, StringRef name = "")
203  {
204  const GVVectorArray &vector_array = this->readonly_vector_input(param_index, name);
205  return builder_->scope_.construct<VVectorArrayForGVVectorArray<T>>(__func__, vector_array);
206  }
207  const GVVectorArray &readonly_vector_input(int param_index, StringRef name = "")
208  {
209  this->assert_correct_param(param_index, name, MFParamType::VectorInput);
210  int data_index = builder_->signature_->data_index(param_index);
211  return *builder_->virtual_vector_arrays_[data_index];
212  }
213 
214  template<typename T>
216  {
217  return {this->vector_output(param_index, name)};
218  }
219  GVectorArray &vector_output(int param_index, StringRef name = "")
220  {
221  this->assert_correct_param(param_index, name, MFParamType::VectorOutput);
222  int data_index = builder_->signature_->data_index(param_index);
223  return *builder_->vector_arrays_[data_index];
224  }
225 
226  template<typename T> MutableSpan<T> single_mutable(int param_index, StringRef name = "")
227  {
228  return this->single_mutable(param_index, name).typed<T>();
229  }
230  GMutableSpan single_mutable(int param_index, StringRef name = "")
231  {
232  this->assert_correct_param(param_index, name, MFParamType::SingleMutable);
233  int data_index = builder_->signature_->data_index(param_index);
234  return builder_->mutable_spans_[data_index];
235  }
236 
237  template<typename T>
239  {
240  return {this->vector_mutable(param_index, name)};
241  }
242  GVectorArray &vector_mutable(int param_index, StringRef name = "")
243  {
244  this->assert_correct_param(param_index, name, MFParamType::VectorMutable);
245  int data_index = builder_->signature_->data_index(param_index);
246  return *builder_->vector_arrays_[data_index];
247  }
248 
249  private:
250  void assert_correct_param(int param_index, StringRef name, MFParamType param_type)
251  {
252  UNUSED_VARS_NDEBUG(param_index, name, param_type);
253 #ifdef DEBUG
254  BLI_assert(builder_->signature_->param_types[param_index] == param_type);
255  if (name.size() > 0) {
256  BLI_assert(builder_->signature_->param_names[param_index] == name);
257  }
258 #endif
259  }
260 
261  void assert_correct_param(int param_index, StringRef name, MFParamType::Category category)
262  {
263  UNUSED_VARS_NDEBUG(param_index, name, category);
264 #ifdef DEBUG
265  BLI_assert(builder_->signature_->param_types[param_index].category() == category);
266  if (name.size() > 0) {
267  BLI_assert(builder_->signature_->param_names[param_index] == name);
268  }
269 #endif
270  }
271 };
272 
273 } // namespace blender::fn
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
T & construct(const char *name, Args &&... args)
constexpr int64_t size() const
int64_t size() const
Definition: BLI_vector.hh:662
void append(const T &value)
Definition: BLI_vector.hh:438
const CPPType & type() const
const CPPType & type() const
const CPPType & type() const
static MFParamType ForVectorInput(const CPPType &base_type)
static MFParamType ForSingleOutput(const CPPType &type)
static MFParamType ForMutableSingle(const CPPType &type)
static MFParamType ForVectorOutput(const CPPType &base_type)
static MFParamType ForMutableVector(const CPPType &base_type)
static MFParamType ForSingleInput(const CPPType &type)
void add_uninitialized_single_output(GMutableSpan ref, StringRef expected_name="")
void add_readonly_single_input(const GVArray &ref, StringRef expected_name="")
MFParamsBuilder(const MFSignature &signature, int64_t min_array_size)
void add_readonly_single_input(const GSpan span, StringRef expected_name="")
void add_readonly_vector_input(const GVVectorArray &ref, StringRef expected_name="")
void add_uninitialized_single_output(T *value, StringRef expected_name="")
void add_vector_mutable(GVectorArray &vector_array, StringRef expected_name="")
GVectorArray & computed_vector_array(int param_index)
void add_vector_output(GVectorArray &vector_array, StringRef expected_name="")
void add_readonly_vector_input(const GVectorArray &vector_array, StringRef expected_name="")
void add_single_mutable(GMutableSpan ref, StringRef expected_name="")
GMutableSpan computed_array(int param_index)
void add_readonly_single_input(const T *value, StringRef expected_name="")
MutableSpan< T > uninitialized_single_output(int param_index, StringRef name="")
GVectorArray_TypedMutableRef< T > vector_output(int param_index, StringRef name="")
const GVVectorArray & readonly_vector_input(int param_index, StringRef name="")
const VVectorArray< T > & readonly_vector_input(int param_index, StringRef name="")
MutableSpan< T > single_mutable(int param_index, StringRef name="")
GVectorArray & vector_mutable(int param_index, StringRef name="")
const VArray< T > & readonly_single_input(int param_index, StringRef name="")
MFParams(MFParamsBuilder &builder)
const GVArray & readonly_single_input(int param_index, StringRef name="")
GMutableSpan single_mutable(int param_index, StringRef name="")
GMutableSpan uninitialized_single_output(int param_index, StringRef name="")
GVectorArray_TypedMutableRef< T > vector_mutable(int param_index, StringRef name="")
GVectorArray & vector_output(int param_index, StringRef name="")
#define T
__int64 int64_t
Definition: stdint.h:92
int data_index(int param_index) const