chris@162
|
1 /**
|
chris@162
|
2 * Copyright (c) 2014, 2015, Enzien Audio Ltd.
|
chris@162
|
3 *
|
chris@162
|
4 * Permission to use, copy, modify, and/or distribute this software for any
|
chris@162
|
5 * purpose with or without fee is hereby granted, provided that the above
|
chris@162
|
6 * copyright notice and this permission notice appear in all copies.
|
chris@162
|
7 *
|
chris@162
|
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
chris@162
|
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
chris@162
|
10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
chris@162
|
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
chris@162
|
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
chris@162
|
13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
chris@162
|
14 * PERFORMANCE OF THIS SOFTWARE.
|
chris@162
|
15 */
|
chris@162
|
16
|
chris@162
|
17 #ifndef _HEAVY_MESSAGE_H_
|
chris@162
|
18 #define _HEAVY_MESSAGE_H_
|
chris@162
|
19
|
chris@162
|
20 #include "Utils.h"
|
chris@162
|
21
|
chris@162
|
22 typedef enum ElementType {
|
chris@162
|
23 BANG,
|
chris@162
|
24 FLOAT,
|
chris@162
|
25 SYMBOL,
|
chris@162
|
26 HASH
|
chris@162
|
27 } ElementType;
|
chris@162
|
28
|
chris@162
|
29 typedef struct Element {
|
chris@162
|
30 ElementType type;
|
chris@162
|
31 union {
|
chris@162
|
32 float f; // float
|
chris@162
|
33 char *s; // symbol
|
chris@162
|
34 hv_uint32_t h; // hash
|
chris@162
|
35 } data;
|
chris@162
|
36 } Element;
|
chris@162
|
37
|
chris@162
|
38 typedef struct HvMessage {
|
chris@162
|
39 hv_uint32_t timestamp; // the sample at which this message should be processed
|
chris@162
|
40 hv_uint16_t numElements;
|
chris@162
|
41 hv_uint16_t numBytes; // the number of bytes that this message occupies in memory
|
chris@162
|
42 Element elem;
|
chris@162
|
43 } HvMessage;
|
chris@162
|
44
|
chris@162
|
45 #define HV_MESSAGE_ON_STACK(_x) (HvMessage *) hv_alloca(msg_getByteSize(_x))
|
chris@162
|
46
|
chris@162
|
47 /** Returns the total length in bytes of this message for a given number of elements. */
|
chris@162
|
48 static inline hv_size_t msg_getByteSize(hv_size_t numElements) {
|
chris@162
|
49 hv_assert(numElements > 0);
|
chris@162
|
50 return sizeof(HvMessage) + ((numElements-1) * sizeof(Element));
|
chris@162
|
51 }
|
chris@162
|
52
|
chris@162
|
53 HvMessage *msg_copy(const HvMessage *m);
|
chris@162
|
54
|
chris@162
|
55 /** Returns the number of bytes that this message would occupy on the heap. */
|
chris@162
|
56 hv_size_t msg_getNumHeapBytes(const HvMessage *m);
|
chris@162
|
57
|
chris@162
|
58 /** Copies the message into the given buffer. The buffer must be at least as large as msg_getNumHeapBytes(). */
|
chris@162
|
59 void msg_copyToBuffer(const HvMessage *m, char *buffer, hv_size_t len);
|
chris@162
|
60
|
chris@162
|
61 void msg_setElementToFrom(HvMessage *n, int indexN, const HvMessage *const m, int indexM);
|
chris@162
|
62
|
chris@162
|
63 /** Frees a message on the heap. Does nothing if argument is NULL. */
|
chris@162
|
64 void msg_free(HvMessage *m);
|
chris@162
|
65
|
chris@162
|
66 HvMessage *msg_init(HvMessage *m, hv_size_t numElements, hv_uint32_t timestamp);
|
chris@162
|
67
|
chris@162
|
68 HvMessage *msg_initWithFloat(HvMessage *m, hv_uint32_t timestamp, float f);
|
chris@162
|
69
|
chris@162
|
70 HvMessage *msg_initWithBang(HvMessage *m, hv_uint32_t timestamp);
|
chris@162
|
71
|
chris@162
|
72 HvMessage *msg_initWithSymbol(HvMessage *m, hv_uint32_t timestamp, char *s);
|
chris@162
|
73
|
chris@162
|
74 HvMessage *msg_initWithHash(HvMessage *m, hv_uint32_t timestamp, hv_uint32_t h);
|
chris@162
|
75
|
chris@162
|
76 HvMessage *msg_initV(HvMessage *const m, const hv_uint32_t timestamp, const char *format, ...);
|
chris@162
|
77
|
chris@162
|
78 static inline hv_uint32_t msg_getTimestamp(const HvMessage *m) {
|
chris@162
|
79 return m->timestamp;
|
chris@162
|
80 }
|
chris@162
|
81
|
chris@162
|
82 static inline void msg_setTimestamp(HvMessage *m, hv_uint32_t timestamp) {
|
chris@162
|
83 m->timestamp = timestamp;
|
chris@162
|
84 }
|
chris@162
|
85
|
chris@162
|
86 static inline int msg_getNumElements(const HvMessage *m) {
|
chris@162
|
87 return (int) m->numElements;
|
chris@162
|
88 }
|
chris@162
|
89
|
chris@162
|
90 /** Returns the number of bytes this message in memory. */
|
chris@162
|
91 static inline hv_size_t msg_getNumBytes(const HvMessage *m) {
|
chris@162
|
92 return m->numBytes;
|
chris@162
|
93 }
|
chris@162
|
94
|
chris@162
|
95 static inline ElementType msg_getType(const HvMessage *m, int index) {
|
chris@162
|
96 hv_assert(index < msg_getNumElements(m)); // invalid index
|
chris@162
|
97 return (&(m->elem)+index)->type;
|
chris@162
|
98 }
|
chris@162
|
99
|
chris@162
|
100 static inline void msg_setBang(HvMessage *m, int index) {
|
chris@162
|
101 hv_assert(index < msg_getNumElements(m)); // invalid index
|
chris@162
|
102 (&(m->elem)+index)->type = BANG;
|
chris@162
|
103 (&(m->elem)+index)->data.s = NULL;
|
chris@162
|
104 }
|
chris@162
|
105
|
chris@162
|
106 static inline bool msg_isBang(const HvMessage *m, int index) {
|
chris@162
|
107 return (index < msg_getNumElements(m)) ? (msg_getType(m,index) == BANG) : false;
|
chris@162
|
108 }
|
chris@162
|
109
|
chris@162
|
110 static inline void msg_setFloat(HvMessage *m, int index, float f) {
|
chris@162
|
111 hv_assert(index < msg_getNumElements(m)); // invalid index
|
chris@162
|
112 (&(m->elem)+index)->type = FLOAT;
|
chris@162
|
113 (&(m->elem)+index)->data.f = f;
|
chris@162
|
114 }
|
chris@162
|
115
|
chris@162
|
116 static inline float msg_getFloat(const HvMessage *const m, int index) {
|
chris@162
|
117 hv_assert(index < msg_getNumElements(m)); // invalid index
|
chris@162
|
118 return (&(m->elem)+index)->data.f;
|
chris@162
|
119 }
|
chris@162
|
120
|
chris@162
|
121 static inline bool msg_isFloat(const HvMessage *const m, int index) {
|
chris@162
|
122 return (index < msg_getNumElements(m)) ? (msg_getType(m,index) == FLOAT) : false;
|
chris@162
|
123 }
|
chris@162
|
124
|
chris@162
|
125 static inline void msg_setHash(HvMessage *m, int index, hv_uint32_t h) {
|
chris@162
|
126 hv_assert(index < msg_getNumElements(m)); // invalid index
|
chris@162
|
127 (&(m->elem)+index)->type = HASH;
|
chris@162
|
128 (&(m->elem)+index)->data.h = h;
|
chris@162
|
129 }
|
chris@162
|
130
|
chris@162
|
131 static inline bool msg_isHash(const HvMessage *m, int index) {
|
chris@162
|
132 return (index < msg_getNumElements(m)) ? (msg_getType(m, index) == HASH) : false;
|
chris@162
|
133 }
|
chris@162
|
134
|
chris@162
|
135 /** Returns true if the element is a hash or symbol. False otherwise. */
|
chris@162
|
136 static inline bool msg_isHashLike(const HvMessage *m, int index) {
|
chris@162
|
137 return (index < msg_getNumElements(m)) ? ((msg_getType(m, index) == HASH) || (msg_getType(m, index) == SYMBOL)) : false;
|
chris@162
|
138 }
|
chris@162
|
139
|
chris@162
|
140 /** Returns a 32-bit hash of the given string. */
|
chris@162
|
141 hv_uint32_t msg_symbolToHash(const char *s);
|
chris@162
|
142
|
chris@162
|
143 /** Returns a 32-bit hash of the given element. */
|
chris@162
|
144 hv_uint32_t msg_getHash(const HvMessage *const m, int i);
|
chris@162
|
145
|
chris@162
|
146 static inline void msg_setSymbol(HvMessage *m, int index, char *s) {
|
chris@162
|
147 hv_assert(index < msg_getNumElements(m)); // invalid index
|
chris@162
|
148 (&(m->elem)+index)->type = SYMBOL;
|
chris@162
|
149 (&(m->elem)+index)->data.s = s;
|
chris@162
|
150 }
|
chris@162
|
151
|
chris@162
|
152 static inline char *msg_getSymbol(const HvMessage *m, int index) {
|
chris@162
|
153 hv_assert(index < msg_getNumElements(m)); // invalid index
|
chris@162
|
154 return (&(m->elem)+index)->data.s;
|
chris@162
|
155 }
|
chris@162
|
156
|
chris@162
|
157 static inline bool msg_isSymbol(const HvMessage *m, int index) {
|
chris@162
|
158 return (index < msg_getNumElements(m)) ? (msg_getType(m, index) == SYMBOL) : false;
|
chris@162
|
159 }
|
chris@162
|
160
|
chris@162
|
161 bool msg_compareSymbol(const HvMessage *m, int i, const char *s);
|
chris@162
|
162
|
chris@162
|
163 /** Returns 1 if the element i_m of message m is equal to element i_n of message n. */
|
chris@162
|
164 bool msg_equalsElement(const HvMessage *m, int i_m, const HvMessage *n, int i_n);
|
chris@162
|
165
|
chris@162
|
166 bool msg_hasFormat(const HvMessage *m, const char *fmt);
|
chris@162
|
167
|
chris@162
|
168 /**
|
chris@162
|
169 * Create a string representation of the message. Suitable for use by the print object.
|
chris@162
|
170 * The resulting string must be freed by the caller.
|
chris@162
|
171 */
|
chris@162
|
172 char *msg_toString(const HvMessage *msg);
|
chris@162
|
173
|
chris@162
|
174 /**
|
chris@162
|
175 * Resolves any number of dollar arguments and generates a string based on the arguments.
|
chris@162
|
176 * @param m The message from which to take values
|
chris@162
|
177 * @param n The message to fill in
|
chris@162
|
178 * @param z The element index to resolve
|
chris@162
|
179 * @param buf The scratch (i.e. resolution) buffer
|
chris@162
|
180 * @param len The length of the scratch buffer
|
chris@162
|
181 * @param args A string of 'i' and 's' chars indicating the type of the arguments, either indicies or strings
|
chris@162
|
182 * @param varargs The components to resolve, either dollar indicies or strings.
|
chris@162
|
183 * If the index is negative, the graph id is used
|
chris@162
|
184 * @return true if the resolution buffer is needed, false otherwise.
|
chris@162
|
185 */
|
chris@162
|
186 // bool msg_resolveDollarArguments(HvMessage *m, HvMessage *n, int z, char *buf, hv_size_t len, const char *args, ...);
|
chris@162
|
187
|
chris@162
|
188 #endif // _HEAVY_MESSAGE_H_
|