scons --> make
[aftubes.git] / graph.h
blob7e5b7c193e7ba2a7cc3f4b0f0138cd658fdf08bf
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
12 ECANNOT_GUESS_FORMAT, // cannot guess a node's output format
13 EBAD_FORMAT, // bad format
16 enum direction {
17 DIR_IN,
18 DIR_OUT,
21 struct graphnode;
22 struct graphpin;
23 struct graphedge;
25 struct graphnode_functab {
26 bool (* is_acceptable_input_format)(struct graphnode *node, struct graphpin *pin, const struct aformat *af);
27 err_t (* get_ideal_input_format)(struct graphnode *node, struct graphpin *pin, struct aformat *af);
28 err_t (* get_output_format)(struct graphnode *node, struct graphpin *pin, struct aformat *af);
29 err_t (* set_buffer)(struct graphnode *node, struct graphpin *pin, struct buffer *buf);
30 err_t (* run)(struct graphnode *node);
33 struct graphedge {
34 struct graphpin *a, *b;
35 struct buffer buf;
36 bool processed;
39 struct graphpin {
40 struct graphpin *next;
41 struct graphnode *node;
42 struct graphedge *edge;
43 const char *name;
44 enum direction dir;
45 int tag; // user-defined value
48 struct graphnode {
49 const struct graphnode_functab *functab;
50 struct graphnode *prev, *next; // nodes in a list (doesn't relate to data flow)
51 struct graphnode *sorted_prev,
52 *sorted_next; // nodes in topological order (the order to run them in)
53 struct graphpin *pins;
54 const char *name; // name (for debugging, atm)
55 void *extra; // pointer to additional data, if needed
56 char extra_data[];
59 struct graph {
60 struct graphnode *node_first, *node_last,
61 *sorted_nodes;
64 struct graphpin *graphnode_get_pin(struct graphnode *node, enum direction dir); // return the first pin of direction 'dir'
65 err_t graphnode_add_pin(struct graphnode *node, struct graphpin **pin_out); // create a new pin for the node
66 void graphnode_remove_pin(struct graphnode *node, struct graphpin *pin); // remove a pin from this node
68 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)
69 void graphnode_free(struct graphnode *node); // destroy a node
70 bool graphnode_is_source(struct graphnode *node); // returns true if node is a source (no inputs)
71 bool graphnode_all_inputs_connected(struct graphnode *node); // returns true if ... well, have a guess!
73 err_t graph_create(struct graph *graph); // create a new graph
74 void graph_destroy(struct graph *graph); // destroy a graph and its contents
75 err_t graph_add_node(struct graph *graph, struct graphnode *node); // add a node to the graph (doesn't connect anything - just adds it)
76 err_t graph_remove_node(struct graph *graph, struct graphnode *node); // removes a node from the graph (doesn't destroy the node)
78 // Make connections. Pins a and b must be unconnected.
79 // Pin a must be an output, pin b must be an input.
80 err_t graph_connect(struct graph *graph, struct graphpin *a, struct graphpin *b);
82 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)
83 err_t graph_run(struct graph *graph); // run the graph! (actually does stuff)
85 #endif