LV2 Toolkit  1.1.1
 All Classes Namespaces Functions Typedefs Enumerations Enumerator Groups Pages
options.hpp
1 /*
2  options.hpp - Support file for writing LV2 plugins in C++
3 
4  Copyright (C) 2013 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 */
20 
25 #ifndef LVTK_OPTIONS_HPP
26 #define LVTK_OPTIONS_HPP
27 
28 #include <lv2/lv2plug.in/ns/ext/options/options.h>
29 
30 
31 #ifndef LVTK_OPTIONS_IFACE
32 #define LVTK_OPTIONS_IFACE 1
33 #endif
34 
35 namespace lvtk {
36 
37 
39  typedef LV2_Options_Option Option;
40 
42  typedef enum {
45  OPTIONS_INSTANCE = LV2_OPTIONS_INSTANCE,
46 
49  OPTIONS_RESOURCE = LV2_OPTIONS_RESOURCE,
50 
53  OPTIONS_BLANK = LV2_OPTIONS_BLANK,
54 
57  OPTIONS_PORT = LV2_OPTIONS_PORT
59 
60 
62  typedef enum {
63  OPTIONS_SUCCESS = LV2_OPTIONS_SUCCESS,
64  OPTIONS_ERR_UNKNOWN = LV2_OPTIONS_ERR_UNKNOWN,
65  OPTIONS_ERR_BAD_SUBJECT = LV2_OPTIONS_ERR_BAD_SUBJECT,
66  OPTIONS_ERR_BAD_KEY = LV2_OPTIONS_ERR_BAD_KEY,
67  OPTIONS_ERR_BAD_VALUE = LV2_OPTIONS_ERR_BAD_VALUE
68  } OptionsStatus;
69 
70 
72  {
73  public:
74 
75  OptionsIter (const Option* options)
76  : index (0),m_size (0), p_opts (options)
77  {
78  while (0 != next())
79  ++m_size;
80  index = 0;
81  }
82 
84  const Option* next()
85  {
86  if (p_opts == 0 || (p_opts[index].key == 0 &&
87  p_opts[index].value == 0))
88  return 0;
89 
90  return &p_opts[index++];
91  }
92 
93  uint32_t size() const { return m_size; }
94 
95  private:
96 
98  uint32_t index;
99 
101  uint32_t m_size;
102 
104  const Option* p_opts;
105 
106  };
107 
108 
118  template <bool Required = false>
119  struct Options
120  {
121 
122  template <class Derived>
123  struct I : Extension<Required>
124  {
125 
127  I() : p_supplied_opts (0) { }
128 
130  static void
132  {
133  hmap[LV2_OPTIONS__options] = &I<Derived>::handle_feature;
134  }
135 
137  static void
138  handle_feature (void* instance, FeatureData data)
139  {
140  Derived* plugin (reinterpret_cast<Derived*> (instance));
141  I<Derived>* mixin (static_cast<I<Derived>*> (plugin));
142  mixin->p_supplied_opts = (Option*) data;
143  mixin->m_ok = true;
144  }
145 
147  bool
148  check_ok()
149  {
150  if (LVTK_DEBUG) {
151  std::clog <<" [Options] validation "
152  <<(this->m_ok ? "succeeded" : "failed")<<"."<<std::endl;
153  }
154  return this->m_ok;
155  }
156 
158  static const void*
159  extension_data (const char* uri)
160  {
161  #if LVTK_OPTIONS_IFACE
162  if (! strcmp (uri, LV2_OPTIONS__interface)) {
163  static LV2_Options_Interface options = { &I<Derived>::_get,
164  &I<Derived>::_set };
165  return &options;
166  }
167  #endif
168  return 0;
169  }
170 
176  const Option* get_supplied_options() { return p_supplied_opts; }
177 
178  protected:
179 
190  uint32_t get_options (Option*) { return OPTIONS_SUCCESS; }
191 
192 
193 
194 
204  uint32_t set_options (const Option*) { return OPTIONS_SUCCESS; }
205 
206  private:
207 
212  Option* p_supplied_opts;
213 
214  /* LV2 Options Implementation */
215 
216  static uint32_t _get (LV2_Handle handle, LV2_Options_Option* options)
217  {
218  Derived* plugin (reinterpret_cast<Derived*> (handle));
219  return plugin->get_options (options);
220  }
221 
222  static uint32_t _set (LV2_Handle handle, const LV2_Options_Option* options)
223  {
224  Derived* plugin (reinterpret_cast<Derived*> (handle));
225  return plugin->set_options (options);
226  }
227 
228  };
229  };
230 
231 } /* namespace lvtk */
232 
233 #endif /* LVTK_OPTIONS_HPP */