LV2 Toolkit  1.2.0
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 
82  {
83  public:
84 
85  OptionsIter (const Option* options)
86  : index (0),m_size (0), p_opts (options)
87  {
88  while (0 != next())
89  ++m_size;
90  index = 0;
91  }
92 
94  const Option* next()
95  {
96  if (p_opts == 0 || (p_opts[index].key == 0 &&
97  p_opts[index].value == 0))
98  return 0;
99 
100  return &p_opts[index++];
101  }
102 
104  uint32_t size() const { return m_size; }
105 
106  private:
107 
109  uint32_t index;
110 
112  uint32_t m_size;
113 
115  const Option* p_opts;
116 
117  };
118 
119 
129  template <bool Required = false>
130  struct Options
131  {
132 
133  template <class Derived>
134  struct I : Extension<Required>
135  {
136 
138  I() : p_supplied_opts (0) { }
139 
141  static void
143  {
144  hmap[LV2_OPTIONS__options] = &I<Derived>::handle_feature;
145  }
146 
148  static void
149  handle_feature (void* instance, FeatureData data)
150  {
151  Derived* plugin (reinterpret_cast<Derived*> (instance));
152  I<Derived>* mixin (static_cast<I<Derived>*> (plugin));
153  mixin->p_supplied_opts = (Option*) data;
154  mixin->m_ok = true;
155  }
156 
158  bool
159  check_ok()
160  {
161  if (LVTK_DEBUG) {
162  std::clog <<" [Options] validation "
163  <<(this->m_ok ? "succeeded" : "failed")<<"."<<std::endl;
164  }
165  return this->m_ok;
166  }
167 
169  static const void*
170  extension_data (const char* uri)
171  {
172  #if LVTK_OPTIONS_IFACE
173  if (! strcmp (uri, LV2_OPTIONS__interface)) {
174  static LV2_Options_Interface options = { &I<Derived>::_get,
175  &I<Derived>::_set };
176  return &options;
177  }
178  #endif
179  return 0;
180  }
181 
187  const Option* get_supplied_options() { return p_supplied_opts; }
188 
189  protected:
190 
201  uint32_t get_options (Option*) { return OPTIONS_SUCCESS; }
202 
203 
204 
205 
215  uint32_t set_options (const Option*) { return OPTIONS_SUCCESS; }
216 
217  private:
218 
223  Option* p_supplied_opts;
224 
225  /* LV2 Options Implementation */
226 
227  static uint32_t _get (LV2_Handle handle, LV2_Options_Option* options)
228  {
229  Derived* plugin (reinterpret_cast<Derived*> (handle));
230  return plugin->get_options (options);
231  }
232 
233  static uint32_t _set (LV2_Handle handle, const LV2_Options_Option* options)
234  {
235  Derived* plugin (reinterpret_cast<Derived*> (handle));
236  return plugin->set_options (options);
237  }
238 
239  };
240  };
241 
242 } /* namespace lvtk */
243 
244 #endif /* LVTK_OPTIONS_HPP */
uint32_t get_options(Option *)
Definition: options.hpp:201
uint32_t size() const
Definition: options.hpp:104
Definition: options.hpp:67
Definition: options.hpp:45
void * FeatureData
Definition: feature.hpp:47
const Option * next()
Definition: options.hpp:94
Definition: options.hpp:81
static void map_feature_handlers(FeatureHandlerMap &hmap)
Definition: options.hpp:142
Definition: options.hpp:57
Definition: options.hpp:49
static const void * extension_data(const char *uri)
Definition: options.hpp:170
Definition: feature.hpp:34
const Option * get_supplied_options()
Definition: options.hpp:187
Definition: options.hpp:130
Definition: options.hpp:134
uint32_t set_options(const Option *)
Definition: options.hpp:215
LV2_Options_Option Option
Definition: options.hpp:39
Definition: options.hpp:64
Definition: options.hpp:66
OptionsContext
Definition: options.hpp:42
OptionsStatus
Definition: options.hpp:62
I()
Definition: options.hpp:138
Definition: options.hpp:65
Definition: options.hpp:53
map< string, FeatureHandler > FeatureHandlerMap
Definition: feature.hpp:57
Definition: options.hpp:63