Blender V4.3
FN_multi_function_procedure_builder.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
12
14
19 private:
21 Procedure *procedure_ = nullptr;
24
25 public:
26 struct Branch;
27 struct Loop;
28
29 ProcedureBuilder(Procedure &procedure,
31
33
34 ProcedureBuilder(Branch &branch);
35
36 void set_cursor(const InstructionCursor &cursor);
39 void set_cursor_after_branch(Branch &branch);
40 void set_cursor_after_loop(Loop &loop);
41
42 void add_destruct(Variable &variable);
43 void add_destruct(Span<Variable *> variables);
44
46
47 Branch add_branch(Variable &condition);
48
49 Loop add_loop();
50 void add_loop_continue(Loop &loop);
51 void add_loop_break(Loop &loop);
52
55 Span<Variable *> param_variables);
56
58 Span<Variable *> input_and_mutable_variables = {});
59
60 template<int OutputN>
61 std::array<Variable *, OutputN> add_call(const MultiFunction &fn,
62 Span<Variable *> input_and_mutable_variables = {});
63
64 void add_parameter(ParamType::InterfaceType interface_type, Variable &variable);
65 Variable &add_parameter(ParamType param_type, std::string name = "");
66
67 Variable &add_input_parameter(DataType data_type, std::string name = "");
68 template<typename T> Variable &add_single_input_parameter(std::string name = "");
69 template<typename T> Variable &add_single_mutable_parameter(std::string name = "");
70
71 void add_output_parameter(Variable &variable);
72
73 private:
74 void link_to_cursors(Instruction *instruction);
75};
76
81
83 Instruction *begin = nullptr;
84 DummyInstruction *end = nullptr;
85};
86
87/* --------------------------------------------------------------------
88 * ProcedureBuilder inline methods.
89 */
90
92 : ProcedureBuilder(*branch.branch_true.procedure_)
93{
94 this->set_cursor_after_branch(branch);
95}
96
98 : procedure_(&procedure), cursors_({initial_cursor})
99{
100}
101
103 : ProcedureBuilder(*builders[0]->procedure_)
104{
105 this->set_cursor(builders);
106}
107
109{
110 cursors_ = {cursor};
111}
112
114{
115 cursors_ = cursors;
116}
117
119{
120 this->set_cursor({&branch.branch_false, &branch.branch_true});
121}
122
124{
125 this->set_cursor(InstructionCursor{*loop.end});
126}
127
129{
130 cursors_.clear();
131 for (ProcedureBuilder *builder : builders) {
132 cursors_.extend(builder->cursors_);
133 }
134}
135
136template<int OutputN>
137inline std::array<Variable *, OutputN> ProcedureBuilder::add_call(
138 const MultiFunction &fn, Span<Variable *> input_and_mutable_variables)
139{
140 Vector<Variable *> output_variables = this->add_call(fn, input_and_mutable_variables);
141 BLI_assert(output_variables.size() == OutputN);
142
143 std::array<Variable *, OutputN> output_array;
144 initialized_copy_n(output_variables.data(), OutputN, output_array.data());
145 return output_array;
146}
147
149 Variable &variable)
150{
151 procedure_->add_parameter(interface_type, variable);
152}
153
154inline Variable &ProcedureBuilder::add_parameter(ParamType param_type, std::string name)
155{
156 Variable &variable = procedure_->new_variable(param_type.data_type(), std::move(name));
157 this->add_parameter(param_type.interface_type(), variable);
158 return variable;
159}
160
161inline Variable &ProcedureBuilder::add_input_parameter(DataType data_type, std::string name)
162{
163 return this->add_parameter(ParamType(ParamType::Input, data_type), std::move(name));
164}
165
166template<typename T>
168{
169 return this->add_parameter(ParamType::ForSingleInput(CPPType::get<T>()), std::move(name));
170}
171
172template<typename T>
174{
175 return this->add_parameter(ParamType::ForMutableSingle(CPPType::get<T>()), std::move(name));
176}
177
179{
180 this->add_parameter(ParamType::Output, variable);
181}
182
183inline void ProcedureBuilder::link_to_cursors(Instruction *instruction)
184{
185 for (InstructionCursor &cursor : cursors_) {
186 cursor.set_next(*procedure_, instruction);
187 }
188}
189
190} // namespace blender::fn::multi_function
#define BLI_assert(a)
Definition BLI_assert.h:50
static const CPPType & get()
int64_t size() const
static ParamType ForMutableSingle(const CPPType &type)
static ParamType ForSingleInput(const CPPType &type)
Variable & add_input_parameter(DataType data_type, std::string name="")
ProcedureBuilder(Procedure &procedure, InstructionCursor initial_cursor=InstructionCursor::ForEntry())
Vector< Variable * > add_call(const MultiFunction &fn, Span< Variable * > input_and_mutable_variables={})
void add_parameter(ParamType::InterfaceType interface_type, Variable &variable)
CallInstruction & add_call_with_no_variables(const MultiFunction &fn)
CallInstruction & add_call_with_all_variables(const MultiFunction &fn, Span< Variable * > param_variables)
void add_parameter(ParamType::InterfaceType interface_type, Variable &variable)
Variable & new_variable(DataType data_type, std::string name="")
void initialized_copy_n(const T *src, int64_t n, T *dst)