LV2 Toolkit  1.1.1
 All Classes Namespaces Functions Typedefs Enumerations Enumerator Groups Pages
urid.hpp
1 /****************************************************************************
2 
3  urid.hpp - support file for writing LV2 plugins in C++
4 
5  Copyright (C) 2012 Michael Fisher <mfisher31@gmail.com>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 01222-1307 USA
20 
21  ****************************************************************************/
22 
23 #ifndef LVTK_LV2_URID_HPP
24 #define LVTK_LV2_URID_HPP
25 
26 #include <lv2/lv2plug.in/ns/ext/urid/urid.h>
27 
28 namespace lvtk
29 {
31  typedef uint32_t (*MapFunc)(const char* symbol);
32 
34  typedef const char* (*UnmapFunc)(uint32_t id);
35 
42  template<bool Required = true>
43  struct URID
44  {
45  template<class Derived>
46  struct I : Extension<Required>
47  {
48  I() : p_unmap(NULL), p_map(NULL) { }
49 
51  static void
52  map_feature_handlers (FeatureHandlerMap& hmap)
53  {
54  hmap[LV2_URID__map] = &I<Derived>::handle_map_feature;
55  hmap[LV2_URID__unmap] = &I<Derived>::handle_unmap_feature;
56  }
57 
59  static void
60  handle_map_feature (void* instance, void* data)
61  {
62  Derived* d = reinterpret_cast<Derived*>(instance);
63  I<Derived>* mixin = static_cast<I<Derived>*>(d);
64 
65  mixin->p_map = reinterpret_cast<LV2_URID_Map*>(data);
66  mixin->m_ok = true;
67  }
68 
70  static void
71  handle_unmap_feature (void* instance, void* data)
72  {
73  Derived* d = reinterpret_cast<Derived*>(instance);
74  I<Derived>* mixin = static_cast<I<Derived>*>(d);
75 
76  mixin->p_unmap =
77  reinterpret_cast<LV2_URID_Unmap*>(data);
78  mixin->m_ok = true;
79  }
80 
81  bool
82  check_ok()
83  {
84  if (LVTK_DEBUG)
85  {
86  std::clog << " [URID] Validation "
87  << (this->m_ok ? "succeeded" : "failed")
88  << "." << std::endl;
89  }
90  return this->m_ok;
91  }
92 
93 
94 
109  const char*
110  unmap (LV2_URID urid)
111  {
112  if (p_unmap != NULL)
113  return p_unmap->unmap(p_unmap->handle, urid);
114  return "";
115  }
116 
137  LV2_URID
138  map (const char* uri)
139  {
140  if (p_map != NULL)
141  return p_map->map(p_map->handle, uri);
142  return 0;
143  }
144 
145  protected:
146 
147  LV2_URID_Map *p_map;
148  LV2_URID_Unmap *p_unmap;
149 
150  };
151  };
152 }
153 
154 #endif /* LVTK_LV2_URID_HPP */