From 502cb4b60f23044107bf687434fbeb172c7644b7 Mon Sep 17 00:00:00 2001 From: Sean Rhea Date: Sun, 9 Aug 2009 14:49:18 -0700 Subject: [PATCH] abstract Device to support multiple device types --- src/Device.cpp | 43 ++++++++++++++++++++++++++++++++++++++ src/Device.h | 41 ++++++++++++++++++++++++++++++++++++ src/DownloadRideDialog.cpp | 5 +++-- src/PowerTapDevice.cpp | 3 +++ src/PowerTapDevice.h | 12 +++++------ src/src.pro | 2 ++ 6 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 src/Device.cpp create mode 100644 src/Device.h diff --git a/src/Device.cpp b/src/Device.cpp new file mode 100644 index 000000000..8efe3b5f1 --- /dev/null +++ b/src/Device.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2008 Sean C. Rhea (srhea@srhea.net) + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "Device.h" + +static QMap devices; + +QList +Device::deviceTypes() +{ + return devices.keys(); +} + +Device & +Device::device(const QString &deviceType) +{ + assert(devices.contains(deviceType)); + return *devices.value(deviceType); +} + +bool +Device::addDevice(const QString &deviceType, Device *device) +{ + assert(!devices.contains(deviceType)); + devices.insert(deviceType, device); + return true; +} + diff --git a/src/Device.h b/src/Device.h new file mode 100644 index 000000000..e355813be --- /dev/null +++ b/src/Device.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2008 Sean C. Rhea (srhea@srhea.net) + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef _GC_Device_h +#define _GC_Device_h 1 + +#include "CommPort.h" +#include + +struct Device +{ + virtual ~Device() {} + + typedef boost::function StatusCallback; + + virtual bool download(CommPortPtr dev, const QDir &tmpdir, + QString &tmpname, QString &filename, + StatusCallback statusCallback, QString &err) = 0; + + static QList deviceTypes(); + static Device &device(const QString &deviceType); + static bool addDevice(const QString &deviceType, Device *device); +}; + +#endif // _GC_Device_h + diff --git a/src/DownloadRideDialog.cpp b/src/DownloadRideDialog.cpp index 3e1a0d886..404fccc6b 100644 --- a/src/DownloadRideDialog.cpp +++ b/src/DownloadRideDialog.cpp @@ -19,8 +19,8 @@ */ #include "DownloadRideDialog.h" +#include "Device.h" #include "MainWindow.h" -#include "PowerTapDevice.h" #include #include #include @@ -139,7 +139,8 @@ DownloadRideDialog::downloadClicked() assert(dev); QString err; QString tmpname, filename; - if (!PowerTapDevice::download( + Device &device = Device::device("PowerTap"); + if (!device.download( dev, home, tmpname, filename, boost::bind(&DownloadRideDialog::statusCallback, this, _1), err)) { diff --git a/src/PowerTapDevice.cpp b/src/PowerTapDevice.cpp index 4a4ca482b..ca87dc675 100644 --- a/src/PowerTapDevice.cpp +++ b/src/PowerTapDevice.cpp @@ -22,6 +22,9 @@ #define PT_DEBUG false +static bool powerTapRegistered = + Device::addDevice("PowerTap", new PowerTapDevice()); + static bool hasNewline(const char *buf, int len) { diff --git a/src/PowerTapDevice.h b/src/PowerTapDevice.h index 43699de91..a803c6119 100644 --- a/src/PowerTapDevice.h +++ b/src/PowerTapDevice.h @@ -20,15 +20,13 @@ #define _GC_PowerTapDevice_h 1 #include "CommPort.h" -#include +#include "Device.h" -struct PowerTapDevice +struct PowerTapDevice : public Device { - typedef boost::function StatusCallback; - - static bool download(CommPortPtr dev, const QDir &tmpdir, - QString &tmpname, QString &filename, - StatusCallback statusCallback, QString &err); + virtual bool download(CommPortPtr dev, const QDir &tmpdir, + QString &tmpname, QString &filename, + StatusCallback statusCallback, QString &err); }; #endif // _GC_PowerTapDevice_h diff --git a/src/src.pro b/src/src.pro index f6b453345..fa704920d 100644 --- a/src/src.pro +++ b/src/src.pro @@ -30,6 +30,7 @@ HEADERS += \ CpintPlot.h \ CsvRideFile.h \ DBAccess.h \ + Device.h \ DownloadRideDialog.h \ MainWindow.h \ PfPvPlot.h \ @@ -73,6 +74,7 @@ SOURCES += \ CpintPlot.cpp \ CsvRideFile.cpp \ DBAccess.cpp \ + Device.cpp \ DownloadRideDialog.cpp \ MainWindow.cpp \ PfPvPlot.cpp \