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

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