Blender  V3.3
multi_function_procedure_builder.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
4 
5 namespace blender::fn {
6 
8 {
9  MFDestructInstruction &instruction = procedure_->new_destruct_instruction();
10  instruction.set_variable(&variable);
11  this->link_to_cursors(&instruction);
12  cursors_ = {MFInstructionCursor{instruction}};
13 }
14 
16 {
17  for (MFVariable *variable : variables) {
18  this->add_destruct(*variable);
19  }
20 }
21 
23 {
24  MFReturnInstruction &instruction = procedure_->new_return_instruction();
25  this->link_to_cursors(&instruction);
26  cursors_ = {};
27  return instruction;
28 }
29 
31 {
32  MFCallInstruction &instruction = procedure_->new_call_instruction(fn);
33  this->link_to_cursors(&instruction);
34  cursors_ = {MFInstructionCursor{instruction}};
35  return instruction;
36 }
37 
39  const MultiFunction &fn, Span<MFVariable *> param_variables)
40 {
41  MFCallInstruction &instruction = this->add_call_with_no_variables(fn);
42  instruction.set_params(param_variables);
43  return instruction;
44 }
45 
47  Span<MFVariable *> input_and_mutable_variables)
48 {
49  Vector<MFVariable *> output_variables;
50  MFCallInstruction &instruction = this->add_call_with_no_variables(fn);
51  for (const int param_index : fn.param_indices()) {
52  const MFParamType param_type = fn.param_type(param_index);
53  switch (param_type.interface_type()) {
54  case MFParamType::Input:
55  case MFParamType::Mutable: {
56  MFVariable *variable = input_and_mutable_variables.first();
57  instruction.set_param_variable(param_index, variable);
58  input_and_mutable_variables = input_and_mutable_variables.drop_front(1);
59  break;
60  }
61  case MFParamType::Output: {
62  MFVariable &variable = procedure_->new_variable(param_type.data_type(),
63  fn.param_name(param_index));
64  instruction.set_param_variable(param_index, &variable);
65  output_variables.append(&variable);
66  break;
67  }
68  }
69  }
70  /* All passed in variables should have been dropped in the loop above. */
71  BLI_assert(input_and_mutable_variables.is_empty());
72  return output_variables;
73 }
74 
76 {
77  MFBranchInstruction &instruction = procedure_->new_branch_instruction();
78  instruction.set_condition(&condition);
79  this->link_to_cursors(&instruction);
80  /* Clear cursors because this builder ends here. */
81  cursors_.clear();
82 
83  Branch branch{*procedure_, *procedure_};
84  branch.branch_true.set_cursor(MFInstructionCursor{instruction, true});
85  branch.branch_false.set_cursor(MFInstructionCursor{instruction, false});
86  return branch;
87 }
88 
90 {
91  MFDummyInstruction &loop_begin = procedure_->new_dummy_instruction();
92  MFDummyInstruction &loop_end = procedure_->new_dummy_instruction();
93  this->link_to_cursors(&loop_begin);
94  cursors_ = {MFInstructionCursor{loop_begin}};
95 
96  Loop loop;
97  loop.begin = &loop_begin;
98  loop.end = &loop_end;
99 
100  return loop;
101 }
102 
104 {
105  this->link_to_cursors(loop.begin);
106  /* Clear cursors because this builder ends here. */
107  cursors_.clear();
108 }
109 
111 {
112  this->link_to_cursors(loop.end);
113  /* Clear cursors because this builder ends here. */
114  cursors_.clear();
115 }
116 
117 } // namespace blender::fn
#define BLI_assert(a)
Definition: BLI_assert.h:46
constexpr Span drop_front(int64_t n) const
Definition: BLI_span.hh:159
constexpr const T & first() const
Definition: BLI_span.hh:303
constexpr bool is_empty() const
Definition: BLI_span.hh:248
void append(const T &value)
Definition: BLI_vector.hh:433
void set_condition(MFVariable *variable)
void set_param_variable(int param_index, MFVariable *variable)
void set_params(Span< MFVariable * > variables)
MFCallInstruction & add_call_with_all_variables(const MultiFunction &fn, Span< MFVariable * > param_variables)
MFCallInstruction & add_call_with_no_variables(const MultiFunction &fn)
Vector< MFVariable * > add_call(const MultiFunction &fn, Span< MFVariable * > input_and_mutable_variables={})
MFBranchInstruction & new_branch_instruction()
MFDummyInstruction & new_dummy_instruction()
MFReturnInstruction & new_return_instruction()
MFCallInstruction & new_call_instruction(const MultiFunction &fn)
MFDestructInstruction & new_destruct_instruction()
MFVariable & new_variable(MFDataType data_type, std::string name="")
StringRefNull param_name(int param_index) const
MFParamType param_type(int param_index) const
IndexRange param_indices() const