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.
pull/144/head
Sven Steckmann 5 years ago
parent ddade99c43
commit 88f4447777
  1. 21
      src/AutoConnect.cpp
  2. 9
      src/AutoConnect.h

@ -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<WebServerClass>() );
AC_DBG("WebServer allocated\n");
}
// Discard the original the not found handler to redirect captive portal detection.

@ -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<WebServerClass> _webServer;
typedef std::unique_ptr<WebServerClass, std::function<void(WebServerClass *)> > WebserverUP;
WebserverUP _webServer = WebserverUP(nullptr, std::default_delete<WebServerClass>());
std::unique_ptr<DNSServer> _dnsServer;
AC_WEBSERVER_TYPE _webServerAlloc;
/**
* Dynamically hold one page of AutoConnect menu.

Loading…
Cancel
Save