From 88f4447777b700de83e0d59699b625121ba8903a Mon Sep 17 00:00:00 2001 From: Sven Steckmann Date: Thu, 17 Oct 2019 13:04:50 +0200 Subject: [PATCH] Make handling of WebServerClass instance easier Adding a custom deleter to unique_ptr in case of using the reference. This makes the code less error prone and easier to read since we will only have to use the creation of the pointer the correct way. --- src/AutoConnect.cpp | 21 ++++----------------- src/AutoConnect.h | 9 ++------- 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/src/AutoConnect.cpp b/src/AutoConnect.cpp index bedce03..d6bc673 100644 --- a/src/AutoConnect.cpp +++ b/src/AutoConnect.cpp @@ -22,6 +22,7 @@ #define SET_HOSTNAME(x) do { WiFi.setHostname(x); } while(0) #endif + /** * AutoConnect default constructor. This entry activates WebServer * internally and the web server is allocated internal. @@ -30,7 +31,6 @@ AutoConnect::AutoConnect() { _initialize(); _webServer.reset(nullptr); _dnsServer.reset(nullptr); - _webServerAlloc = AC_WEBSERVER_HOSTED; } /** @@ -40,9 +40,8 @@ AutoConnect::AutoConnect() { */ AutoConnect::AutoConnect(WebServerClass& webServer) { _initialize(); - _webServer.reset(&webServer); + _webServer = WebserverUP(&webServer, [](WebServerClass*){}); _dnsServer.reset(nullptr); - _webServerAlloc = AC_WEBSERVER_PARASITIC; } void AutoConnect::_initialize(void) { @@ -386,18 +385,7 @@ void AutoConnect::end(void) { _stopPortal(); _dnsServer.reset(); - - if (_webServer) { - switch (_webServerAlloc) { - case AC_WEBSERVER_HOSTED: - _webServer.reset(); - break; - case AC_WEBSERVER_PARASITIC: - _webServer.release(); - break; - } - } - + _webServer.reset(); } /** @@ -453,8 +441,7 @@ void AutoConnect::_startWebServer(void) { // Boot Web server if (!_webServer) { // Only when hosting WebServer internally - _webServer.reset(new WebServerClass(AUTOCONNECT_HTTPPORT)); - _webServerAlloc = AC_WEBSERVER_HOSTED; + _webServer = WebserverUP(new WebServerClass(AUTOCONNECT_HTTPPORT), std::default_delete() ); AC_DBG("WebServer allocated\n"); } // Discard the original the not found handler to redirect captive portal detection. diff --git a/src/AutoConnect.h b/src/AutoConnect.h index d6c605f..ee800fb 100644 --- a/src/AutoConnect.h +++ b/src/AutoConnect.h @@ -218,11 +218,6 @@ class AutoConnect { void onNotFound(WebServerClass::THandlerFunction fn); protected: - enum _webServerAllocateType { - AC_WEBSERVER_PARASITIC, - AC_WEBSERVER_HOSTED - }; - typedef enum _webServerAllocateType AC_WEBSERVER_TYPE; typedef enum { AC_RECONNECT_SET, AC_RECONNECT_RESET @@ -271,9 +266,9 @@ class AutoConnect { size_t _freeHeapSize; /** Servers which works in concert. */ - std::unique_ptr _webServer; + typedef std::unique_ptr > WebserverUP; + WebserverUP _webServer = WebserverUP(nullptr, std::default_delete()); std::unique_ptr _dnsServer; - AC_WEBSERVER_TYPE _webServerAlloc; /** * Dynamically hold one page of AutoConnect menu.