O2 2.0
A communication protocol for interactive music and media applications.
hashnode.h
1/* hashnode.h -- hash node implementation */
2
3/* Roger B. Dannenberg
4 * April 2020
5 */
6
7#ifndef HASHNODE_H
8#define HASHNODE_H
9
10#define NODE_HASH 10 // tag for hash_node
11#define NODE_HANDLER 11 // tag for handler_entry
12#define NODE_SERVICES 12 // tag for services_entry
13#define NODE_EMPTY 13 // tag that redirects to full path table
14// see also the tag values in o2_net.h for o2n_info_ptr's
15
16
21// The structure of an entry in hash table. o2_node
22// subclasses are:
23// hash_node,
24// handler_entry,
25// services_entry,
26// osc_info,
27// bridge_inst
28// Any o2_node can be an entry in a hash table, so hash tables
29// can be used to form trees with named links, i.e. path trees
30// for O2 address search.
31typedef struct o2_node {
32 int tag;
33 o2string key; // key is "owned" by this generic entry struct
34 struct o2_node *next;
36
37
38// Hash table node, another hash table
39typedef struct hash_node { // "subclass" of o2_node
40 int tag; // must be NODE_HASH
41 o2string key; // key is "owned" by this hash_node struct
42 o2_node_ptr next;
43 int num_children;
44 Vec<O2node *> children;
45 // At the top level, all children are services_entry_ptrs (tag
46 // NODE_SERVICES). Below that level children can have tags NODE_HASH
47 // or NODE_HANDLER.
49
50#ifdef O2_NO_DEBUG
51#define TO_HASH_NODE(node) ((hash_node_ptr) node)
52#else
53#define TO_HASH_NODE(node) (assert((node)->tag == NODE_HASH), \
54 (hash_node_ptr) (node))
55#endif
56
57// To enumerate elements of a hash table (at one level), use this
58// structure and see o2_enumerate_begin(), o2_enumerate_next()
59typedef struct enumerate {
60 dyn_array_ptr dict;
61 int index;
62 o2_node_ptr entry;
64
65
66// Hash table's entry for handler
67typedef struct handler_entry { // "subclass" of o2_node
68 int tag; // must be NODE_HANDLER
69 o2string key; // key is "owned" by this handler_entry struct
70 o2_node_ptr next;
71 o2_method_handler handler;
72 const void *user_data;
73 char *full_path; // this is the key for this entry in the
74 // o2_ctx->full_path_table; it is a copy of the key in the
75 // path_tree table entry, so you should never free this pointer
76 // -- it will be freed when the path_tree table entry is freed.
77 // (Exception: if O2_NO_PATTERNS, there is no path_tree.)
78 o2string type_string;
86
87#ifdef O2_NO_DEBUG
88#define TO_HANDLER_ENTRY((node) ((handler_entry_ptr) node)
89#else
90#define TO_HANDLER_ENTRY(node) (assert(node->tag == NODE_HANDLER), \
91 (handler_entry_ptr) node)
92#endif
93
94
95void o2_enumerate_begin(enumerate_ptr enumerator, dyn_array_ptr dict);
96
97
98o2_node_ptr o2_enumerate_next(enumerate_ptr enumerator);
99
100
101#ifndef O2_NO_DEBUG
102void o2_node_show(o2_node_ptr info, int indent);
103#endif
104
105
106/* compute the size of a string including EOS and padding to next word */
107int o2_strsize(const char *s);
108
109void o2_string_pad(char *dst, const char *src);
110
111o2_err_t o2_add_entry_at(hash_node_ptr node, o2_node_ptr *loc,
112 o2_node_ptr entry);
113
117o2_err_t o2_node_add(hash_node_ptr node, o2_node_ptr entry);
118
122void o2_node_free(o2_node_ptr entry);
123
127hash_node_ptr o2_hash_node_new(const char *key);
128
129hash_node_ptr o2_tree_insert_node(hash_node_ptr node, o2string key);
130
131int o2_remove_hash_entry_by_name(hash_node_ptr node, o2string key);
132
133o2_err_t o2_hash_entry_remove(hash_node_ptr node, o2_node_ptr *child,
134 int resize);
135
136void o2_hash_node_finish(hash_node_ptr node);
137
138o2string o2_heapify(const char *path);
139
149hash_node_ptr o2_node_initialize(hash_node_ptr node, const char *key);
150
159o2_node_ptr *o2_lookup(hash_node_ptr dict, o2string key);
160
161/* Since hash entries can be many things, we delegate the cleanup
162 * to higher levels of abstraction. Think of this as subclasses
163 * overriding the finish method to do the cleanup. Declare them
164 * here in hashnode.h, but implement them in pathtree.c, services.c.
165 */
166void o2_handler_entry_finish(handler_entry_ptr handler);
167#ifndef O2_NO_DEBUG
168void o2_handler_entry_show(handler_entry_ptr handler);
169#endif
170
171#endif /* HASHNODE_H */
172
Definition: vec.h:6
Definition: dynarray.h:5
Definition: hashnode.h:59
Definition: hashnode.h:67
o2string type_string
types expected by handler, or NULL to ignore
Definition: hashnode.h:78
int types_len
the length of type_string
Definition: hashnode.h:79
int coerce_flag
Definition: hashnode.h:80
int parse_args
boolean - send argc and argv to handler?
Definition: hashnode.h:84
Definition: hashnode.h:39
Definition: hashnode.h:31