Blender  V3.3
FN_multi_function_procedure_builder.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
10 
11 namespace blender::fn {
12 
17  private:
19  MFProcedure *procedure_ = nullptr;
22 
23  public:
24  struct Branch;
25  struct Loop;
26 
29 
31 
32  MFProcedureBuilder(Branch &branch);
33 
34  void set_cursor(const MFInstructionCursor &cursor);
37  void set_cursor_after_branch(Branch &branch);
38  void set_cursor_after_loop(Loop &loop);
39 
40  void add_destruct(MFVariable &variable);
41  void add_destruct(Span<MFVariable *> variables);
42 
44 
45  Branch add_branch(MFVariable &condition);
46 
47  Loop add_loop();
48  void add_loop_continue(Loop &loop);
49  void add_loop_break(Loop &loop);
50 
53  Span<MFVariable *> param_variables);
54 
56  Span<MFVariable *> input_and_mutable_variables = {});
57 
58  template<int OutputN>
59  std::array<MFVariable *, OutputN> add_call(const MultiFunction &fn,
60  Span<MFVariable *> input_and_mutable_variables = {});
61 
62  void add_parameter(MFParamType::InterfaceType interface_type, MFVariable &variable);
63  MFVariable &add_parameter(MFParamType param_type, std::string name = "");
64 
65  MFVariable &add_input_parameter(MFDataType data_type, std::string name = "");
66  template<typename T> MFVariable &add_single_input_parameter(std::string name = "");
67  template<typename T> MFVariable &add_single_mutable_parameter(std::string name = "");
68 
69  void add_output_parameter(MFVariable &variable);
70 
71  private:
72  void link_to_cursors(MFInstruction *instruction);
73 };
74 
78 };
79 
81  MFInstruction *begin = nullptr;
82  MFDummyInstruction *end = nullptr;
83 };
84 
85 /* --------------------------------------------------------------------
86  * MFProcedureBuilder inline methods.
87  */
88 
90  : MFProcedureBuilder(*branch.branch_true.procedure_)
91 {
92  this->set_cursor_after_branch(branch);
93 }
94 
96  MFInstructionCursor initial_cursor)
97  : procedure_(&procedure), cursors_({initial_cursor})
98 {
99 }
100 
102  : MFProcedureBuilder(*builders[0]->procedure_)
103 {
104  this->set_cursor(builders);
105 }
106 
108 {
109  cursors_ = {cursor};
110 }
111 
113 {
114  cursors_ = cursors;
115 }
116 
118 {
119  this->set_cursor({&branch.branch_false, &branch.branch_true});
120 }
121 
123 {
124  this->set_cursor(MFInstructionCursor{*loop.end});
125 }
126 
128 {
129  cursors_.clear();
130  for (MFProcedureBuilder *builder : builders) {
131  cursors_.extend(builder->cursors_);
132  }
133 }
134 
135 template<int OutputN>
136 inline std::array<MFVariable *, OutputN> MFProcedureBuilder::add_call(
137  const MultiFunction &fn, Span<MFVariable *> input_and_mutable_variables)
138 {
139  Vector<MFVariable *> output_variables = this->add_call(fn, input_and_mutable_variables);
140  BLI_assert(output_variables.size() == OutputN);
141 
142  std::array<MFVariable *, OutputN> output_array;
143  initialized_copy_n(output_variables.data(), OutputN, output_array.data());
144  return output_array;
145 }
146 
148  MFVariable &variable)
149 {
150  procedure_->add_parameter(interface_type, variable);
151 }
152 
153 inline MFVariable &MFProcedureBuilder::add_parameter(MFParamType param_type, std::string name)
154 {
155  MFVariable &variable = procedure_->new_variable(param_type.data_type(), std::move(name));
156  this->add_parameter(param_type.interface_type(), variable);
157  return variable;
158 }
159 
161 {
162  return this->add_parameter(MFParamType(MFParamType::Input, data_type), std::move(name));
163 }
164 
165 template<typename T>
167 {
168  return this->add_parameter(MFParamType::ForSingleInput(CPPType::get<T>()), std::move(name));
169 }
170 
171 template<typename T>
173 {
174  return this->add_parameter(MFParamType::ForMutableSingle(CPPType::get<T>()), std::move(name));
175 }
176 
178 {
179  this->add_parameter(MFParamType::Output, variable);
180 }
181 
182 inline void MFProcedureBuilder::link_to_cursors(MFInstruction *instruction)
183 {
184  for (MFInstructionCursor &cursor : cursors_) {
185  cursor.set_next(*procedure_, instruction);
186  }
187 }
188 
189 } // namespace blender::fn
#define BLI_assert(a)
Definition: BLI_assert.h:46
static const std::unordered_map< GHOST_TStandardCursor, const char * > cursors
int64_t size() const
Definition: BLI_vector.hh:694
static MFParamType ForMutableSingle(const CPPType &type)
static MFParamType ForSingleInput(const CPPType &type)
MFCallInstruction & add_call_with_all_variables(const MultiFunction &fn, Span< MFVariable * > param_variables)
MFCallInstruction & add_call_with_no_variables(const MultiFunction &fn)
void add_parameter(MFParamType::InterfaceType interface_type, MFVariable &variable)
MFVariable & add_input_parameter(MFDataType data_type, std::string name="")
MFProcedureBuilder(MFProcedure &procedure, MFInstructionCursor initial_cursor=MFInstructionCursor::ForEntry())
MFVariable & add_single_mutable_parameter(std::string name="")
void set_cursor(const MFInstructionCursor &cursor)
Vector< MFVariable * > add_call(const MultiFunction &fn, Span< MFVariable * > input_and_mutable_variables={})
MFVariable & add_single_input_parameter(std::string name="")
void add_parameter(MFParamType::InterfaceType interface_type, MFVariable &variable)
MFVariable & new_variable(MFDataType data_type, std::string name="")
void initialized_copy_n(const T *src, int64_t n, T *dst)