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