LV2 Toolkit  1.1.1
 All Classes Namespaces Functions Typedefs Enumerations Enumerator Groups Pages
atom.hpp
1 /*
2  atom.hpp - support file for writing LV2 plugins in C++
3 
4  Copyright (C) 2012 Michael Fisher <mfisher31@gmail.com>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 01222-1307 USA
19 */
25 #ifndef LVTK_ATOM_HPP
26 #define LVTK_ATOM_HPP
27 
28 #include <lv2/lv2plug.in/ns/ext/atom/atom.h>
29 #include <lv2/lv2plug.in/ns/ext/atom/forge.h>
30 #include <lv2/lv2plug.in/ns/ext/atom/util.h>
31 
32 namespace lvtk {
33 
35  typedef LV2_Atom_Event AtomEvent;
36  typedef LV2_Atom_Forge_Frame ForgeFrame;
37  typedef LV2_Atom_Forge_Ref ForgeRef;
38  typedef LV2_Atom_Object_Query ObjectQuery;
39 
40 
44  struct AtomObject
45  {
47  AtomObject (const void* atom) : p_obj ((LV2_Atom_Object*) atom) { }
48 
50  AtomObject (ForgeRef ref) : p_obj ((LV2_Atom_Object*) ref) { }
51 
52  AtomObject (const AtomObject& other) : p_obj (other.p_obj) { }
53 
55  inline uint32_t
56  otype() const
57  {
58  return p_obj->body.otype;
59  }
60 
62  inline uint32_t
63  id() const
64  {
65  return p_obj->body.id;
66  }
67 
69  inline uint32_t
70  total_size() const
71  {
72  return lv2_atom_total_size ((LV2_Atom*) p_obj);
73  }
74 
87  inline void
88  query (ObjectQuery& query)
89  {
90  lv2_atom_object_query (p_obj, &query);
91  }
92 
94  inline LV2_Atom_Object* cobj() const { return p_obj; }
95 
97  inline operator LV2_Atom_Object*() { return p_obj; }
98 
100  inline AtomObject&
101  operator= (const AtomObject& other)
102  {
103  p_obj = other.p_obj;
104  return *this;
105  }
106 
107  private:
108 
109  LV2_Atom_Object* p_obj;
110 
111  };
112 
113 
117  struct Atom
118  {
120  Atom () : p_atom (0) { }
121 
123  Atom (const void* atom) : p_atom ((LV2_Atom*) atom) { }
124 
126  Atom (ForgeRef ref) : p_atom ((LV2_Atom*) ref) { }
127 
129  Atom (AtomEvent* ev) : p_atom (&ev->body) { }
130 
132  inline static uint32_t
133  pad_size (uint32_t size)
134  {
135  return lv2_atom_pad_size (size);
136  }
137 
139  inline bool
141  {
142  return lv2_atom_is_null (p_atom);
143  }
144 
146  inline void*
147  body() const
148  {
149  return LV2_ATOM_BODY (p_atom);
150  }
151 
153  inline float
154  as_float() const
155  {
156  return ((LV2_Atom_Float*)p_atom)->body;
157  }
158 
160  const AtomObject
161  as_object() const {
162  return AtomObject ((LV2_Atom_Object* ) p_atom);
163  }
164 
166  inline const char*
167  as_string() const
168  {
169  return (const char*) LV2_ATOM_BODY (p_atom);
170  }
171 
173  inline int
174  as_int() const
175  {
176  return ((LV2_Atom_Int*)p_atom)->body;
177  }
178 
180  inline int64_t
181  as_long() const
182  {
183  return ((LV2_Atom_Long*)p_atom)->body;
184  }
185 
187  inline uint32_t
188  as_urid() const
189  {
190  return ((LV2_Atom_URID*)p_atom)->body;
191  }
194  inline uint32_t
195  type() const
196  {
197  return p_atom->type;
198  }
199 
201  inline uint32_t
202  total_size() const
203  {
204  return lv2_atom_total_size (p_atom);
205  }
206 
207 
209  inline uint32_t
210  size() const
211  {
212  return p_atom->size;
213  }
214 
216  inline const LV2_Atom*
217  cobj() const
218  {
219  return p_atom;
220  }
221 
223  inline operator const LV2_Atom*() { return cobj(); }
224 
226  inline Atom&
227  operator= (const Atom& other)
228  {
229  p_atom = other.p_atom;
230  return *this;
231  }
232 
234  inline bool
235  operator== (Atom& other)
236  {
237  return lv2_atom_equals (cobj(), other.cobj());
238  }
239 
240  private:
241 
242  const LV2_Atom* p_atom;
243  friend class AtomObject;
244 
245  };
246 
247 
250  {
252  AtomSequence (const void* slab) : p_seq ((LV2_Atom_Sequence*) slab) { }
253 
255  AtomSequence (LV2_Atom_Sequence* seq) : p_seq (seq) { }
256 
258  AtomSequence (ForgeRef ref) : p_seq ((LV2_Atom_Sequence*) ref) { }
259 
261  inline uint32_t
262  pad() const
263  {
264  return p_seq->body.pad;
265  }
266 
268  inline uint32_t
269  size() const
270  {
271  return p_seq->atom.size;
272  }
273 
275  inline uint32_t
276  unit() const
277  {
278  return p_seq->body.unit;
279  }
280 
282  inline LV2_Atom_Sequence*
284  {
285  return p_seq;
286  }
287 
289  inline operator LV2_Atom_Sequence*() const { return p_seq; }
290 
292  inline operator uint8_t*() const { return (uint8_t*) p_seq; }
293 
294  private:
295 
296  LV2_Atom_Sequence* p_seq;
297  };
298 
299 
301  class AtomForge
302  {
303  public:
304 
310  AtomForge() { }
311 
313  AtomForge (LV2_URID_Map* map)
314  {
315  init (map);
316  }
317 
321  inline void
322  init (LV2_URID_Map* map)
323  {
324  lv2_atom_forge_init (&forge, map);
325  }
326 
330  inline LV2_Atom_Forge*
332  {
333  return &forge;
334  }
335 
337  inline ForgeRef
338  sequence_head (ForgeFrame& frame, uint32_t unit)
339  {
340  return lv2_atom_forge_sequence_head (&forge, &frame, unit);
341  }
342 
343  inline operator LV2_Atom_Forge* () { return cobj(); }
344 
350  inline void
351  set_buffer (uint8_t* buf, uint32_t size)
352  {
353  lv2_atom_forge_set_buffer (&forge, buf, size);
354  }
355 
357  inline ForgeRef
358  beat_time (double beats)
359  {
360  return lv2_atom_forge_beat_time (&forge, beats);
361  }
362 
366  inline ForgeRef
367  frame_time (int64_t frames)
368  {
369  return lv2_atom_forge_frame_time (&forge, frames);
370  }
371 
372  inline ForgeRef
373  property_head (uint32_t key, uint32_t context)
374  {
375  return lv2_atom_forge_property_head (&forge, key, context);
376  }
377 
378  inline void
379  pop (ForgeFrame& frame)
380  {
381  lv2_atom_forge_pop (&forge, &frame);
382  }
383 
390  inline ForgeRef
391  write_atom (uint32_t size, uint32_t type)
392  {
393  return lv2_atom_forge_atom (&forge, size, type);
394  }
395 
401  inline ForgeRef
402  write_path (const std::string& path)
403  {
404  return lv2_atom_forge_path (&forge, path.c_str(), path.size());
405  }
406 
414  inline ForgeRef
415  write_resource (ForgeFrame& frame, uint32_t id, uint32_t otype)
416  {
417  // Write object header
418  return lv2_atom_forge_resource (&forge, &frame, id, otype);
419  }
420 
421  inline ForgeRef
422  write_blank (ForgeFrame& frame, uint32_t id, uint32_t otype)
423  {
424  // Write object header
425  return lv2_atom_forge_blank (&forge, &frame, id, otype);
426  }
427 
428  inline ForgeRef
429  write_bool (const bool val)
430  {
431  return lv2_atom_forge_bool (&forge, val);
432  }
433 
434  inline ForgeRef
435  write_int (const int val)
436  {
437  return lv2_atom_forge_int (&forge, val);
438  }
439 
440 
441  inline ForgeRef
442  write_float (const float val)
443  {
444  return lv2_atom_forge_float (&forge, val);
445  }
446 
447  inline ForgeRef
448  write_long (const int64_t val)
449  {
450  return lv2_atom_forge_long (&forge, val);
451  }
452 
453  inline ForgeRef
454  write_string (const char* str)
455  {
456  return lv2_atom_forge_string (&forge, str, strlen (str));
457  }
458 
459  inline ForgeRef
460  write_uri (const char* uri)
461  {
462  return lv2_atom_forge_uri (&forge, uri, strlen (uri));
463  }
464 
465  inline ForgeRef
466  write_raw (const void* data, uint32_t size)
467  {
468  return lv2_atom_forge_raw (&forge, data, size);
469  }
470 
471  inline ForgeRef
472  write_urid (LV2_URID id)
473  {
474  return lv2_atom_forge_urid (&forge, id);
475  }
476 
477  private:
478 
479  LV2_Atom_Forge forge;
480  };
481 
482 } /* namespace lvtk */
483 
484 #endif /* LVTK_ATOM_HPP */