Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

atom-util.h 11KB

12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. Copyright 2008-2012 David Robillard <http://drobilla.net>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. /**
  15. @file util.h Helper functions for the LV2 Atom extension.
  16. Note these functions are all static inline, do not take their address.
  17. This header is non-normative, it is provided for convenience.
  18. */
  19. #ifndef LV2_ATOM_UTIL_H
  20. #define LV2_ATOM_UTIL_H
  21. #include <stdarg.h>
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include "atom.h"
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #else
  28. # include <stdbool.h>
  29. #endif
  30. /** Pad a size to 64 bits. */
  31. static inline uint32_t
  32. lv2_atom_pad_size(uint32_t size)
  33. {
  34. return (size + 7) & (~7);
  35. }
  36. /** Return the total size of @p atom, including the header. */
  37. static inline uint32_t
  38. lv2_atom_total_size(const LV2_Atom* atom)
  39. {
  40. return sizeof(LV2_Atom) + atom->size;
  41. }
  42. /** Return true iff @p atom is null. */
  43. static inline bool
  44. lv2_atom_is_null(const LV2_Atom* atom)
  45. {
  46. return !atom || (atom->type == 0 && atom->size == 0);
  47. }
  48. /** Return true iff @p a is equal to @p b. */
  49. static inline bool
  50. lv2_atom_equals(const LV2_Atom* a, const LV2_Atom* b)
  51. {
  52. return (a == b) || ((a->type == b->type) &&
  53. (a->size == b->size) &&
  54. !memcmp(a + 1, b + 1, a->size));
  55. }
  56. /**
  57. @name Sequence Iterator
  58. @{
  59. */
  60. /** Get an iterator pointing to the first event in a Sequence body. */
  61. static inline LV2_Atom_Event*
  62. lv2_atom_sequence_begin(const LV2_Atom_Sequence_Body* body)
  63. {
  64. return (LV2_Atom_Event*)(body + 1);
  65. }
  66. /** Get an iterator pointing to the end of a Sequence body. */
  67. static inline LV2_Atom_Event*
  68. lv2_atom_sequence_end(const LV2_Atom_Sequence_Body* body, uint32_t size)
  69. {
  70. return (LV2_Atom_Event*)((uint8_t*)body + lv2_atom_pad_size(size));
  71. }
  72. /** Return true iff @p i has reached the end of @p body. */
  73. static inline bool
  74. lv2_atom_sequence_is_end(const LV2_Atom_Sequence_Body* body,
  75. uint32_t size,
  76. LV2_Atom_Event* i)
  77. {
  78. return (uint8_t*)i >= ((uint8_t*)body + size);
  79. }
  80. /** Return an iterator to the element following @p i. */
  81. static inline LV2_Atom_Event*
  82. lv2_atom_sequence_next(const LV2_Atom_Event* i)
  83. {
  84. return (LV2_Atom_Event*)((uint8_t*)i
  85. + sizeof(LV2_Atom_Event)
  86. + lv2_atom_pad_size(i->body.size));
  87. }
  88. /**
  89. A macro for iterating over all events in a Sequence.
  90. @param seq The sequence to iterate over
  91. @param iter The name of the iterator
  92. This macro is used similarly to a for loop (which it expands to), e.g.:
  93. @code
  94. LV2_ATOM_SEQUENCE_FOREACH(sequence, ev) {
  95. // Do something with ev (an LV2_Atom_Event*) here...
  96. }
  97. @endcode
  98. */
  99. #define LV2_ATOM_SEQUENCE_FOREACH(seq, iter) \
  100. for (LV2_Atom_Event* (iter) = lv2_atom_sequence_begin(&(seq)->body); \
  101. !lv2_atom_sequence_is_end(&(seq)->body, (seq)->atom.size, (iter)); \
  102. (iter) = lv2_atom_sequence_next(iter))
  103. /** Like LV2_ATOM_SEQUENCE_FOREACH but for a headerless sequence body. */
  104. #define LV2_ATOM_SEQUENCE_BODY_FOREACH(body, size, iter) \
  105. for (LV2_Atom_Event* (iter) = lv2_atom_sequence_begin(body); \
  106. !lv2_atom_sequence_is_end(body, size, (iter)); \
  107. (iter) = lv2_atom_sequence_next(iter))
  108. /**
  109. @}
  110. @name Tuple Iterator
  111. @{
  112. */
  113. /** Get an iterator pointing to the first element in @p tup. */
  114. static inline LV2_Atom*
  115. lv2_atom_tuple_begin(const LV2_Atom_Tuple* tup)
  116. {
  117. return (LV2_Atom*)(LV2_ATOM_BODY(tup));
  118. }
  119. /** Return true iff @p i has reached the end of @p body. */
  120. static inline bool
  121. lv2_atom_tuple_is_end(const void* body, uint32_t size, LV2_Atom* i)
  122. {
  123. return (uint8_t*)i >= ((uint8_t*)body + size);
  124. }
  125. /** Return an iterator to the element following @p i. */
  126. static inline LV2_Atom*
  127. lv2_atom_tuple_next(const LV2_Atom* i)
  128. {
  129. return (LV2_Atom*)(
  130. (uint8_t*)i + sizeof(LV2_Atom) + lv2_atom_pad_size(i->size));
  131. }
  132. /**
  133. A macro for iterating over all properties of a Tuple.
  134. @param tuple The tuple to iterate over
  135. @param iter The name of the iterator
  136. This macro is used similarly to a for loop (which it expands to), e.g.:
  137. @code
  138. LV2_ATOMO_TUPLE_FOREACH(tuple, elem) {
  139. // Do something with elem (an LV2_Atom*) here...
  140. }
  141. @endcode
  142. */
  143. #define LV2_ATOM_TUPLE_FOREACH(tuple, iter) \
  144. for (LV2_Atom* (iter) = lv2_atom_tuple_begin(tuple); \
  145. !lv2_atom_tuple_is_end(LV2_ATOM_BODY(tuple), (tuple)->size, (iter)); \
  146. (iter) = lv2_atom_tuple_next(iter))
  147. /** Like LV2_ATOM_TUPLE_FOREACH but for a headerless tuple body. */
  148. #define LV2_ATOM_TUPLE_BODY_FOREACH(body, size, iter) \
  149. for (LV2_Atom* (iter) = (LV2_Atom*)body; \
  150. !lv2_atom_tuple_is_end(body, size, (iter)); \
  151. (iter) = lv2_atom_tuple_next(iter))
  152. /**
  153. @}
  154. @name Object Iterator
  155. @{
  156. */
  157. /** Return a pointer to the first property in @p body. */
  158. static inline LV2_Atom_Property_Body*
  159. lv2_atom_object_begin(const LV2_Atom_Object_Body* body)
  160. {
  161. return (LV2_Atom_Property_Body*)(body + 1);
  162. }
  163. /** Return true iff @p i has reached the end of @p obj. */
  164. static inline bool
  165. lv2_atom_object_is_end(const LV2_Atom_Object_Body* body,
  166. uint32_t size,
  167. LV2_Atom_Property_Body* i)
  168. {
  169. return (uint8_t*)i >= ((uint8_t*)body + size);
  170. }
  171. /** Return an iterator to the property following @p i. */
  172. static inline LV2_Atom_Property_Body*
  173. lv2_atom_object_next(const LV2_Atom_Property_Body* i)
  174. {
  175. const LV2_Atom* const value = (LV2_Atom*)(
  176. (uint8_t*)i + 2 * sizeof(uint32_t));
  177. return (LV2_Atom_Property_Body*)(
  178. (uint8_t*)i + lv2_atom_pad_size(sizeof(LV2_Atom_Property_Body)
  179. + value->size));
  180. }
  181. /**
  182. A macro for iterating over all properties of an Object.
  183. @param obj The object to iterate over
  184. @param iter The name of the iterator
  185. This macro is used similarly to a for loop (which it expands to), e.g.:
  186. @code
  187. LV2_ATOM_OBJECT_FOREACH(object, i) {
  188. // Do something with prop (an LV2_Atom_Property_Body*) here...
  189. }
  190. @endcode
  191. */
  192. #define LV2_ATOM_OBJECT_FOREACH(obj, iter) \
  193. for (LV2_Atom_Property_Body* (iter) = lv2_atom_object_begin(&(obj)->body); \
  194. !lv2_atom_object_is_end(&(obj)->body, (obj)->atom.size, (iter)); \
  195. (iter) = lv2_atom_object_next(iter))
  196. /** Like LV2_ATOM_OBJECT_FOREACH but for a headerless object body. */
  197. #define LV2_ATOM_OBJECT_BODY_FOREACH(body, size, iter) \
  198. for (LV2_Atom_Property_Body* (iter) = lv2_atom_object_begin(body); \
  199. !lv2_atom_object_is_end(body, size, (iter)); \
  200. (iter) = lv2_atom_object_next(iter))
  201. /**
  202. @}
  203. @name Object Query
  204. @{
  205. */
  206. /** A single entry in an Object query. */
  207. typedef struct {
  208. uint32_t key; /**< Key to query (input set by user) */
  209. const LV2_Atom** value; /**< Found value (output set by query function) */
  210. } LV2_Atom_Object_Query;
  211. static const LV2_Atom_Object_Query LV2_ATOM_OBJECT_QUERY_END = { 0, NULL };
  212. /**
  213. Get an object's values for various keys.
  214. The value pointer of each item in @p query will be set to the location of
  215. the corresponding value in @p object. Every value pointer in @p query MUST
  216. be initialised to NULL. This function reads @p object in a single linear
  217. sweep. By allocating @p query on the stack, objects can be "queried"
  218. quickly without allocating any memory. This function is realtime safe.
  219. This function can only do "flat" queries, it is not smart enough to match
  220. variables in nested objects.
  221. For example:
  222. @code
  223. const LV2_Atom* name = NULL;
  224. const LV2_Atom* age = NULL;
  225. LV2_Atom_Object_Query q[] = {
  226. { urids.eg_name, &name },
  227. { urids.eg_age, &age },
  228. LV2_ATOM_OBJECT_QUERY_END
  229. };
  230. lv2_atom_object_query(obj, q);
  231. // name and age are now set to the appropriate values in obj, or NULL.
  232. @endcode
  233. */
  234. static inline int
  235. lv2_atom_object_query(const LV2_Atom_Object* object,
  236. LV2_Atom_Object_Query* query)
  237. {
  238. int matches = 0;
  239. int n_queries = 0;
  240. /* Count number of query keys so we can short-circuit when done */
  241. for (LV2_Atom_Object_Query* q = query; q->key; ++q) {
  242. ++n_queries;
  243. }
  244. LV2_ATOM_OBJECT_FOREACH(object, prop) {
  245. for (LV2_Atom_Object_Query* q = query; q->key; ++q) {
  246. if (q->key == prop->key && !*q->value) {
  247. *q->value = &prop->value;
  248. if (++matches == n_queries) {
  249. return matches;
  250. }
  251. break;
  252. }
  253. }
  254. }
  255. return matches;
  256. }
  257. /**
  258. Body only version of lv2_atom_object_get().
  259. */
  260. static inline int
  261. lv2_atom_object_body_get(uint32_t size, const LV2_Atom_Object_Body* body, ...)
  262. {
  263. int matches = 0;
  264. int n_queries = 0;
  265. /* Count number of keys so we can short-circuit when done */
  266. va_list args;
  267. va_start(args, body);
  268. for (n_queries = 0; va_arg(args, uint32_t); ++n_queries) {
  269. if (!va_arg(args, const LV2_Atom**)) {
  270. return -1;
  271. }
  272. }
  273. va_end(args);
  274. LV2_ATOM_OBJECT_BODY_FOREACH(body, size, prop) {
  275. va_start(args, body);
  276. for (int i = 0; i < n_queries; ++i) {
  277. uint32_t qkey = va_arg(args, uint32_t);
  278. const LV2_Atom** qval = va_arg(args, const LV2_Atom**);
  279. if (qkey == prop->key && !*qval) {
  280. *qval = &prop->value;
  281. if (++matches == n_queries) {
  282. return matches;
  283. }
  284. break;
  285. }
  286. }
  287. va_end(args);
  288. }
  289. return matches;
  290. }
  291. /**
  292. Variable argument version of lv2_atom_object_query().
  293. This is nicer-looking in code, but a bit more error-prone since it is not
  294. type safe and the argument list must be terminated.
  295. The arguments should be a series of uint32_t key and const LV2_Atom** value
  296. pairs, terminated by a zero key. The value pointers MUST be initialized to
  297. NULL. For example:
  298. @code
  299. const LV2_Atom* name = NULL;
  300. const LV2_Atom* age = NULL;
  301. lv2_atom_object_get(obj,
  302. uris.name_key, &name,
  303. uris.age_key, &age,
  304. 0);
  305. @endcode
  306. */
  307. static inline int
  308. lv2_atom_object_get(const LV2_Atom_Object* object, ...)
  309. {
  310. int matches = 0;
  311. int n_queries = 0;
  312. /* Count number of keys so we can short-circuit when done */
  313. va_list args;
  314. va_start(args, object);
  315. for (n_queries = 0; va_arg(args, uint32_t); ++n_queries) {
  316. if (!va_arg(args, const LV2_Atom**)) {
  317. return -1;
  318. }
  319. }
  320. va_end(args);
  321. LV2_ATOM_OBJECT_FOREACH(object, prop) {
  322. va_start(args, object);
  323. for (int i = 0; i < n_queries; ++i) {
  324. uint32_t qkey = va_arg(args, uint32_t);
  325. const LV2_Atom** qval = va_arg(args, const LV2_Atom**);
  326. if (qkey == prop->key && !*qval) {
  327. *qval = &prop->value;
  328. if (++matches == n_queries) {
  329. return matches;
  330. }
  331. break;
  332. }
  333. }
  334. va_end(args);
  335. }
  336. return matches;
  337. }
  338. /**
  339. @}
  340. */
  341. #ifdef __cplusplus
  342. } /* extern "C" */
  343. #endif
  344. #endif /* LV2_ATOM_UTIL_H */