From 7967300508de84ec05938f5811e8703fced44e1d Mon Sep 17 00:00:00 2001 From: Matthew Colvin <35540398+Mc067415@users.noreply.github.com> Date: Sun, 13 Aug 2023 16:42:17 -0500 Subject: [PATCH] Add Template Class for Notification to allow for easier creation of notifications --- Platformio/HAL/HardwareAbstract.cpp | 12 +++--------- Platformio/HAL/HardwareAbstract.hpp | 29 +++++++++++++---------------- Platformio/HAL/Notification.hpp | 26 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 Platformio/HAL/Notification.hpp diff --git a/Platformio/HAL/HardwareAbstract.cpp b/Platformio/HAL/HardwareAbstract.cpp index 1c5da99..e4fae8c 100644 --- a/Platformio/HAL/HardwareAbstract.cpp +++ b/Platformio/HAL/HardwareAbstract.cpp @@ -20,12 +20,6 @@ std::optional HardwareAbstract::getBatteryStatu return std::nullopt; } - void HardwareAbstract::notifyBatteryChange(HardwareAbstract::batteryStatus aStatus){ - for (auto handler : mBatteryUpdateHandlers){ - handler(aStatus); - } - } - - void HardwareAbstract::onBatteryChange(std::function onBatteryStatusChangeHandler){ - mBatteryUpdateHandlers.push_back(std::move(onBatteryStatusChangeHandler)); - } \ No newline at end of file +void HardwareAbstract::onBatteryChange(std::function onBatteryStatusChangeHandler){ + mBatteryNotification.onNotify(std::move(onBatteryStatusChangeHandler)); +} \ No newline at end of file diff --git a/Platformio/HAL/HardwareAbstract.hpp b/Platformio/HAL/HardwareAbstract.hpp index e1fac74..f3c91c9 100644 --- a/Platformio/HAL/HardwareAbstract.hpp +++ b/Platformio/HAL/HardwareAbstract.hpp @@ -11,10 +11,15 @@ #include "BatteryInterface.h" #include "DisplayInterface.h" #include "wifiHandlerInterface.h" +#include "Notification.hpp" class HardwareAbstract { public: - + HardwareAbstract(std::shared_ptr aBattery = nullptr, + std::shared_ptr aWifiHandler = nullptr, + std::shared_ptr aDisplay = nullptr + ); + struct batteryStatus { /// @brief Percent of battery remaining (0-100] int percentage; @@ -24,11 +29,11 @@ public: }; virtual std::optional getBatteryStatus(); - HardwareAbstract(std::shared_ptr aBattery = nullptr, - std::shared_ptr aWifiHandler = nullptr, - std::shared_ptr aDisplay = nullptr - ); - + /// @brief Register function to be ran when hardware notifies battery + /// status has changed. + /// @param onBatteryStatusChangeHandler - Callable to be ran when batter status changes + void onBatteryChange(std::function onBatteryStatusChangeHandler); + /// @brief Override in order to do setup of hardware devices virtual void init() = 0; @@ -36,17 +41,9 @@ public: /// @param message - Debug message virtual void debugPrint(std::string message) = 0; - /// @brief Register function to be ran when hardware notifies battery - /// status has changed. - /// @param onBatteryStatusChangeHandler - Callable to be ran when batter status changes - void onBatteryChange(std::function onBatteryStatusChangeHandler); - protected: - /// @brief Call in child class implementation to alert users - /// the battery status has changed - /// @param aStatus - Current Battery Status - void notifyBatteryChange(batteryStatus aStatus); - + Notification mBatteryNotification; + private: std::shared_ptr mBattery; std::shared_ptr mWifiHandler; diff --git a/Platformio/HAL/Notification.hpp b/Platformio/HAL/Notification.hpp new file mode 100644 index 0000000..0cb279c --- /dev/null +++ b/Platformio/HAL/Notification.hpp @@ -0,0 +1,26 @@ +#pragma once +#include +#include + +template +class Notification{ + public: + Notification() = default; + void onNotify(std::function aHandler); + void notify(notifyData... notifySendData); + + private: + std::vector> mFunctionHandlers; +}; + +template +void Notification::onNotify(std::function aHandler){ + mFunctionHandlers.push_back(std::move(aHandler)); +} + +template +void Notification::notify(outboundData... notifySendData){ + for (auto handler : mFunctionHandlers){ + handler(notifySendData...); + } +}