diff options
author | Lincoln Ramsay <lincoln.ramsay@nokia.com> | 2012-05-28 12:08:06 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-05-28 04:58:36 +0200 |
commit | 8ff4b2dbeeba8ffc6503a6a8ac53a3df2625298a (patch) | |
tree | d6b3566f97299970e0210325376f407b6dc048b6 /src/plugins/sensors | |
parent | f6a6c002a6b549b77894caee46abb97b89268729 (diff) | |
download | qtsensors-8ff4b2dbeeba8ffc6503a6a8ac53a3df2625298a.tar.gz |
Check for Q_GLOBAL_STATIC returning 0 (simulator plugin).
Consolidate all of the Q_GLOBAL_STATIC data into 1 class so we have one
lifetime to manage. Provide a static instance() method for use outside
of the .cpp file and check for this returning 0.
Change-Id: I243e2c5497e1c018ab7713bd9ef43ac22a658ff7
Reviewed-by: Lorn Potter <lorn.potter@nokia.com>
Reviewed-by: Zsolt Simon <zsolt.simon@nokia.com>
Diffstat (limited to 'src/plugins/sensors')
9 files changed, 45 insertions, 66 deletions
diff --git a/src/plugins/sensors/simulator/simulatoraccelerometer.cpp b/src/plugins/sensors/simulator/simulatoraccelerometer.cpp index 8ab6c1a..e40dd0b 100644 --- a/src/plugins/sensors/simulator/simulatoraccelerometer.cpp +++ b/src/plugins/sensors/simulator/simulatoraccelerometer.cpp @@ -53,7 +53,7 @@ SimulatorAccelerometer::SimulatorAccelerometer(QSensor *sensor) void SimulatorAccelerometer::poll() { - QtMobility::QAccelerometerReadingData data = get_qtAccelerometerData(); + QtMobility::QAccelerometerReadingData data = SensorsConnection::instance()->qtAccelerometerData; quint64 newTimestamp; if (!data.timestamp.isValid()) newTimestamp = QDateTime::currentDateTime().toTime_t(); diff --git a/src/plugins/sensors/simulator/simulatorambientlightsensor.cpp b/src/plugins/sensors/simulator/simulatorambientlightsensor.cpp index 6b9d387..8ae0169 100644 --- a/src/plugins/sensors/simulator/simulatorambientlightsensor.cpp +++ b/src/plugins/sensors/simulator/simulatorambientlightsensor.cpp @@ -53,7 +53,7 @@ SimulatorAmbientLightSensor::SimulatorAmbientLightSensor(QSensor *sensor) void SimulatorAmbientLightSensor::poll() { - QtMobility::QAmbientLightReadingData data = get_qtAmbientLightData(); + QtMobility::QAmbientLightReadingData data = SensorsConnection::instance()->qtAmbientLightData; QAmbientLightReading::LightLevel convertedLightLevel; switch (data.lightLevel) { case QtMobility::Dark: diff --git a/src/plugins/sensors/simulator/simulatorcommon.cpp b/src/plugins/sensors/simulator/simulatorcommon.cpp index 76df155..900b6bd 100644 --- a/src/plugins/sensors/simulator/simulatorcommon.cpp +++ b/src/plugins/sensors/simulator/simulatorcommon.cpp @@ -47,13 +47,6 @@ using namespace Simulator; -Q_GLOBAL_STATIC(QtMobility::QAmbientLightReadingData, qtAmbientLightData) -Q_GLOBAL_STATIC(QtMobility::QLightReadingData, qtLightData) -Q_GLOBAL_STATIC(QtMobility::QAccelerometerReadingData, qtAccelerometerData) -Q_GLOBAL_STATIC(QtMobility::QMagnetometerReadingData, qtMagnetometerData) -Q_GLOBAL_STATIC(QtMobility::QCompassReadingData, qtCompassData) -Q_GLOBAL_STATIC(QtMobility::QProximityReadingData, qtProximityData) -Q_GLOBAL_STATIC(QtMobility::QIRProximityReadingData, qtIRProximityData) Q_GLOBAL_STATIC(SensorsConnection, sensorsConnection) class SimulatorAsyncConnection: public QThread @@ -146,39 +139,47 @@ SensorsConnection::~SensorsConnection() delete mConnection; } +SensorsConnection *SensorsConnection::instance() +{ + SensorsConnection *connection = sensorsConnection(); + // It's safe to return 0 because this is checked when used + //if (!connection) qFatal("Cannot return from SensorsConnection::instance because sensorsConnection() returned 0"); + return connection; +} + void SensorsConnection::setAmbientLightData(const QtMobility::QAmbientLightReadingData &data) { - *qtAmbientLightData() = data; + qtAmbientLightData = data; } void SensorsConnection::setLightData(const QtMobility::QLightReadingData &data) { - *qtLightData() = data; + qtLightData = data; } void SensorsConnection::setAccelerometerData(const QtMobility::QAccelerometerReadingData &data) { - *qtAccelerometerData() = data; + qtAccelerometerData = data; } void SensorsConnection::setMagnetometerData(const QtMobility::QMagnetometerReadingData &data) { - *qtMagnetometerData() = data; + qtMagnetometerData = data; } void SensorsConnection::setCompassData(const QtMobility::QCompassReadingData &data) { - *qtCompassData() = data; + qtCompassData = data; } void SensorsConnection::setProximityData(const QtMobility::QProximityReadingData &data) { - *qtProximityData() = data; + qtProximityData = data; } void SensorsConnection::setIRProximityData(const QtMobility::QIRProximityReadingData &data) { - *qtIRProximityData() = data; + qtIRProximityData = data; } void SensorsConnection::initialSensorsDataSent() @@ -192,11 +193,17 @@ SimulatorCommon::SimulatorCommon(QSensor *sensor) { addDataRate(1, 100); sensor->setDataRate(20); - (void)sensorsConnection(); // Ensure this exists + (void)SensorsConnection::instance(); // Ensure this exists } void SimulatorCommon::start() { + SensorsConnection *connection = SensorsConnection::instance(); + if (!connection) { + sensorStopped(); + return; + } + if (m_timerid) return; @@ -221,43 +228,13 @@ void SimulatorCommon::stop() void SimulatorCommon::timerEvent(QTimerEvent * /*event*/) { - if (!sensorsConnection()->safe()) return; // wait until it's safe to read the data + SensorsConnection *connection = SensorsConnection::instance(); + if (!connection) { + stop(); + sensorStopped(); + } + if (!connection->safe()) return; // wait until it's safe to read the data poll(); } -QtMobility::QAccelerometerReadingData get_qtAccelerometerData() -{ - return *qtAccelerometerData(); -} - -QtMobility::QMagnetometerReadingData get_qtMagnetometerData() -{ - return *qtMagnetometerData(); -} - -QtMobility::QAmbientLightReadingData get_qtAmbientLightData() -{ - return *qtAmbientLightData(); -} - -QtMobility::QLightReadingData get_qtLightData() -{ - return *qtLightData(); -} - -QtMobility::QCompassReadingData get_qtCompassData() -{ - return *qtCompassData(); -} - -QtMobility::QProximityReadingData get_qtProximityData() -{ - return *qtProximityData(); -} - -QtMobility::QIRProximityReadingData get_qtIRProximityData() -{ - return *qtIRProximityData(); -} - #include "simulatorcommon.moc" diff --git a/src/plugins/sensors/simulator/simulatorcommon.h b/src/plugins/sensors/simulator/simulatorcommon.h index 21cacc0..6555f1f 100644 --- a/src/plugins/sensors/simulator/simulatorcommon.h +++ b/src/plugins/sensors/simulator/simulatorcommon.h @@ -56,6 +56,7 @@ public: explicit SensorsConnection(QObject *parent = 0); virtual ~SensorsConnection(); + static SensorsConnection *instance(); bool safe() const { return mInitialDataSent; } public slots: @@ -71,6 +72,15 @@ public slots: private: SimulatorAsyncConnection *mConnection; bool mInitialDataSent; + +public: + QtMobility::QAmbientLightReadingData qtAmbientLightData; + QtMobility::QLightReadingData qtLightData; + QtMobility::QAccelerometerReadingData qtAccelerometerData; + QtMobility::QMagnetometerReadingData qtMagnetometerData; + QtMobility::QCompassReadingData qtCompassData; + QtMobility::QProximityReadingData qtProximityData; + QtMobility::QIRProximityReadingData qtIRProximityData; }; class SimulatorCommon : public QSensorBackend @@ -87,13 +97,5 @@ private: int m_timerid; }; -QtMobility::QAccelerometerReadingData get_qtAccelerometerData(); -QtMobility::QMagnetometerReadingData get_qtMagnetometerData(); -QtMobility::QAmbientLightReadingData get_qtAmbientLightData(); -QtMobility::QLightReadingData get_qtLightData(); -QtMobility::QCompassReadingData get_qtCompassData(); -QtMobility::QProximityReadingData get_qtProximityData(); -QtMobility::QIRProximityReadingData get_qtIRProximityData(); - #endif diff --git a/src/plugins/sensors/simulator/simulatorcompass.cpp b/src/plugins/sensors/simulator/simulatorcompass.cpp index 37bf0dc..032eb37 100644 --- a/src/plugins/sensors/simulator/simulatorcompass.cpp +++ b/src/plugins/sensors/simulator/simulatorcompass.cpp @@ -53,7 +53,7 @@ SimulatorCompass::SimulatorCompass(QSensor *sensor) void SimulatorCompass::poll() { - QtMobility::QCompassReadingData data = get_qtCompassData(); + QtMobility::QCompassReadingData data = SensorsConnection::instance()->qtCompassData; quint64 newTimestamp; if (!data.timestamp.isValid()) newTimestamp = QDateTime::currentDateTime().toTime_t(); diff --git a/src/plugins/sensors/simulator/simulatorirproximitysensor.cpp b/src/plugins/sensors/simulator/simulatorirproximitysensor.cpp index 0590ad9..3198b77 100644 --- a/src/plugins/sensors/simulator/simulatorirproximitysensor.cpp +++ b/src/plugins/sensors/simulator/simulatorirproximitysensor.cpp @@ -53,7 +53,7 @@ SimulatorIRProximitySensor::SimulatorIRProximitySensor(QSensor *sensor) void SimulatorIRProximitySensor::poll() { - QtMobility::QIRProximityReadingData data = get_qtIRProximityData(); + QtMobility::QIRProximityReadingData data = SensorsConnection::instance()->qtIRProximityData; quint64 newTimestamp; if (!data.timestamp.isValid()) newTimestamp = QDateTime::currentDateTime().toTime_t(); diff --git a/src/plugins/sensors/simulator/simulatorlightsensor.cpp b/src/plugins/sensors/simulator/simulatorlightsensor.cpp index 47d98de..a0c5035 100644 --- a/src/plugins/sensors/simulator/simulatorlightsensor.cpp +++ b/src/plugins/sensors/simulator/simulatorlightsensor.cpp @@ -53,7 +53,7 @@ SimulatorLightSensor::SimulatorLightSensor(QSensor *sensor) void SimulatorLightSensor::poll() { - QtMobility::QLightReadingData data = get_qtLightData(); + QtMobility::QLightReadingData data = SensorsConnection::instance()->qtLightData; quint64 newTimestamp; if (!data.timestamp.isValid()) newTimestamp = QDateTime::currentDateTime().toTime_t(); diff --git a/src/plugins/sensors/simulator/simulatormagnetometer.cpp b/src/plugins/sensors/simulator/simulatormagnetometer.cpp index bafb2fc..cd6ce53 100644 --- a/src/plugins/sensors/simulator/simulatormagnetometer.cpp +++ b/src/plugins/sensors/simulator/simulatormagnetometer.cpp @@ -53,7 +53,7 @@ SimulatorMagnetometer::SimulatorMagnetometer(QSensor *sensor) void SimulatorMagnetometer::poll() { - QtMobility::QMagnetometerReadingData data = get_qtMagnetometerData(); + QtMobility::QMagnetometerReadingData data = SensorsConnection::instance()->qtMagnetometerData; quint64 newTimestamp; if (!data.timestamp.isValid()) newTimestamp = QDateTime::currentDateTime().toTime_t(); diff --git a/src/plugins/sensors/simulator/simulatorproximitysensor.cpp b/src/plugins/sensors/simulator/simulatorproximitysensor.cpp index 5926dd7..44977ab 100644 --- a/src/plugins/sensors/simulator/simulatorproximitysensor.cpp +++ b/src/plugins/sensors/simulator/simulatorproximitysensor.cpp @@ -53,7 +53,7 @@ SimulatorProximitySensor::SimulatorProximitySensor(QSensor *sensor) void SimulatorProximitySensor::poll() { - QtMobility::QProximityReadingData data = get_qtProximityData(); + QtMobility::QProximityReadingData data = SensorsConnection::instance()->qtProximityData; quint64 newTimestamp; if (!data.timestamp.isValid()) newTimestamp = QDateTime::currentDateTime().toTime_t(); |