LCB
LCB

Reputation: 87

C++ Difficulty Creating Instance of Class within Singleton Class

I have a fairly good template (as in snippet of code) I pull out whenever I need a singleton class. I am now trying to apply it within my project to allow me to control a single instance of a web server. I can make a web server without encasing it in my class. When I try to encase it within the class I'm apparently too unskilled to pull it off.

I've tried the obvious Googling and searching here. I've read relevant posts. I am sure this does not mean I have a unique problem, just that I've not figured out the right way to fix it. Here's what I am working with:

webserver.h:

#include <ESP8266WebServer.h>
#include <FS.h>

class WebServer {
    private:
        // Singleton Declarations
        static bool instanceFlag;
        static WebServer *single;
        WebServer() {}
        // Other Declarations
        FS *filesystem;
        ESP8266WebServer server();
        String getContentType(String);
        bool handleFileRead(String);

    public:
        // Singleton Declarations
        static WebServer* getInstance();
        ~WebServer() {instanceFlag = false;}
        // Other Declarations
        void initialize(int);
        void handleLoop();
};

webserver.cpp:

#include "webserver.h"

bool WebServer::instanceFlag = false;
WebServer* WebServer::single = NULL;

WebServer* WebServer::getInstance() {
    if(!instanceFlag) {
        single = new WebServer();
        instanceFlag = true;
        return single;
    } else {
        return single;
    }
}

void WebServer::initialize (int port) {
    ESP8266WebServer server(port);
    FS *filesystem;
    filesystem->begin();

    Serial.print("Open: http://");
    Serial.print(WiFi.hostname().c_str());
    Serial.println(".local");

    server.onNotFound([]() {
        if (!single->handleFileRead(single->server.uri())) {
            single->server.send(404, "text/plain", "404: File not found.");
        }
    });

    server.begin();
    Serial.print("HTTP server started on port ");
    Serial.print(port);
    Serial.println(".");
}

String WebServer::getContentType(String filename) {
    if (single->server.hasArg("download")) {
        return "application/octet-stream";
    } else if (filename.endsWith(".htm")) {
        return "text/html";
    } else if (filename.endsWith(".html")) {
        return "text/html";
    } else if (filename.endsWith(".css")) {
        return "text/css";
    } else if (filename.endsWith(".js")) {
        return "application/javascript";
    } else if (filename.endsWith(".png")) {
        return "image/png";
    } else if (filename.endsWith(".gif")) {
        return "image/gif";
    } else if (filename.endsWith(".jpg")) {
        return "image/jpeg";
    } else if (filename.endsWith(".ico")) {
        return "image/x-icon";
    } else if (filename.endsWith(".xml")) {
        return "text/xml";
    } else if (filename.endsWith(".pdf")) {
        return "application/x-pdf";
    } else if (filename.endsWith(".zip")) {
        return "application/x-zip";
    } else if (filename.endsWith(".gz")) {
        return "application/x-gzip";
    } else {
       return "text/plain"; 
    }
}

bool WebServer::handleFileRead(String path) {
    Serial.println("handleFileRead: " + path);
    if (path.endsWith("/")) {
        path += "index.htm";
    }
    String contentType = getContentType(path);
    String pathWithGz = path + ".gz";
    if (filesystem->exists(pathWithGz) || filesystem->exists(path)) {
        if (filesystem->exists(pathWithGz)) {
            path += ".gz";
        }
        File file = filesystem->open(path, "r");
        single->server.streamFile(file, contentType);
        file.close();
        return true;
    }
    return false;
}

void WebServer::handleLoop() {
    single->server.handleClient();
}

The errors I am getting are all similar to the following:

src\webserver.cpp: In member function 'bool WebServer::handleFileRead(String)':
src\webserver.cpp:81:23: error: 'WebServer::single->WebServer::server' does not have class type
         single->server.streamFile(file, contentType);

I get the idea of "does not have a class type", I just have no idea what it means here. In my mind, "single" is a pointer to the class so I'm unclear what that reference is not working.

Obviously, there are ample examples out there how to do a web server without encapsulating it. Other things I need to do for this project lend itself to creating that requirement.

Upvotes: 0

Views: 409

Answers (1)

N Alex
N Alex

Reputation: 1014

There are some mistake in your code. In webserver.h:

...
private:
    // Singleton Declarations
    static bool instanceFlag;
    static WebServer *single;
    WebServer() {}
    // Other Declarations
    FS *filesystem;
    ESP8266WebServer *server; // <--- remove the parentheses and make it a pointer
    String getContentType(String);
    bool handleFileRead(String);
...

In webserver.cpp:

In WebServer::initialize I am guessing you want to initialize the class server and filesystem not locals, so it should probably look like this:

void WebServer::initialize (int port) {
    server = new ESP8266WebServer(port);
    filesystem = new FS();
    ...
}

And now everywhere you use the server you have to use the -> operator. For example:

void WebServer::handleLoop() {
    single->server->handleClient();
}

Please keep in mind that server and filesystem objects have to be deleted to avoid memory leaks.

EDIT:

You get the new error because FS has no constructor without arguments. FS's constructor looks like this: FS(FSImplPtr impl) : _impl(impl) { }, here you can see that FSImplPtr is a typedef for std::shared_ptr<FileImpl>, so you need to provide this as a parameter.

It works your way, because SPIFFS's existence is declared here and is of type FS.

If you want to use SPIFFS, you have to use it like this: filesystem = &SPIFFS;, not like you mentioned in the comments (FS* filesystem = &SPIFFS;) because your way creates a new temporary variable named filesystem, and probably you expect to initiate the filesystem in the class, not a local one.

Upvotes: 3

Related Questions