LV2 Toolkit  1.2.0
worker.hpp
1 /*
2 
3  worker.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_WORKER_HPP
24 #define LVTK_WORKER_HPP
25 
26 #include <cstring>
27 
28 #include <lv2/lv2plug.in/ns/ext/worker/worker.h>
29 
30 #include "lvtk/private/types.hpp"
31 
32 namespace lvtk {
33 
35  typedef enum {
36  WORKER_SUCCESS = LV2_WORKER_SUCCESS,
37  WORKER_ERR_UNKNOWN = LV2_WORKER_ERR_UNKNOWN,
38  WORKER_ERR_NO_SPACE = LV2_WORKER_ERR_NO_SPACE
39  } WorkerStatus;
40 
41 
46  struct WorkerRespond {
47  WorkerRespond(LV2_Handle instance,
48  LV2_Worker_Respond_Function wrfunc,
49  LV2_Worker_Respond_Handle handle)
50  : p_instance(instance),
51  p_handle(handle),
52  p_wrfunc (wrfunc)
53  { }
54 
61  WorkerStatus operator () (uint32_t size, const void* data) const
62  {
63  return (WorkerStatus) p_wrfunc (p_handle, size, data);
64  }
65 
66  private:
67  LV2_Handle p_instance;
68  LV2_Worker_Respond_Handle p_handle;
69  LV2_Worker_Respond_Function p_wrfunc;
70  };
71 
76  template <bool Required = true>
77  struct Worker
78  {
79  template <class Derived>
80  struct I : Extension<Required>
81  {
82 
84  static void
86  {
87  hmap[LV2_WORKER__schedule] = &I<Derived>::handle_feature;
88  }
89 
91  static void
92  handle_feature(void* instance, FeatureData data)
93  {
94  Derived* d = reinterpret_cast<Derived*>(instance);
95  I<Derived>* fe = static_cast<I<Derived>*>(d);
96  LV2_Worker_Schedule *ws = reinterpret_cast<LV2_Worker_Schedule*>(data);
97 
98  fe->m_work_schedule_handle = ws->handle;
99  fe->m_schedule_work_func = ws->schedule_work;
100  fe->m_ok = true;
101  }
102 
104  bool
106  {
107  if (LVTK_DEBUG) {
108  std::clog<<" [Worker] validation "
109  <<(this->m_ok ? "succeeded" : "failed")<<"."<<std::endl;
110  }
111  return this->m_ok;
112  }
113 
115  static const void*
116  extension_data (const char* uri)
117  {
118  if (!std::strcmp (uri, LV2_WORKER__interface)) {
119  static LV2_Worker_Interface worker = { &I<Derived>::_work,
122  return &worker;
123  }
124 
125  return 0;
126  }
127 
128  /* =============== LV2 Worker C++ Interface =============== */
129 
130 
156  WorkerStatus
157  schedule_work (uint32_t size, const void* data)
158  {
159  return (WorkerStatus)m_schedule_work_func(
160  m_work_schedule_handle, size, data);
161  }
162 
174  WorkerStatus
175  work (WorkerRespond &respond, uint32_t size, const void* data)
176  {
177  return WORKER_SUCCESS;
178  }
179 
180 
188  WorkerStatus
189  work_response (uint32_t size, const void* body)
190  {
191  return WORKER_SUCCESS;
192  }
193 
194 
205  WorkerStatus
207  {
208  return WORKER_SUCCESS;
209  }
210 
211  protected:
212 
213  /* LV2 Worker Implementation */
214 
216  LV2_Worker_Schedule_Handle m_work_schedule_handle;
217 
219  LV2_Worker_Status (*m_schedule_work_func)(LV2_Worker_Schedule_Handle handle,
220  uint32_t size,
221  const void* data);
222 
224  static LV2_Worker_Status _work(LV2_Handle instance,
225  LV2_Worker_Respond_Function respond,
226  LV2_Worker_Respond_Handle handle,
227  uint32_t size,
228  const void* data)
229  {
230  Derived* plugin = reinterpret_cast<Derived*>(instance);
231  WorkerRespond wrsp (instance, respond, handle);
232  return (LV2_Worker_Status)plugin->work(wrsp, size, data);
233  }
234 
236  static LV2_Worker_Status _work_response (LV2_Handle instance,
237  uint32_t size,
238  const void* body)
239  {
240  Derived* plugin = reinterpret_cast<Derived*>(instance);
241  return (LV2_Worker_Status)plugin->work_response (size, body);
242  }
243 
245  static LV2_Worker_Status _end_run (LV2_Handle instance)
246  {
247  Derived* plugin = reinterpret_cast<Derived*>(instance);
248  return (LV2_Worker_Status)plugin->end_run();
249  }
250  };
251  };
252 
253 } /* namespace lvtk */
254 
255 #endif /* LVTK_WORKER_HPP */
static void map_feature_handlers(FeatureHandlerMap &hmap)
Definition: worker.hpp:85
static void handle_feature(void *instance, FeatureData data)
Definition: worker.hpp:92
WorkerStatus schedule_work(uint32_t size, const void *data)
Definition: worker.hpp:157
Definition: worker.hpp:80
bool check_ok()
Definition: worker.hpp:105
void * FeatureData
Definition: feature.hpp:47
WorkerStatus work(WorkerRespond &respond, uint32_t size, const void *data)
Definition: worker.hpp:175
Definition: worker.hpp:38
Definition: worker.hpp:46
Definition: feature.hpp:34
Definition: worker.hpp:36
WorkerStatus operator()(uint32_t size, const void *data) const
Definition: worker.hpp:61
WorkerStatus end_run()
Definition: worker.hpp:206
Definition: worker.hpp:77
WorkerStatus
Definition: worker.hpp:35
Definition: worker.hpp:37
static const void * extension_data(const char *uri)
Definition: worker.hpp:116
WorkerStatus work_response(uint32_t size, const void *body)
Definition: worker.hpp:189
map< string, FeatureHandler > FeatureHandlerMap
Definition: feature.hpp:57