Blender V4.3
FN_lazy_function_graph.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
21
22#include "FN_lazy_function.hh"
23
24namespace blender::dot {
25class DirectedEdge;
26}
27
29
30class Socket;
31class InputSocket;
32class OutputSocket;
33class Node;
34class Graph;
35
41 protected:
49 const CPPType *type_;
62
63 friend Graph;
64
65 public:
66 bool is_input() const;
67 bool is_output() const;
68
69 int index() const;
70 int index_in_graph() const;
71
74 const InputSocket &as_input() const;
75 const OutputSocket &as_output() const;
76
77 const Node &node() const;
78 Node &node();
79
80 const CPPType &type() const;
81
82 std::string name() const;
83 std::string detailed_name() const;
84};
85
86class InputSocket : public Socket {
87 private:
93 OutputSocket *origin_;
102 const void *default_value_ = nullptr;
103
104 friend Graph;
105
106 public:
108 const OutputSocket *origin() const;
109
110 const void *default_value() const;
111 void set_default_value(const void *value);
112};
113
114class OutputSocket : public Socket {
115 private:
119 Vector<InputSocket *> targets_;
120
121 friend Graph;
122
123 public:
126};
127
132 protected:
137 const LazyFunction *fn_ = nullptr;
153
154 friend Graph;
155
156 public:
157 bool is_interface() const;
158 bool is_function() const;
159 int index_in_graph() const;
160
165
166 const InputSocket &input(int index) const;
167 const OutputSocket &output(int index) const;
168 InputSocket &input(int index);
169 OutputSocket &output(int index);
170
171 std::string name() const;
172};
173
177class FunctionNode final : public Node {
178 public:
179 const LazyFunction &function() const;
180};
181
186class InterfaceNode final : public Node {
187 private:
188 friend Node;
189 friend Socket;
190 friend Graph;
191
192 Vector<std::string> socket_names_;
193};
194
201
206 private:
210 LinearAllocator<> allocator_;
214 StringRefNull name_;
219 Vector<Node *> nodes_;
220
221 InterfaceNode *graph_input_node_ = nullptr;
222 InterfaceNode *graph_output_node_ = nullptr;
223
224 Vector<GraphInputSocket *> graph_inputs_;
225 Vector<GraphOutputSocket *> graph_outputs_;
226
231 int socket_num_ = 0;
232
233 public:
234 Graph(StringRef name = "unkown");
235 ~Graph();
236
237 StringRefNull name() const;
238
244
247
250
253
258
262 GraphInputSocket &add_input(const CPPType &type, std::string name = "");
263 GraphOutputSocket &add_output(const CPPType &type, std::string name = "");
264
269 void add_link(OutputSocket &from, InputSocket &to);
270
274 void clear_origin(InputSocket &socket);
275
279 void update_node_indices();
284
288 int socket_num() const;
289
293 bool node_indices_are_valid() const;
294
300 public:
301 virtual std::string socket_name(const Socket &socket) const;
302 virtual std::optional<std::string> socket_font_color(const Socket &socket) const;
303 virtual void add_edge_attributes(const OutputSocket &from,
304 const InputSocket &to,
305 dot::DirectedEdge &dot_edge) const;
306 };
307
311 std::string to_dot(const ToDotOptions &options = {}) const;
312};
313
314/* -------------------------------------------------------------------- */
318inline bool Socket::is_input() const
319{
320 return is_input_;
321}
322
323inline bool Socket::is_output() const
324{
325 return !is_input_;
326}
327
328inline int Socket::index() const
329{
330 return index_in_node_;
331}
332
333inline int Socket::index_in_graph() const
334{
335 return index_in_graph_;
336}
337
339{
340 BLI_assert(this->is_input());
341 return *static_cast<InputSocket *>(this);
342}
343
345{
346 BLI_assert(this->is_output());
347 return *static_cast<OutputSocket *>(this);
348}
349
350inline const InputSocket &Socket::as_input() const
351{
352 BLI_assert(this->is_input());
353 return *static_cast<const InputSocket *>(this);
354}
355
356inline const OutputSocket &Socket::as_output() const
357{
358 BLI_assert(this->is_output());
359 return *static_cast<const OutputSocket *>(this);
360}
361
362inline const Node &Socket::node() const
363{
364 return *node_;
365}
366
368{
369 return *node_;
370}
371
372inline const CPPType &Socket::type() const
373{
374 return *type_;
375}
376
379/* -------------------------------------------------------------------- */
384{
385 return origin_;
386}
387
389{
390 return origin_;
391}
392
393inline const void *InputSocket::default_value() const
394{
395 return default_value_;
396}
397
398inline void InputSocket::set_default_value(const void *value)
399{
400 default_value_ = value;
401}
402
405/* -------------------------------------------------------------------- */
410{
411 return targets_;
412}
413
415{
416 return targets_;
417}
418
421/* -------------------------------------------------------------------- */
425inline bool Node::is_interface() const
426{
427 return fn_ == nullptr;
428}
429
430inline bool Node::is_function() const
431{
432 return fn_ != nullptr;
433}
434
435inline int Node::index_in_graph() const
436{
437 return index_in_graph_;
438}
439
441{
442 return inputs_;
443}
444
446{
447 return outputs_;
448}
449
451{
452 return inputs_;
453}
454
456{
457 return outputs_;
458}
459
460inline const InputSocket &Node::input(const int index) const
461{
462 return *inputs_[index];
463}
464
465inline const OutputSocket &Node::output(const int index) const
466{
467 return *outputs_[index];
468}
469
470inline InputSocket &Node::input(const int index)
471{
472 return *inputs_[index];
473}
474
475inline OutputSocket &Node::output(const int index)
476{
477 return *outputs_[index];
478}
479
482/* -------------------------------------------------------------------- */
487{
488 BLI_assert(fn_ != nullptr);
489 return *fn_;
490}
491
494/* -------------------------------------------------------------------- */
499{
500 return name_;
501}
502
504{
505 return nodes_;
506}
507
509{
510 return nodes_;
511}
512
514{
515 return nodes_.as_span().drop_front(2).cast<const FunctionNode *>();
516}
517
519{
520 return nodes_.as_span().drop_front(2).cast<FunctionNode *>();
521}
522
524{
525 return graph_inputs_;
526}
527
529{
530 return graph_outputs_;
531}
532
534{
535 return graph_inputs_;
536}
537
539{
540 return graph_outputs_;
541}
542
543inline int Graph::socket_num() const
544{
545 return socket_num_;
546}
547
550} // namespace blender::fn::lazy_function
#define BLI_assert(a)
Definition BLI_assert.h:50
#define output
Span< T > as_span() const
virtual std::string socket_name(const Socket &socket) const
virtual void add_edge_attributes(const OutputSocket &from, const InputSocket &to, dot::DirectedEdge &dot_edge) const
virtual std::optional< std::string > socket_font_color(const Socket &socket) const
Span< GraphInputSocket * > graph_inputs()
Span< const FunctionNode * > function_nodes() const
FunctionNode & add_function(const LazyFunction &fn)
void add_link(OutputSocket &from, InputSocket &to)
Span< GraphOutputSocket * > graph_outputs()
GraphOutputSocket & add_output(const CPPType &type, std::string name="")
void clear_origin(InputSocket &socket)
std::string to_dot(const ToDotOptions &options={}) const
GraphInputSocket & add_input(const CPPType &type, std::string name="")
Span< const InputSocket * > inputs() const
Span< const OutputSocket * > outputs() const
const InputSocket & input(int index) const
const OutputSocket & output(int index) const
CCL_NAMESPACE_BEGIN struct Options options