LV2 Toolkit  1.2.0
state.hpp
1 /*
2  state.hpp - support file for writing LV2 plugins in C++
3  Copyright (C) 2012 Michael Fisher <mfisher31@gmail.com>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 01222-1307 USA
18  */
19 
20 #ifndef LVTK_STATE_HPP
21 #define LVTK_STATE_HPP
22 
23 #include <cassert>
24 #include <lv2/lv2plug.in/ns/ext/state/state.h>
25 
26 namespace lvtk {
27 
28  typedef enum {
41  STATE_IS_POD = LV2_STATE_IS_POD,
42 
51  STATE_IS_PORTABLE = LV2_STATE_IS_PORTABLE,
52 
61  STATE_IS_NATIVE = LV2_STATE_IS_NATIVE
62  } StateFlags;
63 
64  typedef enum {
65  STATE_SUCCESS = LV2_STATE_SUCCESS,
66  STATE_ERR_UNKNOWN = LV2_STATE_ERR_UNKNOWN,
67  STATE_ERR_BAD_TYPE = LV2_STATE_ERR_BAD_TYPE,
68  STATE_ERR_BAD_FLAGS = LV2_STATE_ERR_BAD_FLAGS,
69  STATE_ERR_NO_FEATURE = LV2_STATE_ERR_NO_FEATURE,
70  STATE_ERR_NO_PROPERTY = LV2_STATE_ERR_NO_PROPERTY
71  } StateStatus;
72 
73 
76  struct StateRetrieve {
77  StateRetrieve(LV2_State_Retrieve_Function srfunc, LV2_State_Handle handle)
78  : p_handle(handle), p_srfunc(srfunc) { }
79 
88  const void* operator () (uint32_t key, size_t *size = NULL,
89  uint32_t *type = NULL,
90  uint32_t *flags = NULL) const
91  {
92  return p_srfunc(p_handle, key, size, type, flags);
93  }
94 
95  private:
96  LV2_State_Handle p_handle;
97  LV2_State_Retrieve_Function p_srfunc;
98  };
99 
100  /* A little redundant */
101 
104  struct StateStore {
105  StateStore (LV2_State_Store_Function ssfunc, LV2_State_Handle handle)
106  : p_handle(handle), p_ssfunc(ssfunc) { }
107 
117  inline StateStatus operator () (uint32_t key, const void* value,
118  size_t size,
119  uint32_t type,
120  uint32_t flags = 0) const
121  {
122  return (StateStatus) p_ssfunc (p_handle, key, value, size, type, flags);
123  }
124 
125  private:
126  LV2_State_Handle p_handle;
127  LV2_State_Store_Function p_ssfunc;
128  };
129 
136  template <bool Required = true>
137  struct State
138  {
139  template <class Derived>
140  struct I : Extension<Required>
141  {
142 
143  I() : p_make_path (NULL) { }
144 
146  static void
148  {
151  hmap[LV2_STATE__makePath] = &I<Derived>::handle_make_feature;
152  }
153 
155  static void
156  handle_make_feature(void* instance, FeatureData data)
157  {
158  Derived* d = reinterpret_cast<Derived*>(instance);
159  I<Derived>* mixin = static_cast<I<Derived>*>(d);
160  mixin->p_make_path =
161  reinterpret_cast<LV2_State_Make_Path*> (data);
162  }
163 
164  bool
165  check_ok()
166  {
167  if (Required) {
168  this->m_ok = (p_make_path != NULL);
169  } else {
170  this->m_ok = true;
171  }
172 
173  if (LVTK_DEBUG) {
174  std::clog<<" [State] Validation "
175  <<(this->m_ok ? "succeeded" : "failed")<<"."
176  <<std::endl;
177  }
178 
179  return this->m_ok;
180  }
181 
183  static const void*
184  extension_data (const char* uri)
185  {
186  if (!std::strcmp (uri, LV2_STATE__interface))
187  {
188  static LV2_State_Interface state = { &I<Derived>::_save,
190  return &state;
191  }
192  return 0;
193  }
194 
195 
196  StateStatus
197  save (StateStore &store, uint32_t flags,
198  const FeatureVec &features)
199  {
200  return STATE_SUCCESS;
201  }
202 
203  StateStatus
204  restore (StateRetrieve &retrieve, uint32_t flags,
205  const FeatureVec &features)
206  {
207  return STATE_SUCCESS;
208  }
209 
210  protected:
211 
212 #if 0
213  SAVEME - Map Path is for the State interface methods only.
214 
215  char*
216  abstract_path (const char* absolute_path);
217 
218  char*
219  absolute_path(const char* abstract_path)
220  {
221  return p_map_path->absolute_path (p_map_path->handle, abstract_path);
222  }
223 #endif
224 
235  char*
236  path (const char* path)
237  {
238  assert (p_make_path != NULL);
239  return p_make_path->path (p_make_path->handle, path);
240  }
241 
242 
243  /* ============== LV2 Boiler Plate Implementation ================= */
244 
245  LV2_State_Make_Path * p_make_path;
246 
248  static LV2_State_Status _save(LV2_Handle instance,
249  LV2_State_Store_Function store,
250  LV2_State_Handle handle,
251  uint32_t flags,
252  const LV2_Feature *const * features)
253  {
254  Derived* plugin = reinterpret_cast<Derived*>(instance);
255 
256  StateStore ss (store, handle);
257 
258  FeatureVec feature_set;
259  for (int i = 0; features[i]; ++i) {
260  feature_set.push_back (features[i]);
261  }
262 
263  return (LV2_State_Status)plugin->save(ss, flags, feature_set);
264  }
265 
267  static LV2_State_Status _restore(LV2_Handle instance,
268  LV2_State_Retrieve_Function retrieve,
269  LV2_State_Handle handle,
270  uint32_t flags,
271  const LV2_Feature *const * features)
272  {
273  Derived* plugin = reinterpret_cast<Derived*>(instance);
274 
276  StateRetrieve sr (retrieve, handle);
277 
279  FeatureVec feature_set;
280  for (int i = 0; features[i]; ++i) {
281  feature_set.push_back (features[i]);
282  }
283 
284  return (LV2_State_Status)plugin->restore(sr, flags, feature_set);
285  }
286  };
287  };
288 
289 } /* namespace lvtk */
290 
291 #endif /* LVTK_STATE_HPP */
Definition: state.hpp:66
Definition: state.hpp:69
static void map_feature_handlers(FeatureHandlerMap &hmap)
Definition: state.hpp:147
void * FeatureData
Definition: feature.hpp:47
Definition: state.hpp:61
Definition: state.hpp:41
Definition: feature.hpp:34
char * path(const char *path)
Definition: state.hpp:236
Definition: state.hpp:68
Definition: state.hpp:137
Definition: state.hpp:76
Definition: state.hpp:70
static LV2_State_Status _restore(LV2_Handle instance, LV2_State_Retrieve_Function retrieve, LV2_State_Handle handle, uint32_t flags, const LV2_Feature *const *features)
Definition: state.hpp:267
Definition: state.hpp:51
vector< const Feature * > FeatureVec
Definition: feature.hpp:44
Definition: state.hpp:67
StateFlags
Definition: state.hpp:28
Definition: state.hpp:65
Definition: state.hpp:104
StateStatus
Definition: state.hpp:64
map< string, FeatureHandler > FeatureHandlerMap
Definition: feature.hpp:57
const void * operator()(uint32_t key, size_t *size=NULL, uint32_t *type=NULL, uint32_t *flags=NULL) const
Definition: state.hpp:88
Definition: state.hpp:140