Open Lighting Architecture  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WidgetFactory.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  * WidgetFactory.h
17  * Creates USB Widgets.
18  * Copyright (C) 2014 Simon Newton
19  */
20 
21 #ifndef PLUGINS_USBDMX_WIDGETFACTORY_H_
22 #define PLUGINS_USBDMX_WIDGETFACTORY_H_
23 
24 #include <libusb.h>
25 #include <map>
26 #include "ola/Logging.h"
27 #include "ola/base/Macro.h"
28 #include "ola/stl/STLUtils.h"
29 
30 namespace ola {
31 namespace plugin {
32 namespace usbdmx {
33 
48  public:
49  virtual ~WidgetObserver() {}
50 
58  virtual bool NewWidget(class AnymauDMX *widget) = 0;
59 
67  virtual bool NewWidget(class EurolitePro *widget) = 0;
68 
76  virtual bool NewWidget(class JaRuleWidget *widget) = 0;
77 
85  virtual bool NewWidget(class ScanlimeFadecandy *widget) = 0;
86 
94  virtual bool NewWidget(class Sunlite *widget) = 0;
95 
103  virtual bool NewWidget(class VellemanK8062 *widget) = 0;
104 
111  virtual void WidgetRemoved(class AnymauDMX *widget) = 0;
112 
119  virtual void WidgetRemoved(class EurolitePro *widget) = 0;
120 
127  virtual void WidgetRemoved(class JaRuleWidget *widget) = 0;
128 
135  virtual void WidgetRemoved(class ScanlimeFadecandy *widget) = 0;
136 
143  virtual void WidgetRemoved(class Sunlite *widget) = 0;
144 
151  virtual void WidgetRemoved(class VellemanK8062 *widget) = 0;
152 };
153 
167  public:
168  virtual ~WidgetFactory() {}
169 
179  virtual bool DeviceAdded(
180  WidgetObserver *observer,
181  libusb_device *usb_device,
182  const struct libusb_device_descriptor &descriptor) = 0;
183 
190  virtual void DeviceRemoved(WidgetObserver *observer,
191  libusb_device *usb_device) = 0;
192 };
193 
200 template <typename WidgetType>
202  public:
203  BaseWidgetFactory() {}
204 
205  void DeviceRemoved(WidgetObserver *observer,
206  libusb_device *device);
207 
208  protected:
215  bool HasDevice(libusb_device *usb_device) {
216  return STLContains(m_widget_map, usb_device);
217  }
218 
223  unsigned int DeviceCount() const {
224  return m_widget_map.size();
225  }
226 
234  bool AddWidget(WidgetObserver *observer, libusb_device *usb_device,
235  WidgetType *widget);
236 
237  private:
238  typedef std::map<libusb_device*, WidgetType*> WidgetMap;
239 
240  WidgetMap m_widget_map;
241 
242  DISALLOW_COPY_AND_ASSIGN(BaseWidgetFactory<WidgetType>);
243 };
244 
245 template <typename WidgetType>
247  libusb_device *usb_device,
248  WidgetType *widget) {
249  if (!widget->Init()) {
250  delete widget;
251  return false;
252  }
253 
254  if (!observer->NewWidget(widget)) {
255  delete widget;
256  return false;
257  }
258 
259  WidgetType *old_widget = STLReplacePtr(&m_widget_map, usb_device, widget);
260  if (old_widget) {
261  // This should never happen.
262  OLA_WARN << "Widget conflict for " << usb_device;
263  observer->WidgetRemoved(old_widget);
264  delete old_widget;
265  }
266  return true;
267 }
268 
269 template <typename WidgetType>
271  libusb_device *usb_device) {
272  WidgetType *widget = STLLookupAndRemovePtr(&m_widget_map, usb_device);
273  if (widget) {
274  observer->WidgetRemoved(widget);
275  delete widget;
276  }
277 }
278 } // namespace usbdmx
279 } // namespace plugin
280 } // namespace ola
281 #endif // PLUGINS_USBDMX_WIDGETFACTORY_H_