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

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