#ifndef SENSOR_H #define SENSOR_H #include #include #include template class Sensor; /** listen for sensor events */ template class SensorListener { public: /** incoming sensor data */ virtual void onSensorData(Sensor* sensor, const Timestamp ts, const T& data) = 0; }; /** base-class for all sensors */ template class Sensor { private: std::vector*> listeners; public: /** start this sensor */ virtual void start() = 0; /** stop this sensor */ virtual void stop() = 0; /** add the given listener to the sensor */ void addListener(SensorListener* l) { listeners.push_back(l); } protected: /** inform all attached listeners */ void informListeners(const T& sensorData) { const Timestamp now = Timestamp::fromRunningTime(); for (SensorListener* l : listeners) { l->onSensorData(this, now, sensorData); } } /** inform all attached listeners. call this if you know the timestamp */ void informListeners(const Timestamp ts, const T& sensorData) { for (SensorListener* l : listeners) { l->onSensorData(this, ts, sensorData); } } }; #endif // SENSOR_H