Add an example demonstrating use of sequencer API.
[calfbox.git] / dom.h
blobaa4749a8f9cce34ebc104529c97be68f042b6c49
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 <uuid/uuid.h>
25 struct cbox_command_target;
26 struct cbox_osc_command;
27 struct cbox_objhdr;
28 struct cbox_document;
29 struct GList;
31 struct cbox_uuid
33 uuid_t uuid;
36 struct cbox_class
38 struct cbox_class *parent;
39 const char *name;
40 int hdr_offset;
41 void (*destroyfunc)(struct cbox_objhdr *objhdr);
42 struct cbox_command_target *(*getcmdtargetfunc)(struct cbox_objhdr *objhdr);
45 extern struct cbox_class *cbox_class_find_by_name(const char *name);
46 extern void cbox_class_register(struct cbox_class *class_ptr);
48 struct cbox_objhdr
50 struct cbox_class *class_ptr;
51 struct cbox_document *owner;
52 void *link_in_document;
53 struct cbox_uuid instance_uuid;
56 inline int cbox_class_is_a(const struct cbox_class *c1, const struct cbox_class *c2)
58 while(c1 != c2 && c1->parent)
59 c1 = c1->parent;
60 return c1 == c2;
63 extern void cbox_object_register_instance(struct cbox_document *doc, struct cbox_objhdr *obj);
64 extern struct cbox_command_target *cbox_object_get_cmd_target(struct cbox_objhdr *hdr_ptr);
65 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);
66 extern gboolean cbox_object_default_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error);
67 extern gboolean cbox_object_default_status(struct cbox_objhdr *objhdr, struct cbox_command_target *fb, GError **error);
69 extern void cbox_object_destroy(struct cbox_objhdr *hdr_ptr);
71 extern struct cbox_document *cbox_document_new();
72 extern void cbox_document_dump(struct cbox_document *);
73 extern struct cbox_command_target *cbox_document_get_cmd_target(struct cbox_document *);
74 extern struct cbox_objhdr *cbox_document_get_service(struct cbox_document *doc, const char *name);
75 extern void cbox_document_set_service(struct cbox_document *doc, const char *name, struct cbox_objhdr *hdr_ptr);
76 extern struct cbox_objhdr *cbox_document_get_object_by_uuid(struct cbox_document *doc, const struct cbox_uuid *uuid);
77 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);
78 extern void cbox_document_destroy(struct cbox_document *);
80 extern void cbox_dom_init();
81 extern void cbox_dom_close();
83 // must be the first field in the object-compatible struct
84 #define CBOX_OBJECT_HEADER() \
85 struct cbox_objhdr _obj_hdr;
87 #define CBOX_CLASS(class) CBOX_CLASS_##class
89 #define CBOX_EXTERN_CLASS(class) \
90 extern struct cbox_class CBOX_CLASS(class);
92 #define CBOX_GET_DOCUMENT(obj) \
93 ((obj)->_obj_hdr.owner)
95 #define CBOX_DELETE(obj) \
96 ((obj) && (cbox_object_destroy(&(obj)->_obj_hdr), 1))
98 #define CBOX_IS_A(obj, class) \
99 ((obj) && cbox_class_is_a((obj)->_obj_hdr.class_ptr, &CBOX_CLASS(class)))
101 #define CBOX_OBJECT_HEADER_INIT(self, class, document) \
102 do { \
103 (self)->_obj_hdr.class_ptr = &CBOX_CLASS_##class; \
104 (self)->_obj_hdr.owner = (document); \
105 (self)->_obj_hdr.link_in_document = NULL; \
106 uuid_generate((self)->_obj_hdr.instance_uuid.uuid); \
107 } while(0)
109 #define CBOX_OBJECT_REGISTER(self) \
110 (cbox_object_register_instance((self)->_obj_hdr.owner, &(self)->_obj_hdr))
112 #define CBOX_OBJECT_DEFAULT_STATUS(self, fb, error) \
113 (cbox_object_default_status(&(self)->_obj_hdr, (fb), (error)))
115 #define CBOX_CLASS_DEFINITION_ROOT(class) \
116 static void class##_destroyfunc(struct cbox_objhdr *hdr_ptr); \
117 static struct cbox_command_target *class##_getcmdtarget(struct cbox_objhdr *hdr) { \
118 return &(((struct class *)hdr)->cmd_target);\
119 }; \
120 struct cbox_class CBOX_CLASS_##class = { \
121 .parent = NULL, \
122 .name = #class, \
123 .hdr_offset = offsetof(struct class, _obj_hdr), \
124 .destroyfunc = class##_destroyfunc, \
125 .getcmdtargetfunc = class##_getcmdtarget \
126 }; \
128 #define CBOX_RETURN_OBJECT(result) \
129 return &(result)->_obj_hdr
131 // Convert header to object, regardless of the relative position of the header.
132 #define CBOX_H2O(hdr) \
133 (void *)(((char *)(hdr)) - (hdr)->class_ptr->hdr_offset)
135 #define CBOX_O2H(obj) \
136 (&(*(obj))._obj_hdr)
138 #endif