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 12KB

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