chris@160: /** chris@160: * Copyright (c) 2014, 2015, Enzien Audio Ltd. chris@160: * chris@160: * Permission to use, copy, modify, and/or distribute this software for any chris@160: * purpose with or without fee is hereby granted, provided that the above chris@160: * copyright notice and this permission notice appear in all copies. chris@160: * chris@160: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH chris@160: * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY chris@160: * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, chris@160: * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM chris@160: * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR chris@160: * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR chris@160: * PERFORMANCE OF THIS SOFTWARE. chris@160: */ chris@160: chris@160: #ifndef _HEAVY_MESSAGE_H_ chris@160: #define _HEAVY_MESSAGE_H_ chris@160: chris@160: #include "Utils.h" chris@160: chris@160: typedef enum ElementType { chris@160: BANG, chris@160: FLOAT, chris@160: SYMBOL, chris@160: HASH chris@160: } ElementType; chris@160: chris@160: typedef struct Element { chris@160: ElementType type; chris@160: union { chris@160: float f; // float chris@160: char *s; // symbol chris@160: hv_uint32_t h; // hash chris@160: } data; chris@160: } Element; chris@160: chris@160: typedef struct HvMessage { chris@160: hv_uint32_t timestamp; // the sample at which this message should be processed chris@160: hv_uint16_t numElements; chris@160: hv_uint16_t numBytes; // the number of bytes that this message occupies in memory chris@160: Element elem; chris@160: } HvMessage; chris@160: chris@160: #define HV_MESSAGE_ON_STACK(_x) (HvMessage *) hv_alloca(msg_getByteSize(_x)) chris@160: chris@160: /** Returns the total length in bytes of this message for a given number of elements. */ chris@160: static inline hv_size_t msg_getByteSize(hv_size_t numElements) { chris@160: hv_assert(numElements > 0); chris@160: return sizeof(HvMessage) + ((numElements-1) * sizeof(Element)); chris@160: } chris@160: chris@160: HvMessage *msg_copy(const HvMessage *m); chris@160: chris@160: /** Returns the number of bytes that this message would occupy on the heap. */ chris@160: hv_size_t msg_getNumHeapBytes(const HvMessage *m); chris@160: chris@160: /** Copies the message into the given buffer. The buffer must be at least as large as msg_getNumHeapBytes(). */ chris@160: void msg_copyToBuffer(const HvMessage *m, char *buffer, hv_size_t len); chris@160: chris@160: void msg_setElementToFrom(HvMessage *n, int indexN, const HvMessage *const m, int indexM); chris@160: chris@160: /** Frees a message on the heap. Does nothing if argument is NULL. */ chris@160: void msg_free(HvMessage *m); chris@160: chris@160: HvMessage *msg_init(HvMessage *m, hv_size_t numElements, hv_uint32_t timestamp); chris@160: chris@160: HvMessage *msg_initWithFloat(HvMessage *m, hv_uint32_t timestamp, float f); chris@160: chris@160: HvMessage *msg_initWithBang(HvMessage *m, hv_uint32_t timestamp); chris@160: chris@160: HvMessage *msg_initWithSymbol(HvMessage *m, hv_uint32_t timestamp, char *s); chris@160: chris@160: HvMessage *msg_initWithHash(HvMessage *m, hv_uint32_t timestamp, hv_uint32_t h); chris@160: chris@160: HvMessage *msg_initV(HvMessage *const m, const hv_uint32_t timestamp, const char *format, ...); chris@160: chris@160: static inline hv_uint32_t msg_getTimestamp(const HvMessage *m) { chris@160: return m->timestamp; chris@160: } chris@160: chris@160: static inline void msg_setTimestamp(HvMessage *m, hv_uint32_t timestamp) { chris@160: m->timestamp = timestamp; chris@160: } chris@160: chris@160: static inline int msg_getNumElements(const HvMessage *m) { chris@160: return (int) m->numElements; chris@160: } chris@160: chris@160: /** Returns the number of bytes this message in memory. */ chris@160: static inline hv_size_t msg_getNumBytes(const HvMessage *m) { chris@160: return m->numBytes; chris@160: } chris@160: chris@160: static inline ElementType msg_getType(const HvMessage *m, int index) { chris@160: hv_assert(index < msg_getNumElements(m)); // invalid index chris@160: return (&(m->elem)+index)->type; chris@160: } chris@160: chris@160: static inline void msg_setBang(HvMessage *m, int index) { chris@160: hv_assert(index < msg_getNumElements(m)); // invalid index chris@160: (&(m->elem)+index)->type = BANG; chris@160: (&(m->elem)+index)->data.s = NULL; chris@160: } chris@160: chris@160: static inline bool msg_isBang(const HvMessage *m, int index) { chris@160: return (index < msg_getNumElements(m)) ? (msg_getType(m,index) == BANG) : false; chris@160: } chris@160: chris@160: static inline void msg_setFloat(HvMessage *m, int index, float f) { chris@160: hv_assert(index < msg_getNumElements(m)); // invalid index chris@160: (&(m->elem)+index)->type = FLOAT; chris@160: (&(m->elem)+index)->data.f = f; chris@160: } chris@160: chris@160: static inline float msg_getFloat(const HvMessage *const m, int index) { chris@160: hv_assert(index < msg_getNumElements(m)); // invalid index chris@160: return (&(m->elem)+index)->data.f; chris@160: } chris@160: chris@160: static inline bool msg_isFloat(const HvMessage *const m, int index) { chris@160: return (index < msg_getNumElements(m)) ? (msg_getType(m,index) == FLOAT) : false; chris@160: } chris@160: chris@160: static inline void msg_setHash(HvMessage *m, int index, hv_uint32_t h) { chris@160: hv_assert(index < msg_getNumElements(m)); // invalid index chris@160: (&(m->elem)+index)->type = HASH; chris@160: (&(m->elem)+index)->data.h = h; chris@160: } chris@160: chris@160: static inline bool msg_isHash(const HvMessage *m, int index) { chris@160: return (index < msg_getNumElements(m)) ? (msg_getType(m, index) == HASH) : false; chris@160: } chris@160: chris@160: /** Returns true if the element is a hash or symbol. False otherwise. */ chris@160: static inline bool msg_isHashLike(const HvMessage *m, int index) { chris@160: return (index < msg_getNumElements(m)) ? ((msg_getType(m, index) == HASH) || (msg_getType(m, index) == SYMBOL)) : false; chris@160: } chris@160: chris@160: /** Returns a 32-bit hash of the given string. */ chris@160: hv_uint32_t msg_symbolToHash(const char *s); chris@160: chris@160: /** Returns a 32-bit hash of the given element. */ chris@160: hv_uint32_t msg_getHash(const HvMessage *const m, int i); chris@160: chris@160: static inline void msg_setSymbol(HvMessage *m, int index, char *s) { chris@160: hv_assert(index < msg_getNumElements(m)); // invalid index chris@160: (&(m->elem)+index)->type = SYMBOL; chris@160: (&(m->elem)+index)->data.s = s; chris@160: } chris@160: chris@160: static inline char *msg_getSymbol(const HvMessage *m, int index) { chris@160: hv_assert(index < msg_getNumElements(m)); // invalid index chris@160: return (&(m->elem)+index)->data.s; chris@160: } chris@160: chris@160: static inline bool msg_isSymbol(const HvMessage *m, int index) { chris@160: return (index < msg_getNumElements(m)) ? (msg_getType(m, index) == SYMBOL) : false; chris@160: } chris@160: chris@160: bool msg_compareSymbol(const HvMessage *m, int i, const char *s); chris@160: chris@160: /** Returns 1 if the element i_m of message m is equal to element i_n of message n. */ chris@160: bool msg_equalsElement(const HvMessage *m, int i_m, const HvMessage *n, int i_n); chris@160: chris@160: bool msg_hasFormat(const HvMessage *m, const char *fmt); chris@160: chris@160: /** chris@160: * Create a string representation of the message. Suitable for use by the print object. chris@160: * The resulting string must be freed by the caller. chris@160: */ chris@160: char *msg_toString(const HvMessage *msg); chris@160: chris@160: /** chris@160: * Resolves any number of dollar arguments and generates a string based on the arguments. chris@160: * @param m The message from which to take values chris@160: * @param n The message to fill in chris@160: * @param z The element index to resolve chris@160: * @param buf The scratch (i.e. resolution) buffer chris@160: * @param len The length of the scratch buffer chris@160: * @param args A string of 'i' and 's' chars indicating the type of the arguments, either indicies or strings chris@160: * @param varargs The components to resolve, either dollar indicies or strings. chris@160: * If the index is negative, the graph id is used chris@160: * @return true if the resolution buffer is needed, false otherwise. chris@160: */ chris@160: // bool msg_resolveDollarArguments(HvMessage *m, HvMessage *n, int z, char *buf, hv_size_t len, const char *args, ...); chris@160: chris@160: #endif // _HEAVY_MESSAGE_H_