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-forge.h 23KB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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 forge.h An API for constructing LV2 atoms.
  16. This file provides an API for constructing Atoms which makes it relatively
  17. simple to build nested atoms of arbitrary complexity without requiring
  18. dynamic memory allocation.
  19. The API is based on successively appending the appropriate pieces to build a
  20. complete Atom. The size of containers is automatically updated. Functions
  21. that begin a container return (via their frame argument) a stack frame which
  22. must be popped when the container is finished.
  23. All output is written to a user-provided buffer or sink function. This
  24. makes it popssible to create create atoms on the stack, on the heap, in LV2
  25. port buffers, in a ringbuffer, or elsewhere, all using the same API.
  26. This entire API is realtime safe if used with a buffer or a realtime safe
  27. sink, except lv2_atom_forge_init() which is only realtime safe if the URI
  28. map function is.
  29. Note these functions are all static inline, do not take their address.
  30. This header is non-normative, it is provided for convenience.
  31. */
  32. #ifndef LV2_ATOM_FORGE_H
  33. #define LV2_ATOM_FORGE_H
  34. #include <assert.h>
  35. #include "atom.h"
  36. #include "atom-util.h"
  37. #include "urid.h"
  38. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
  39. # define LV2_ATOM_FORGE_DEPRECATED __attribute__((__deprecated__))
  40. #else
  41. # define LV2_ATOM_FORGE_DEPRECATED
  42. #endif
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #else
  46. # include <stdbool.h>
  47. #endif
  48. /** Handle for LV2_Atom_Forge_Sink. */
  49. typedef void* LV2_Atom_Forge_Sink_Handle;
  50. /** A reference to a chunk of written output. */
  51. typedef intptr_t LV2_Atom_Forge_Ref;
  52. /** Sink function for writing output. See lv2_atom_forge_set_sink(). */
  53. typedef LV2_Atom_Forge_Ref
  54. (*LV2_Atom_Forge_Sink)(LV2_Atom_Forge_Sink_Handle handle,
  55. const void* buf,
  56. uint32_t size);
  57. /** Function for resolving a reference. See lv2_atom_forge_set_sink(). */
  58. typedef LV2_Atom*
  59. (*LV2_Atom_Forge_Deref_Func)(LV2_Atom_Forge_Sink_Handle handle,
  60. LV2_Atom_Forge_Ref ref);
  61. /** A stack frame used for keeping track of nested Atom containers. */
  62. typedef struct _LV2_Atom_Forge_Frame {
  63. struct _LV2_Atom_Forge_Frame* parent;
  64. LV2_Atom_Forge_Ref ref;
  65. } LV2_Atom_Forge_Frame;
  66. /** A "forge" for creating atoms by appending to a buffer. */
  67. typedef struct {
  68. uint8_t* buf;
  69. uint32_t offset;
  70. uint32_t size;
  71. LV2_Atom_Forge_Sink sink;
  72. LV2_Atom_Forge_Deref_Func deref;
  73. LV2_Atom_Forge_Sink_Handle handle;
  74. LV2_Atom_Forge_Frame* stack;
  75. LV2_URID Blank LV2_ATOM_FORGE_DEPRECATED;
  76. LV2_URID Bool;
  77. LV2_URID Chunk;
  78. LV2_URID Double;
  79. LV2_URID Float;
  80. LV2_URID Int;
  81. LV2_URID Long;
  82. LV2_URID Literal;
  83. LV2_URID Object;
  84. LV2_URID Path;
  85. LV2_URID Property;
  86. LV2_URID Resource LV2_ATOM_FORGE_DEPRECATED;
  87. LV2_URID Sequence;
  88. LV2_URID String;
  89. LV2_URID Tuple;
  90. LV2_URID URI;
  91. LV2_URID URID;
  92. LV2_URID Vector;
  93. } LV2_Atom_Forge;
  94. static inline void
  95. lv2_atom_forge_set_buffer(LV2_Atom_Forge* forge, uint8_t* buf, size_t size);
  96. /**
  97. Initialise @p forge.
  98. URIs will be mapped using @p map and stored, a reference to @p map itself is
  99. not held.
  100. */
  101. static inline void
  102. lv2_atom_forge_init(LV2_Atom_Forge* forge, LV2_URID_Map* map)
  103. {
  104. #if defined(__clang__)
  105. # pragma clang diagnostic push
  106. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  107. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  108. # pragma GCC diagnostic push
  109. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  110. #endif
  111. lv2_atom_forge_set_buffer(forge, NULL, 0);
  112. forge->Blank = map->map(map->handle, LV2_ATOM__Blank);
  113. forge->Bool = map->map(map->handle, LV2_ATOM__Bool);
  114. forge->Chunk = map->map(map->handle, LV2_ATOM__Chunk);
  115. forge->Double = map->map(map->handle, LV2_ATOM__Double);
  116. forge->Float = map->map(map->handle, LV2_ATOM__Float);
  117. forge->Int = map->map(map->handle, LV2_ATOM__Int);
  118. forge->Long = map->map(map->handle, LV2_ATOM__Long);
  119. forge->Literal = map->map(map->handle, LV2_ATOM__Literal);
  120. forge->Object = map->map(map->handle, LV2_ATOM__Object);
  121. forge->Path = map->map(map->handle, LV2_ATOM__Path);
  122. forge->Property = map->map(map->handle, LV2_ATOM__Property);
  123. forge->Resource = map->map(map->handle, LV2_ATOM__Resource);
  124. forge->Sequence = map->map(map->handle, LV2_ATOM__Sequence);
  125. forge->String = map->map(map->handle, LV2_ATOM__String);
  126. forge->Tuple = map->map(map->handle, LV2_ATOM__Tuple);
  127. forge->URI = map->map(map->handle, LV2_ATOM__URI);
  128. forge->URID = map->map(map->handle, LV2_ATOM__URID);
  129. forge->Vector = map->map(map->handle, LV2_ATOM__Vector);
  130. #if defined(__clang__)
  131. # pragma clang diagnostic pop
  132. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  133. # pragma GCC diagnostic pop
  134. #endif
  135. }
  136. static inline LV2_Atom*
  137. lv2_atom_forge_deref(LV2_Atom_Forge* forge, LV2_Atom_Forge_Ref ref)
  138. {
  139. if (forge->buf) {
  140. return (LV2_Atom*)ref;
  141. } else {
  142. return forge->deref(forge->handle, ref);
  143. }
  144. }
  145. /**
  146. @name Object Stack
  147. @{
  148. */
  149. /**
  150. Push a stack frame.
  151. This is done automatically by container functions (which take a stack frame
  152. pointer), but may be called by the user to push the top level container when
  153. writing to an existing Atom.
  154. */
  155. static inline LV2_Atom_Forge_Ref
  156. lv2_atom_forge_push(LV2_Atom_Forge* forge,
  157. LV2_Atom_Forge_Frame* frame,
  158. LV2_Atom_Forge_Ref ref)
  159. {
  160. frame->parent = forge->stack;
  161. frame->ref = ref;
  162. forge->stack = frame;
  163. return ref;
  164. }
  165. /** Pop a stack frame. This must be called when a container is finished. */
  166. static inline void
  167. lv2_atom_forge_pop(LV2_Atom_Forge* forge, LV2_Atom_Forge_Frame* frame)
  168. {
  169. assert(frame == forge->stack);
  170. forge->stack = frame->parent;
  171. }
  172. /** Return true iff the top of the stack has the given type. */
  173. static inline bool
  174. lv2_atom_forge_top_is(LV2_Atom_Forge* forge, uint32_t type)
  175. {
  176. return forge->stack && forge->stack->ref &&
  177. (lv2_atom_forge_deref(forge, forge->stack->ref)->type == type);
  178. }
  179. /** Return true iff @p type is an atom:Object. */
  180. static inline bool
  181. lv2_atom_forge_is_object_type(const LV2_Atom_Forge* forge, uint32_t type)
  182. {
  183. #if defined(__clang__)
  184. # pragma clang diagnostic push
  185. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  186. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  187. # pragma GCC diagnostic push
  188. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  189. #endif
  190. return (type == forge->Object ||
  191. type == forge->Blank ||
  192. type == forge->Resource);
  193. #if defined(__clang__)
  194. # pragma clang diagnostic pop
  195. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  196. # pragma GCC diagnostic pop
  197. #endif
  198. }
  199. /** Return true iff @p type is an atom:Object with a blank ID. */
  200. static inline bool
  201. lv2_atom_forge_is_blank(const LV2_Atom_Forge* forge,
  202. uint32_t type,
  203. const LV2_Atom_Object_Body* body)
  204. {
  205. #if defined(__clang__)
  206. # pragma clang diagnostic push
  207. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  208. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  209. # pragma GCC diagnostic push
  210. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  211. #endif
  212. return (type == forge->Blank ||
  213. (type == forge->Object && body->id == 0));
  214. #if defined(__clang__)
  215. # pragma clang diagnostic pop
  216. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  217. # pragma GCC diagnostic pop
  218. #endif
  219. }
  220. /**
  221. @}
  222. @name Output Configuration
  223. @{
  224. */
  225. /** Set the output buffer where @p forge will write atoms. */
  226. static inline void
  227. lv2_atom_forge_set_buffer(LV2_Atom_Forge* forge, uint8_t* buf, size_t size)
  228. {
  229. forge->buf = buf;
  230. forge->size = (uint32_t)size;
  231. forge->offset = 0;
  232. forge->deref = NULL;
  233. forge->sink = NULL;
  234. forge->handle = NULL;
  235. forge->stack = NULL;
  236. }
  237. /**
  238. Set the sink function where @p forge will write output.
  239. The return value of forge functions is an LV2_Atom_Forge_Ref which is an
  240. integer type safe to use as a pointer but is otherwise opaque. The sink
  241. function must return a ref that can be dereferenced to access as least
  242. sizeof(LV2_Atom) bytes of the written data, so sizes can be updated. For
  243. ringbuffers, this should be possible as long as the size of the buffer is a
  244. multiple of sizeof(LV2_Atom), since atoms are always aligned.
  245. Note that 0 is an invalid reference, so if you are using a buffer offset be
  246. sure to offset it such that 0 is never a valid reference. You will get
  247. confusing errors otherwise.
  248. */
  249. static inline void
  250. lv2_atom_forge_set_sink(LV2_Atom_Forge* forge,
  251. LV2_Atom_Forge_Sink sink,
  252. LV2_Atom_Forge_Deref_Func deref,
  253. LV2_Atom_Forge_Sink_Handle handle)
  254. {
  255. forge->buf = NULL;
  256. forge->size = forge->offset = 0;
  257. forge->deref = deref;
  258. forge->sink = sink;
  259. forge->handle = handle;
  260. forge->stack = NULL;
  261. }
  262. /**
  263. @}
  264. @name Low Level Output
  265. @{
  266. */
  267. /**
  268. Write raw output. This is used internally, but is also useful for writing
  269. atom types not explicitly supported by the forge API. Note the caller is
  270. responsible for ensuring the output is approriately padded.
  271. */
  272. static inline LV2_Atom_Forge_Ref
  273. lv2_atom_forge_raw(LV2_Atom_Forge* forge, const void* data, uint32_t size)
  274. {
  275. LV2_Atom_Forge_Ref out = 0;
  276. if (forge->sink) {
  277. out = forge->sink(forge->handle, data, size);
  278. } else {
  279. out = (LV2_Atom_Forge_Ref)forge->buf + (LV2_Atom_Forge_Ref)forge->offset;
  280. uint8_t* mem = forge->buf + forge->offset;
  281. if (forge->offset + size > forge->size) {
  282. return 0;
  283. }
  284. forge->offset += size;
  285. memcpy(mem, data, size);
  286. }
  287. for (LV2_Atom_Forge_Frame* f = forge->stack; f; f = f->parent) {
  288. lv2_atom_forge_deref(forge, f->ref)->size += size;
  289. }
  290. return out;
  291. }
  292. /** Pad output accordingly so next write is 64-bit aligned. */
  293. static inline void
  294. lv2_atom_forge_pad(LV2_Atom_Forge* forge, uint32_t written)
  295. {
  296. const uint64_t pad = 0;
  297. const uint32_t pad_size = lv2_atom_pad_size(written) - written;
  298. lv2_atom_forge_raw(forge, &pad, pad_size);
  299. }
  300. /** Write raw output, padding to 64-bits as necessary. */
  301. static inline LV2_Atom_Forge_Ref
  302. lv2_atom_forge_write(LV2_Atom_Forge* forge, const void* data, uint32_t size)
  303. {
  304. LV2_Atom_Forge_Ref out = lv2_atom_forge_raw(forge, data, size);
  305. if (out) {
  306. lv2_atom_forge_pad(forge, size);
  307. }
  308. return out;
  309. }
  310. /** Write a null-terminated string body. */
  311. static inline LV2_Atom_Forge_Ref
  312. lv2_atom_forge_string_body(LV2_Atom_Forge* forge,
  313. const char* str,
  314. uint32_t len)
  315. {
  316. LV2_Atom_Forge_Ref out = lv2_atom_forge_raw(forge, str, len);
  317. if (out && (out = lv2_atom_forge_raw(forge, "", 1))) {
  318. lv2_atom_forge_pad(forge, len + 1);
  319. }
  320. return out;
  321. }
  322. /**
  323. @}
  324. @name Atom Output
  325. @{
  326. */
  327. /** Write an atom:Atom header. */
  328. static inline LV2_Atom_Forge_Ref
  329. lv2_atom_forge_atom(LV2_Atom_Forge* forge, uint32_t size, uint32_t type)
  330. {
  331. const LV2_Atom a = { size, type };
  332. return lv2_atom_forge_raw(forge, &a, sizeof(a));
  333. }
  334. /** Write a primitive (fixed-size) atom. */
  335. static inline LV2_Atom_Forge_Ref
  336. lv2_atom_forge_primitive(LV2_Atom_Forge* forge, const LV2_Atom* a)
  337. {
  338. if (lv2_atom_forge_top_is(forge, forge->Vector)) {
  339. return lv2_atom_forge_raw(forge, LV2_ATOM_BODY_CONST(a), a->size);
  340. } else {
  341. return lv2_atom_forge_write(
  342. forge, a, (uint32_t)sizeof(LV2_Atom) + a->size);
  343. }
  344. }
  345. /** Write an atom:Int. */
  346. static inline LV2_Atom_Forge_Ref
  347. lv2_atom_forge_int(LV2_Atom_Forge* forge, int32_t val)
  348. {
  349. const LV2_Atom_Int a = { { sizeof(val), forge->Int }, val };
  350. return lv2_atom_forge_primitive(forge, &a.atom);
  351. }
  352. /** Write an atom:Long. */
  353. static inline LV2_Atom_Forge_Ref
  354. lv2_atom_forge_long(LV2_Atom_Forge* forge, int64_t val)
  355. {
  356. const LV2_Atom_Long a = { { sizeof(val), forge->Long }, val };
  357. return lv2_atom_forge_primitive(forge, &a.atom);
  358. }
  359. /** Write an atom:Float. */
  360. static inline LV2_Atom_Forge_Ref
  361. lv2_atom_forge_float(LV2_Atom_Forge* forge, float val)
  362. {
  363. const LV2_Atom_Float a = { { sizeof(val), forge->Float }, val };
  364. return lv2_atom_forge_primitive(forge, &a.atom);
  365. }
  366. /** Write an atom:Double. */
  367. static inline LV2_Atom_Forge_Ref
  368. lv2_atom_forge_double(LV2_Atom_Forge* forge, double val)
  369. {
  370. const LV2_Atom_Double a = { { sizeof(val), forge->Double }, val };
  371. return lv2_atom_forge_primitive(forge, &a.atom);
  372. }
  373. /** Write an atom:Bool. */
  374. static inline LV2_Atom_Forge_Ref
  375. lv2_atom_forge_bool(LV2_Atom_Forge* forge, bool val)
  376. {
  377. const LV2_Atom_Bool a = { { sizeof(int32_t), forge->Bool }, val ? 1 : 0 };
  378. return lv2_atom_forge_primitive(forge, &a.atom);
  379. }
  380. /** Write an atom:URID. */
  381. static inline LV2_Atom_Forge_Ref
  382. lv2_atom_forge_urid(LV2_Atom_Forge* forge, LV2_URID id)
  383. {
  384. const LV2_Atom_URID a = { { sizeof(id), forge->URID }, id };
  385. return lv2_atom_forge_primitive(forge, &a.atom);
  386. }
  387. /** Write an atom compatible with atom:String. Used internally. */
  388. static inline LV2_Atom_Forge_Ref
  389. lv2_atom_forge_typed_string(LV2_Atom_Forge* forge,
  390. uint32_t type,
  391. const char* str,
  392. uint32_t len)
  393. {
  394. const LV2_Atom_String a = { { len + 1, type } };
  395. LV2_Atom_Forge_Ref out = lv2_atom_forge_raw(forge, &a, sizeof(a));
  396. if (out) {
  397. if (!lv2_atom_forge_string_body(forge, str, len)) {
  398. LV2_Atom* atom = lv2_atom_forge_deref(forge, out);
  399. atom->size = atom->type = 0;
  400. out = 0;
  401. }
  402. }
  403. return out;
  404. }
  405. /** Write an atom:String. Note that @p str need not be NULL terminated. */
  406. static inline LV2_Atom_Forge_Ref
  407. lv2_atom_forge_string(LV2_Atom_Forge* forge, const char* str, uint32_t len)
  408. {
  409. return lv2_atom_forge_typed_string(forge, forge->String, str, len);
  410. }
  411. /**
  412. Write an atom:URI. Note that @p uri need not be NULL terminated.
  413. This does not map the URI, but writes the complete URI string. To write
  414. a mapped URI, use lv2_atom_forge_urid().
  415. */
  416. static inline LV2_Atom_Forge_Ref
  417. lv2_atom_forge_uri(LV2_Atom_Forge* forge, const char* uri, uint32_t len)
  418. {
  419. return lv2_atom_forge_typed_string(forge, forge->URI, uri, len);
  420. }
  421. /** Write an atom:Path. Note that @p path need not be NULL terminated. */
  422. static inline LV2_Atom_Forge_Ref
  423. lv2_atom_forge_path(LV2_Atom_Forge* forge, const char* path, uint32_t len)
  424. {
  425. return lv2_atom_forge_typed_string(forge, forge->Path, path, len);
  426. }
  427. /** Write an atom:Literal. */
  428. static inline LV2_Atom_Forge_Ref
  429. lv2_atom_forge_literal(LV2_Atom_Forge* forge,
  430. const char* str,
  431. uint32_t len,
  432. uint32_t datatype,
  433. uint32_t lang)
  434. {
  435. const LV2_Atom_Literal a = {
  436. { (uint32_t)(sizeof(LV2_Atom_Literal) - sizeof(LV2_Atom) + len + 1),
  437. forge->Literal },
  438. { datatype,
  439. lang }
  440. };
  441. LV2_Atom_Forge_Ref out = lv2_atom_forge_raw(forge, &a, sizeof(a));
  442. if (out) {
  443. if (!lv2_atom_forge_string_body(forge, str, len)) {
  444. LV2_Atom* atom = lv2_atom_forge_deref(forge, out);
  445. atom->size = atom->type = 0;
  446. out = 0;
  447. }
  448. }
  449. return out;
  450. }
  451. /** Start an atom:Vector. */
  452. static inline LV2_Atom_Forge_Ref
  453. lv2_atom_forge_vector_head(LV2_Atom_Forge* forge,
  454. LV2_Atom_Forge_Frame* frame,
  455. uint32_t child_size,
  456. uint32_t child_type)
  457. {
  458. const LV2_Atom_Vector a = {
  459. { sizeof(LV2_Atom_Vector_Body), forge->Vector },
  460. { child_size, child_type }
  461. };
  462. return lv2_atom_forge_push(
  463. forge, frame, lv2_atom_forge_write(forge, &a, sizeof(a)));
  464. }
  465. /** Write a complete atom:Vector. */
  466. static inline LV2_Atom_Forge_Ref
  467. lv2_atom_forge_vector(LV2_Atom_Forge* forge,
  468. uint32_t child_size,
  469. uint32_t child_type,
  470. uint32_t n_elems,
  471. const void* elems)
  472. {
  473. const LV2_Atom_Vector a = {
  474. { (uint32_t)(sizeof(LV2_Atom_Vector_Body) + n_elems * child_size),
  475. forge->Vector },
  476. { child_size, child_type }
  477. };
  478. LV2_Atom_Forge_Ref out = lv2_atom_forge_write(forge, &a, sizeof(a));
  479. if (out) {
  480. lv2_atom_forge_write(forge, elems, child_size * n_elems);
  481. }
  482. return out;
  483. }
  484. /**
  485. Write the header of an atom:Tuple.
  486. The passed frame will be initialised to represent this tuple. To complete
  487. the tuple, write a sequence of atoms, then pop the frame with
  488. lv2_atom_forge_pop().
  489. For example:
  490. @code
  491. // Write tuple (1, 2.0)
  492. LV2_Atom_Forge_Frame frame;
  493. LV2_Atom* tup = (LV2_Atom*)lv2_atom_forge_tuple(forge, &frame);
  494. lv2_atom_forge_int32(forge, 1);
  495. lv2_atom_forge_float(forge, 2.0);
  496. lv2_atom_forge_pop(forge, &frame);
  497. @endcode
  498. */
  499. static inline LV2_Atom_Forge_Ref
  500. lv2_atom_forge_tuple(LV2_Atom_Forge* forge, LV2_Atom_Forge_Frame* frame)
  501. {
  502. const LV2_Atom_Tuple a = { { 0, forge->Tuple } };
  503. return lv2_atom_forge_push(
  504. forge, frame, lv2_atom_forge_write(forge, &a, sizeof(a)));
  505. }
  506. /**
  507. Write the header of an atom:Object.
  508. The passed frame will be initialised to represent this object. To complete
  509. the object, write a sequence of properties, then pop the frame with
  510. lv2_atom_forge_pop().
  511. For example:
  512. @code
  513. LV2_URID eg_Cat = map("http://example.org/Cat");
  514. LV2_URID eg_name = map("http://example.org/name");
  515. // Start object with type eg_Cat and blank ID
  516. LV2_Atom_Forge_Frame frame;
  517. lv2_atom_forge_object(forge, &frame, 0, eg_Cat);
  518. // Append property eg:name = "Hobbes"
  519. lv2_atom_forge_key(forge, eg_name);
  520. lv2_atom_forge_string(forge, "Hobbes", strlen("Hobbes"));
  521. // Finish object
  522. lv2_atom_forge_pop(forge, &frame);
  523. @endcode
  524. */
  525. static inline LV2_Atom_Forge_Ref
  526. lv2_atom_forge_object(LV2_Atom_Forge* forge,
  527. LV2_Atom_Forge_Frame* frame,
  528. LV2_URID id,
  529. LV2_URID otype)
  530. {
  531. const LV2_Atom_Object a = {
  532. { (uint32_t)sizeof(LV2_Atom_Object_Body), forge->Object },
  533. { id, otype }
  534. };
  535. return lv2_atom_forge_push(
  536. forge, frame, lv2_atom_forge_write(forge, &a, sizeof(a)));
  537. }
  538. /**
  539. The same as lv2_atom_forge_object(), but for object:Resource.
  540. This function is deprecated and should not be used in new code.
  541. Use lv2_atom_forge_object() directly instead.
  542. */
  543. LV2_ATOM_FORGE_DEPRECATED
  544. static inline LV2_Atom_Forge_Ref
  545. lv2_atom_forge_resource(LV2_Atom_Forge* forge,
  546. LV2_Atom_Forge_Frame* frame,
  547. LV2_URID id,
  548. LV2_URID otype)
  549. {
  550. #if defined(__clang__)
  551. # pragma clang diagnostic push
  552. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  553. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  554. # pragma GCC diagnostic push
  555. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  556. #endif
  557. const LV2_Atom_Object a = {
  558. { (uint32_t)sizeof(LV2_Atom_Object_Body), forge->Resource },
  559. { id, otype }
  560. };
  561. return lv2_atom_forge_push(
  562. forge, frame, lv2_atom_forge_write(forge, &a, sizeof(a)));
  563. #if defined(__clang__)
  564. # pragma clang diagnostic pop
  565. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  566. # pragma GCC diagnostic pop
  567. #endif
  568. }
  569. /**
  570. The same as lv2_atom_forge_object(), but for object:Blank.
  571. This function is deprecated and should not be used in new code.
  572. Use lv2_atom_forge_object() directly instead.
  573. */
  574. LV2_ATOM_FORGE_DEPRECATED
  575. static inline LV2_Atom_Forge_Ref
  576. lv2_atom_forge_blank(LV2_Atom_Forge* forge,
  577. LV2_Atom_Forge_Frame* frame,
  578. uint32_t id,
  579. LV2_URID otype)
  580. {
  581. #if defined(__clang__)
  582. # pragma clang diagnostic push
  583. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  584. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  585. # pragma GCC diagnostic push
  586. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  587. #endif
  588. const LV2_Atom_Object a = {
  589. { (uint32_t)sizeof(LV2_Atom_Object_Body), forge->Blank },
  590. { id, otype }
  591. };
  592. return lv2_atom_forge_push(
  593. forge, frame, lv2_atom_forge_write(forge, &a, sizeof(a)));
  594. #if defined(__clang__)
  595. # pragma clang diagnostic pop
  596. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  597. # pragma GCC diagnostic pop
  598. #endif
  599. }
  600. /**
  601. Write a property key in an Object, to be followed by the value.
  602. See lv2_atom_forge_object() documentation for an example.
  603. */
  604. static inline LV2_Atom_Forge_Ref
  605. lv2_atom_forge_key(LV2_Atom_Forge* forge,
  606. LV2_URID key)
  607. {
  608. const LV2_Atom_Property_Body a = { key, 0, { 0, 0 } };
  609. return lv2_atom_forge_write(forge, &a, 2 * (uint32_t)sizeof(uint32_t));
  610. }
  611. /**
  612. Write the header for a property body in an object, with context.
  613. If you do not need the context, which is almost certainly the case,
  614. use the simpler lv2_atom_forge_key() instead.
  615. */
  616. static inline LV2_Atom_Forge_Ref
  617. lv2_atom_forge_property_head(LV2_Atom_Forge* forge,
  618. LV2_URID key,
  619. LV2_URID context)
  620. {
  621. const LV2_Atom_Property_Body a = { key, context, { 0, 0 } };
  622. return lv2_atom_forge_write(forge, &a, 2 * (uint32_t)sizeof(uint32_t));
  623. }
  624. /**
  625. Write the header for a Sequence.
  626. */
  627. static inline LV2_Atom_Forge_Ref
  628. lv2_atom_forge_sequence_head(LV2_Atom_Forge* forge,
  629. LV2_Atom_Forge_Frame* frame,
  630. uint32_t unit)
  631. {
  632. const LV2_Atom_Sequence a = {
  633. { (uint32_t)sizeof(LV2_Atom_Sequence_Body), forge->Sequence },
  634. { unit, 0 }
  635. };
  636. return lv2_atom_forge_push(
  637. forge, frame, lv2_atom_forge_write(forge, &a, sizeof(a)));
  638. }
  639. /**
  640. Write the time stamp header of an Event (in a Sequence) in audio frames.
  641. After this, call the appropriate forge method(s) to write the body. Note
  642. the returned reference is to an LV2_Event which is NOT an Atom.
  643. */
  644. static inline LV2_Atom_Forge_Ref
  645. lv2_atom_forge_frame_time(LV2_Atom_Forge* forge, int64_t frames)
  646. {
  647. return lv2_atom_forge_write(forge, &frames, sizeof(frames));
  648. }
  649. /**
  650. Write the time stamp header of an Event (in a Sequence) in beats. After
  651. this, call the appropriate forge method(s) to write the body. Note the
  652. returned reference is to an LV2_Event which is NOT an Atom.
  653. */
  654. static inline LV2_Atom_Forge_Ref
  655. lv2_atom_forge_beat_time(LV2_Atom_Forge* forge, double beats)
  656. {
  657. return lv2_atom_forge_write(forge, &beats, sizeof(beats));
  658. }
  659. /**
  660. @}
  661. */
  662. #ifdef __cplusplus
  663. } /* extern "C" */
  664. #endif
  665. #endif /* LV2_ATOM_FORGE_H */