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.

755 lines
23KB

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