From ba5631625d00858c782b097260a18dcc82ba5189 Mon Sep 17 00:00:00 2001 From: Matti Airas Date: Wed, 30 Oct 2024 12:16:31 +0200 Subject: [PATCH] Store EventLoop in a shared_ptr Strictly speaking, this is a breaking change, but since the impact should be quite limited and it removes a major use case of raw pointers, I'll just keep quiet and do it. Naughty me. --- platformio.ini | 2 +- src/sensesp.cpp | 4 +++- src/sensesp.h | 3 ++- src/sensesp/system/task_queue_producer.h | 3 ++- src/sensesp_app.h | 18 +++++++++--------- src/sensesp_base_app.h | 12 +++++++----- 6 files changed, 24 insertions(+), 18 deletions(-) diff --git a/platformio.ini b/platformio.ini index 221fa7e15..d58ca0afb 100644 --- a/platformio.ini +++ b/platformio.ini @@ -30,7 +30,7 @@ upload_speed = 2000000 monitor_speed = 115200 lib_deps = - mairas/ReactESP @ ^3.1.0 + mairas/ReactESP @ ^3.2.0 bblanchon/ArduinoJson @ ^7.0.0 pfeerick/elapsedMillis @ ^1.0.6 bxparks/AceButton @ ^1.10.1 diff --git a/src/sensesp.cpp b/src/sensesp.cpp index 9686b4629..456df76aa 100644 --- a/src/sensesp.cpp +++ b/src/sensesp.cpp @@ -1,10 +1,12 @@ #include "sensesp.h" +#include + #include "sensesp_base_app.h" namespace sensesp { -reactesp::EventLoop* event_loop() { +std::shared_ptr event_loop() { return SensESPBaseApp::get_event_loop(); } diff --git a/src/sensesp.h b/src/sensesp.h index 98934f6a1..bd84e9d64 100644 --- a/src/sensesp.h +++ b/src/sensesp.h @@ -2,6 +2,7 @@ #define SENSESP_H #include +#include #include "sensesp/system/local_debug.h" @@ -19,7 +20,7 @@ namespace sensesp { typedef std::function void_cb_func; -reactesp::EventLoop* event_loop(); +std::shared_ptr event_loop(); } // namespace sensesp diff --git a/src/sensesp/system/task_queue_producer.h b/src/sensesp/system/task_queue_producer.h index c23a2c594..e19840bd0 100644 --- a/src/sensesp/system/task_queue_producer.h +++ b/src/sensesp/system/task_queue_producer.h @@ -79,7 +79,8 @@ class SafeQueue : public std::queue { template class TaskQueueProducer : public ObservableValue { public: - TaskQueueProducer(const T& value, reactesp::EventLoop* consumer_event_loop, + TaskQueueProducer(const T& value, + std::shared_ptr consumer_event_loop, unsigned int poll_rate = 990) : ObservableValue(value) { auto func = [this]() { diff --git a/src/sensesp_app.h b/src/sensesp_app.h index a0972ac3c..f69ed4b6e 100644 --- a/src/sensesp_app.h +++ b/src/sensesp_app.h @@ -222,7 +222,7 @@ class SensESPApp : public SensESPBaseApp { // Collect metrics for the status page void connect_status_page_items() { this->hostname_->connect_to(&this->hostname_ui_output_); - this->event_loop_.onRepeat(4999, [this]() { + this->event_loop_->onRepeat(4999, [this]() { wifi_ssid_ui_output_.set(WiFi.SSID()); free_memory_ui_output_.set(ESP.getFreeHeap()); wifi_rssi_ui_output_.set(WiFi.RSSI()); @@ -231,19 +231,19 @@ class SensESPApp : public SensESPBaseApp { uptime_ui_output_.set(millis() / 1000); // Event loop queue sizes - int event_loop_queue_size = event_loop_.getEventQueueSize(); - int event_loop_timed_queue_size = event_loop_.getTimedEventQueueSize(); + int event_loop_queue_size = event_loop_->getEventQueueSize(); + int event_loop_timed_queue_size = event_loop_->getTimedEventQueueSize(); int event_loop_untimed_queue_size = - event_loop_.getUntimedEventQueueSize(); + event_loop_->getUntimedEventQueueSize(); // Total tick count - uint64_t current_tick_count = event_loop_.getTickCount(); + uint64_t current_tick_count = event_loop_->getTickCount(); total_tick_count_ui_output_.set(current_tick_count); // Event counts - uint64_t current_event_count = event_loop_.getEventCount(); - uint64_t current_timed_event_count = event_loop_.getTimedEventCount(); - uint64_t current_untimed_event_count = event_loop_.getUntimedEventCount(); + uint64_t current_event_count = event_loop_->getEventCount(); + uint64_t current_timed_event_count = event_loop_->getTimedEventCount(); + uint64_t current_untimed_event_count = event_loop_->getUntimedEventCount(); event_count_ui_output_.set(current_event_count); timed_event_count_ui_output_.set(current_timed_event_count); untimed_event_count_ui_output_.set(current_untimed_event_count); @@ -269,7 +269,7 @@ class SensESPApp : public SensESPBaseApp { event_loop_timed_queue_ui_output_.set(event_loop_timed_queue_size); event_loop_untimed_queue_ui_output_.set(event_loop_untimed_queue_size); event_loop_interrupt_queue_ui_output_.set( - event_loop_.getISREventQueueSize()); + event_loop_->getISREventQueueSize()); ticks_per_second_ui_output_.set(int(ticks_diff / interval_seconds)); events_per_second_ui_output_.set(int(events_diff / interval_seconds)); diff --git a/src/sensesp_base_app.h b/src/sensesp_base_app.h index 2442a7fd5..6501c59df 100644 --- a/src/sensesp_base_app.h +++ b/src/sensesp_base_app.h @@ -41,7 +41,7 @@ inline void SetupSerialDebug(uint32_t baudrate) { */ class SensESPBaseApp { protected: - reactesp::EventLoop event_loop_; + std::shared_ptr event_loop_; public: /** @@ -81,7 +81,7 @@ class SensESPBaseApp { "Resetting the device configuration to system defaults."); Resettable::reset_all(); - this->event_loop_.onDelay(1000, []() { + this->event_loop_->onDelay(1000, []() { ESP.restart(); delay(1000); }); @@ -110,8 +110,8 @@ class SensESPBaseApp { * instance. * */ - static reactesp::EventLoop* get_event_loop() { - return &(SensESPBaseApp::get()->event_loop_); + static std::shared_ptr get_event_loop() { + return SensESPBaseApp::get()->event_loop_; } protected: @@ -122,7 +122,9 @@ class SensESPBaseApp { * be called only once. For compatibility reasons, the class hasn't been * refactored into a singleton. */ - SensESPBaseApp() : filesystem_{std::make_shared()} { + SensESPBaseApp() + : filesystem_{std::make_shared()}, + event_loop_{std::make_shared()} { // Instance is now set by the builder }