Open Lighting Architecture  0.9.5
 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 ScanlimeFadecandy *widget) = 0;
77 
85  virtual bool NewWidget(class Sunlite *widget) = 0;
86 
94  virtual bool NewWidget(class VellemanK8062 *widget) = 0;
95 
102  virtual void WidgetRemoved(class AnymauDMX *widget) = 0;
103 
110  virtual void WidgetRemoved(class EurolitePro *widget) = 0;
111 
118  virtual void WidgetRemoved(class ScanlimeFadecandy *widget) = 0;
119 
126  virtual void WidgetRemoved(class Sunlite *widget) = 0;
127 
134  virtual void WidgetRemoved(class VellemanK8062 *widget) = 0;
135 };
136 
150  public:
151  virtual ~WidgetFactory() {}
152 
162  virtual bool DeviceAdded(
163  WidgetObserver *observer,
164  libusb_device *usb_device,
165  const struct libusb_device_descriptor &descriptor) = 0;
166 
173  virtual void DeviceRemoved(WidgetObserver *observer,
174  libusb_device *usb_device) = 0;
175 };
176 
183 template <typename WidgetType>
185  public:
186  BaseWidgetFactory() {}
187 
188  void DeviceRemoved(WidgetObserver *observer,
189  libusb_device *device);
190 
191  protected:
198  bool HasDevice(libusb_device *usb_device) {
199  return STLContains(m_widget_map, usb_device);
200  }
201 
209  bool AddWidget(WidgetObserver *observer, libusb_device *usb_device,
210  WidgetType *widget);
211 
212  private:
213  typedef std::map<libusb_device*, WidgetType*> WidgetMap;
214 
215  WidgetMap m_widget_map;
216 
217  DISALLOW_COPY_AND_ASSIGN(BaseWidgetFactory<WidgetType>);
218 };
219 
220 template <typename WidgetType>
222  libusb_device *usb_device,
223  WidgetType *widget) {
224  if (!widget->Init()) {
225  delete widget;
226  return false;
227  }
228 
229  if (!observer->NewWidget(widget)) {
230  delete widget;
231  return false;
232  }
233 
234  WidgetType *old_widget = STLReplacePtr(&m_widget_map, usb_device, widget);
235  if (old_widget) {
236  // This should never happen.
237  OLA_WARN << "Widget conflict for " << usb_device;
238  observer->WidgetRemoved(old_widget);
239  delete old_widget;
240  }
241  return true;
242 }
243 
244 template <typename WidgetType>
246  libusb_device *usb_device) {
247  OLA_INFO << "Removing device " << usb_device;
248  WidgetType *widget = STLLookupAndRemovePtr(&m_widget_map, usb_device);
249  if (widget) {
250  observer->WidgetRemoved(widget);
251  delete widget;
252  }
253 }
254 } // namespace usbdmx
255 } // namespace plugin
256 } // namespace ola
257 #endif // PLUGINS_USBDMX_WIDGETFACTORY_H_