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