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
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 const LV2_Atom_Event*
  62. lv2_atom_sequence_begin(const LV2_Atom_Sequence_Body* body)
  63. {
  64. return (const LV2_Atom_Event*)(body + 1);
  65. }
  66. /** Get an iterator pointing to the end of a Sequence body. */
  67. static inline const LV2_Atom_Event*
  68. lv2_atom_sequence_end(const LV2_Atom_Sequence_Body* body, uint32_t size)
  69. {
  70. return (const LV2_Atom_Event*)((const 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. const LV2_Atom_Event* i)
  77. {
  78. return (const uint8_t*)i >= ((const uint8_t*)body + size);
  79. }
  80. /** Return an iterator to the element following @p i. */
  81. static inline const LV2_Atom_Event*
  82. lv2_atom_sequence_next(const LV2_Atom_Event* i)
  83. {
  84. if (!i) return NULL;
  85. return (const LV2_Atom_Event*)((const uint8_t*)i
  86. + sizeof(LV2_Atom_Event)
  87. + lv2_atom_pad_size(i->body.size));
  88. }
  89. /**
  90. A macro for iterating over all events in a Sequence.
  91. @param seq The sequence to iterate over
  92. @param iter The name of the iterator
  93. This macro is used similarly to a for loop (which it expands to), e.g.:
  94. @code
  95. LV2_ATOM_SEQUENCE_FOREACH(sequence, ev) {
  96. // Do something with ev (an LV2_Atom_Event*) here...
  97. }
  98. @endcode
  99. */
  100. #define LV2_ATOM_SEQUENCE_FOREACH(seq, iter) \
  101. for (const LV2_Atom_Event* (iter) = lv2_atom_sequence_begin(&(seq)->body); \
  102. !lv2_atom_sequence_is_end(&(seq)->body, (seq)->atom.size, (iter)); \
  103. (iter) = lv2_atom_sequence_next(iter))
  104. /** Like LV2_ATOM_SEQUENCE_FOREACH but for a headerless sequence body. */
  105. #define LV2_ATOM_SEQUENCE_BODY_FOREACH(body, size, iter) \
  106. for (const LV2_Atom_Event* (iter) = lv2_atom_sequence_begin(body); \
  107. !lv2_atom_sequence_is_end(body, size, (iter)); \
  108. (iter) = lv2_atom_sequence_next(iter))
  109. /**
  110. @}
  111. @name Tuple Iterator
  112. @{
  113. */
  114. /** Get an iterator pointing to the first element in @p tup. */
  115. static inline const LV2_Atom*
  116. lv2_atom_tuple_begin(const LV2_Atom_Tuple* tup)
  117. {
  118. return (const LV2_Atom*)(LV2_ATOM_BODY_CONST(tup));
  119. }
  120. /** Return true iff @p i has reached the end of @p body. */
  121. static inline bool
  122. lv2_atom_tuple_is_end(const void* body, uint32_t size, LV2_Atom* i)
  123. {
  124. return (const uint8_t*)i >= ((const uint8_t*)body + size);
  125. }
  126. /** Return an iterator to the element following @p i. */
  127. static inline const LV2_Atom*
  128. lv2_atom_tuple_next(const LV2_Atom* i)
  129. {
  130. return (const LV2_Atom*)(
  131. (const uint8_t*)i + sizeof(LV2_Atom) + lv2_atom_pad_size(i->size));
  132. }
  133. /**
  134. A macro for iterating over all properties of a Tuple.
  135. @param tuple The tuple to iterate over
  136. @param iter The name of the iterator
  137. This macro is used similarly to a for loop (which it expands to), e.g.:
  138. @code
  139. LV2_ATOMO_TUPLE_FOREACH(tuple, elem) {
  140. // Do something with elem (an LV2_Atom*) here...
  141. }
  142. @endcode
  143. */
  144. #define LV2_ATOM_TUPLE_FOREACH(tuple, iter) \
  145. for (LV2_Atom* (iter) = lv2_atom_tuple_begin(tuple); \
  146. !lv2_atom_tuple_is_end(LV2_ATOM_BODY(tuple), (tuple)->size, (iter)); \
  147. (iter) = lv2_atom_tuple_next(iter))
  148. /** Like LV2_ATOM_TUPLE_FOREACH but for a headerless tuple body. */
  149. #define LV2_ATOM_TUPLE_BODY_FOREACH(body, size, iter) \
  150. for (LV2_Atom* (iter) = (LV2_Atom*)body; \
  151. !lv2_atom_tuple_is_end(body, size, (iter)); \
  152. (iter) = lv2_atom_tuple_next(iter))
  153. /**
  154. @}
  155. @name Object Iterator
  156. @{
  157. */
  158. /** Return a pointer to the first property in @p body. */
  159. static inline const LV2_Atom_Property_Body*
  160. lv2_atom_object_begin(const LV2_Atom_Object_Body* body)
  161. {
  162. return (const LV2_Atom_Property_Body*)(body + 1);
  163. }
  164. /** Return true iff @p i has reached the end of @p obj. */
  165. static inline bool
  166. lv2_atom_object_is_end(const LV2_Atom_Object_Body* body,
  167. uint32_t size,
  168. const LV2_Atom_Property_Body* i)
  169. {
  170. return (const uint8_t*)i >= ((const uint8_t*)body + size);
  171. }
  172. /** Return an iterator to the property following @p i. */
  173. static inline const LV2_Atom_Property_Body*
  174. lv2_atom_object_next(const LV2_Atom_Property_Body* i)
  175. {
  176. const LV2_Atom* const value = (const LV2_Atom*)(
  177. (const uint8_t*)i + 2 * sizeof(uint32_t));
  178. return (const LV2_Atom_Property_Body*)(
  179. (const uint8_t*)i + lv2_atom_pad_size(sizeof(LV2_Atom_Property_Body)
  180. + value->size));
  181. }
  182. /**
  183. A macro for iterating over all properties of an Object.
  184. @param obj The object to iterate over
  185. @param iter The name of the iterator
  186. This macro is used similarly to a for loop (which it expands to), e.g.:
  187. @code
  188. LV2_ATOM_OBJECT_FOREACH(object, i) {
  189. // Do something with prop (an LV2_Atom_Property_Body*) here...
  190. }
  191. @endcode
  192. */
  193. #define LV2_ATOM_OBJECT_FOREACH(obj, iter) \
  194. for (const LV2_Atom_Property_Body* (iter) = lv2_atom_object_begin(&(obj)->body); \
  195. !lv2_atom_object_is_end(&(obj)->body, (obj)->atom.size, (iter)); \
  196. (iter) = lv2_atom_object_next(iter))
  197. /** Like LV2_ATOM_OBJECT_FOREACH but for a headerless object body. */
  198. #define LV2_ATOM_OBJECT_BODY_FOREACH(body, size, iter) \
  199. for (const LV2_Atom_Property_Body* (iter) = lv2_atom_object_begin(body); \
  200. !lv2_atom_object_is_end(body, size, (iter)); \
  201. (iter) = lv2_atom_object_next(iter))
  202. /**
  203. @}
  204. @name Object Query
  205. @{
  206. */
  207. /** A single entry in an Object query. */
  208. typedef struct {
  209. uint32_t key; /**< Key to query (input set by user) */
  210. const LV2_Atom** value; /**< Found value (output set by query function) */
  211. } LV2_Atom_Object_Query;
  212. static const LV2_Atom_Object_Query LV2_ATOM_OBJECT_QUERY_END = { 0, NULL };
  213. /**
  214. Get an object's values for various keys.
  215. The value pointer of each item in @p query will be set to the location of
  216. the corresponding value in @p object. Every value pointer in @p query MUST
  217. be initialised to NULL. This function reads @p object in a single linear
  218. sweep. By allocating @p query on the stack, objects can be "queried"
  219. quickly without allocating any memory. This function is realtime safe.
  220. This function can only do "flat" queries, it is not smart enough to match
  221. variables in nested objects.
  222. For example:
  223. @code
  224. const LV2_Atom* name = NULL;
  225. const LV2_Atom* age = NULL;
  226. LV2_Atom_Object_Query q[] = {
  227. { urids.eg_name, &name },
  228. { urids.eg_age, &age },
  229. LV2_ATOM_OBJECT_QUERY_END
  230. };
  231. lv2_atom_object_query(obj, q);
  232. // name and age are now set to the appropriate values in obj, or NULL.
  233. @endcode
  234. */
  235. static inline int
  236. lv2_atom_object_query(const LV2_Atom_Object* object,
  237. LV2_Atom_Object_Query* query)
  238. {
  239. int matches = 0;
  240. int n_queries = 0;
  241. /* Count number of query keys so we can short-circuit when done */
  242. for (LV2_Atom_Object_Query* q = query; q->key; ++q) {
  243. ++n_queries;
  244. }
  245. LV2_ATOM_OBJECT_FOREACH(object, prop) {
  246. for (LV2_Atom_Object_Query* q = query; q->key; ++q) {
  247. if (q->key == prop->key && !*q->value) {
  248. *q->value = &prop->value;
  249. if (++matches == n_queries) {
  250. return matches;
  251. }
  252. break;
  253. }
  254. }
  255. }
  256. return matches;
  257. }
  258. /**
  259. Body only version of lv2_atom_object_get().
  260. */
  261. static inline int
  262. lv2_atom_object_body_get(uint32_t size, const LV2_Atom_Object_Body* body, ...)
  263. {
  264. int matches = 0;
  265. int n_queries = 0;
  266. /* Count number of keys so we can short-circuit when done */
  267. va_list args;
  268. va_start(args, body);
  269. for (n_queries = 0; va_arg(args, uint32_t); ++n_queries) {
  270. if (!va_arg(args, const LV2_Atom**)) {
  271. return -1;
  272. }
  273. }
  274. va_end(args);
  275. LV2_ATOM_OBJECT_BODY_FOREACH(body, size, prop) {
  276. va_start(args, body);
  277. for (int i = 0; i < n_queries; ++i) {
  278. uint32_t qkey = va_arg(args, uint32_t);
  279. const LV2_Atom** qval = va_arg(args, const LV2_Atom**);
  280. if (qkey == prop->key && !*qval) {
  281. *qval = &prop->value;
  282. if (++matches == n_queries) {
  283. return matches;
  284. }
  285. break;
  286. }
  287. }
  288. va_end(args);
  289. }
  290. return matches;
  291. }
  292. /**
  293. Variable argument version of lv2_atom_object_query().
  294. This is nicer-looking in code, but a bit more error-prone since it is not
  295. type safe and the argument list must be terminated.
  296. The arguments should be a series of uint32_t key and const LV2_Atom** value
  297. pairs, terminated by a zero key. The value pointers MUST be initialized to
  298. NULL. For example:
  299. @code
  300. const LV2_Atom* name = NULL;
  301. const LV2_Atom* age = NULL;
  302. lv2_atom_object_get(obj,
  303. uris.name_key, &name,
  304. uris.age_key, &age,
  305. 0);
  306. @endcode
  307. */
  308. static inline int
  309. lv2_atom_object_get(const LV2_Atom_Object* object, ...)
  310. {
  311. int matches = 0;
  312. int n_queries = 0;
  313. /* Count number of keys so we can short-circuit when done */
  314. va_list args;
  315. va_start(args, object);
  316. for (n_queries = 0; va_arg(args, uint32_t); ++n_queries) {
  317. if (!va_arg(args, const LV2_Atom**)) {
  318. return -1;
  319. }
  320. }
  321. va_end(args);
  322. LV2_ATOM_OBJECT_FOREACH(object, prop) {
  323. va_start(args, object);
  324. for (int i = 0; i < n_queries; ++i) {
  325. uint32_t qkey = va_arg(args, uint32_t);
  326. const LV2_Atom** qval = va_arg(args, const LV2_Atom**);
  327. if (qkey == prop->key && !*qval) {
  328. *qval = &prop->value;
  329. if (++matches == n_queries) {
  330. return matches;
  331. }
  332. break;
  333. }
  334. }
  335. va_end(args);
  336. }
  337. return matches;
  338. }
  339. /**
  340. @}
  341. */
  342. #ifdef __cplusplus
  343. } /* extern "C" */
  344. #endif
  345. #endif /* LV2_ATOM_UTIL_H */