In case of running out of prefetch pipes, truncate the sample to the preloaded part.
[calfbox.git] / dom.h
blob9381c16fcece3b27b1707133b413b7071620d228
1 /*
2 Calf Box, an open source musical instrument.
3 Copyright (C) 2012 Krzysztof Foltman
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef CBOX_DOM_H
20 #define CBOX_DOM_H
22 #include <glib.h>
23 #include <stdint.h>
24 #include <uuid/uuid.h>
26 struct cbox_command_target;
27 struct cbox_osc_command;
28 struct cbox_objhdr;
29 struct cbox_document;
30 struct GList;
32 struct cbox_uuid
34 uuid_t uuid;
37 extern guint cbox_uuid_hash(gconstpointer v);
38 extern gboolean cbox_uuid_equal(gconstpointer v1, gconstpointer v2);
39 extern gboolean cbox_uuid_report(struct cbox_uuid *uuid, struct cbox_command_target *fb, GError **error);
40 extern gboolean cbox_uuid_report_as(struct cbox_uuid *uuid, const char *cmd, struct cbox_command_target *fb, GError **error);
41 extern gboolean cbox_uuid_fromstring(struct cbox_uuid *uuid, const char *str, GError **error);
42 extern void cbox_uuid_tostring(struct cbox_uuid *uuid, char str[40]);
43 extern void cbox_uuid_generate(struct cbox_uuid *uuid);
45 struct cbox_class
47 struct cbox_class *parent;
48 const char *name;
49 int hdr_offset;
50 void (*destroyfunc)(struct cbox_objhdr *objhdr);
51 struct cbox_command_target *(*getcmdtargetfunc)(struct cbox_objhdr *objhdr);
54 extern struct cbox_class *cbox_class_find_by_name(const char *name);
55 extern void cbox_class_register(struct cbox_class *class_ptr);
57 struct cbox_objhdr
59 struct cbox_class *class_ptr;
60 struct cbox_document *owner;
61 void *link_in_document;
62 struct cbox_uuid instance_uuid;
63 uint64_t stamp;
66 inline int cbox_class_is_a(const struct cbox_class *c1, const struct cbox_class *c2)
68 while(c1 != c2 && c1->parent)
69 c1 = c1->parent;
70 return c1 == c2;
73 extern void cbox_object_register_instance(struct cbox_document *doc, struct cbox_objhdr *obj);
74 extern struct cbox_command_target *cbox_object_get_cmd_target(struct cbox_objhdr *hdr_ptr);
75 extern gboolean cbox_object_try_default_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, const char *subcmd, gboolean *result, GError **error);
76 extern gboolean cbox_object_default_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error);
77 extern gboolean cbox_object_default_status(struct cbox_objhdr *objhdr, struct cbox_command_target *fb, GError **error);
79 extern void cbox_object_destroy(struct cbox_objhdr *hdr_ptr);
81 extern struct cbox_document *cbox_document_new(void);
82 extern void cbox_document_dump(struct cbox_document *);
83 extern struct cbox_command_target *cbox_document_get_cmd_target(struct cbox_document *);
84 extern struct cbox_objhdr *cbox_document_get_service(struct cbox_document *doc, const char *name);
85 extern void cbox_document_set_service(struct cbox_document *doc, const char *name, struct cbox_objhdr *hdr_ptr);
86 extern struct cbox_objhdr *cbox_document_get_object_by_uuid(struct cbox_document *doc, const struct cbox_uuid *uuid);
87 extern struct cbox_objhdr *cbox_document_get_object_by_text_uuid(struct cbox_document *doc, const char *uuid, const struct cbox_class *class_ptr, GError **error);
88 extern uint64_t cbox_document_get_next_stamp(struct cbox_document *doc);
89 extern void cbox_document_destroy(struct cbox_document *);
91 extern void cbox_dom_init(void);
92 extern void cbox_dom_close(void);
94 // must be the first field in the object-compatible struct
95 #define CBOX_OBJECT_HEADER() \
96 struct cbox_objhdr _obj_hdr;
98 #define CBOX_CLASS(class) CBOX_CLASS_##class
100 #define CBOX_EXTERN_CLASS(class) \
101 extern struct cbox_class CBOX_CLASS(class);
103 #define CBOX_GET_DOCUMENT(obj) \
104 ((obj)->_obj_hdr.owner)
106 #define CBOX_STAMP(obj) \
107 ((obj)->_obj_hdr.stamp = cbox_document_get_next_stamp(CBOX_GET_DOCUMENT(obj)))
109 #define CBOX_DELETE(obj) \
110 ((obj) && (cbox_object_destroy(&(obj)->_obj_hdr), 1))
112 #define CBOX_IS_A(obj, class) \
113 ((obj) && cbox_class_is_a((obj)->_obj_hdr.class_ptr, &CBOX_CLASS(class)))
115 #define CBOX_OBJECT_HEADER_INIT(self, class, document) \
116 do { \
117 (self)->_obj_hdr.class_ptr = &CBOX_CLASS_##class; \
118 (self)->_obj_hdr.owner = (document); \
119 (self)->_obj_hdr.link_in_document = NULL; \
120 (self)->_obj_hdr.stamp = cbox_document_get_next_stamp(document); \
121 uuid_generate((self)->_obj_hdr.instance_uuid.uuid); \
122 } while(0)
124 #define CBOX_OBJECT_REGISTER(self) \
125 (cbox_object_register_instance((self)->_obj_hdr.owner, &(self)->_obj_hdr))
127 #define CBOX_OBJECT_DEFAULT_STATUS(self, fb, error) \
128 (cbox_object_default_status(&(self)->_obj_hdr, (fb), (error)))
130 #define CBOX_CLASS_DEFINITION_ROOT(class) \
131 static void class##_destroyfunc(struct cbox_objhdr *hdr_ptr); \
132 static struct cbox_command_target *class##_getcmdtarget(struct cbox_objhdr *hdr) { \
133 return &(((struct class *)hdr)->cmd_target);\
134 }; \
135 struct cbox_class CBOX_CLASS_##class = { \
136 .parent = NULL, \
137 .name = #class, \
138 .hdr_offset = offsetof(struct class, _obj_hdr), \
139 .destroyfunc = class##_destroyfunc, \
140 .getcmdtargetfunc = class##_getcmdtarget \
141 }; \
143 #define CBOX_RETURN_OBJECT(result) \
144 return &(result)->_obj_hdr
146 // Convert header to object, regardless of the relative position of the header.
147 #define CBOX_H2O(hdr) \
148 (void *)(((char *)(hdr)) - (hdr)->class_ptr->hdr_offset)
150 #define CBOX_O2H(obj) \
151 (&(*(obj))._obj_hdr)
153 #endif