Initialized graphpin fields
[aftubes.git] / graph.h
blobf383fb1bff12fa937b042387e65b4d13ae82a405
1 #ifndef GRAPH_H
2 #define GRAPH_H
4 #include "stdbool.h"
5 #include "buffer.h"
6 #include "errors.h"
8 enum {
9 EPINDIR = ERR_GROUP(7), // incorrect pin directions
10 EUNCONNECTED, // there are unconnected pins
11 ENO_AGREEABLE_FORMAT, // no format that both nodes will agree on
14 enum direction {
15 DIR_IN,
16 DIR_OUT,
19 struct graphnode;
20 struct graphpin;
21 struct graphedge;
23 struct graphnode_functab {
24 bool (* is_acceptable_input_format)(struct graphnode *node, struct graphpin *pin,
25 const struct aformat *af);
26 err_t (* get_output_format)(struct graphnode *node, struct graphpin *pin,
27 struct aformat *af);
28 err_t (* set_buffer)(struct graphnode *node, struct graphpin *pin, struct buffer *buf);
29 err_t (* run)(struct graphnode *node);
32 struct graphedge {
33 struct graphpin *a, *b;
34 struct buffer buf;
35 bool processed;
38 struct graphpin {
39 struct graphpin *next;
40 struct graphnode *node;
41 struct graphedge *edge;
42 const char *name;
43 enum direction dir;
44 int tag; // user-defined value
47 struct graphnode {
48 const struct graphnode_functab *functab;
49 struct graphnode *prev, *next; // nodes in a list (doesn't relate to data flow)
50 struct graphnode *sorted_prev,
51 *sorted_next; // nodes in topological order (the order to run them in)
52 struct graphpin *pins;
53 const char *name; // name (for debugging, atm)
54 void *extra; // pointer to additional data, if needed
55 char extra_data[];
58 struct graph {
59 struct graphnode *node_first, *node_last,
60 *sorted_nodes;
63 err_t graphnode_add_pin(struct graphnode *node, struct graphpin **pin_out); // create a new pin for the node
64 void graphnode_remove_pin(struct graphnode *node, struct graphpin *pin); // remove a pin from this node
66 err_t graphnode_create(struct graphnode **node_out, const struct graphnode_functab *functab, size_t extra_bytes); // create new node (with extra_bytes additional space)
67 void graphnode_free(struct graphnode *node); // destroy a node
68 bool graphnode_is_source(struct graphnode *node); // returns true if node is a source (no inputs)
69 bool graphnode_all_inputs_connected(struct graphnode *node); // returns true if ... well, have a guess!
71 err_t graph_create(struct graph *graph); // create a new graph
72 void graph_destroy(struct graph *graph); // destroy a graph and its contents
73 err_t graph_add_node(struct graph *graph, struct graphnode *node); // add a node to the graph (doesn't connect anything - just adds it)
74 err_t graph_remove_node(struct graph *graph, struct graphnode *node); // removes a node from the graph (doesn't destroy the node)
76 // Make connections. Pins a and b must be unconnected.
77 // Pin a must be an output, pin b must be an input.
78 err_t graph_connect(struct graph *graph, struct graphpin *a, struct graphpin *b);
80 err_t graph_sort(struct graph *graph); // does a topological sort of graph nodes (must be called before running the graph, after the graph has changed)
81 err_t graph_run(struct graph *graph); // run the graph! (actually does stuff)
83 #endif