简体   繁体   中英

c++ cast member pointer of derived Class to base class pointer

I'm currently working on a HttpServer and have the following problem:

I want to register my controllers and when I get a request I want to check if one of those controllers has a handler for the requested URL and then call the handler.

So far so good, but I don't know how to solve it in c++, in PHP it would be easy.

In the Easteregg controller function getControllerCallbacks I want to return my available callbacks but it won't compile because I can't cast a member function pointer of a derived class to a member function pointer (EastereggController) of the base class (Controller). I have thought about making the ControllerCallback a "template class" but then I have to know the class of the Controller in the ControllerHandler, which I don't know because i have a std::vector

I'm fairly new to C++, maybe i have overlooked something.

This is my code now:

Controller.h

#ifndef CONTROLLER_H
#define CONTROLLER_H

#include "Config.h"
#include "Request.h"
#include "ControllerCallback.h"

namespace HttpServer {

class ControllerCallback;

class Controller {
    public:
        Controller();
        virtual std::vector<ControllerCallback> getControllerCallbacks() = 0;

    private:
        Config *config;
        const Request *request;
};
}
#endif // CONTROLLER_H

EastereggController.h

#ifndef EASTEREGGCONTROLLER_H
#define EASTEREGGCONTROLLER_H

#include "../HttpServer/Controller.h"

namespace Controller {

    class EastereggController: public HttpServer::Controller  {
        public:
            EastereggController() {};
            std::vector<HttpServer::ControllerCallback> getControllerCallbacks();
            HttpServer::Reply easteregg();
    };
}

#endif // EASTEREGGCONTROLLER_H

EastereggController.cpp

#include "EastereggController.h"

namespace Controller {
    std::vector<HttpServer::ControllerCallback> EastereggController::getControllerCallbacks() {
        std::vector<HttpServer::ControllerCallback> controllerCallbacks;
        HttpServer::ControllerCallback callback;

        callback.pathTemplate = "/easteregg";
        callback.handlerFunctionPtr = &EastereggController::easteregg;
        return controllerCallbacks;
    }

    HttpServer::Reply EastereggController::easteregg() {
        HttpServer::Reply rep;
        rep.content.append("you have found an easteregg\n");
        rep.status = HttpServer::Reply::ok;
        rep.headers.resize(2);
        rep.headers[0].name = "Content-Length";
        rep.headers[0].value = std::to_string(rep.content.size());
        rep.headers[1].name = "Content-Type";
        rep.headers[1].value = "text/plain";
        return rep;
    }
}

ControllerCallback.h

#ifndef CONTROLLERCALLBACK_H
#define CONTROLLERCALLBACK_H


#include "Reply.h"
#include <string>

namespace HttpServer {

    class Controller;

    class ControllerCallback {

        public:
            ControllerCallback()
                : pathTemplate("") {};
            //,handlerFunctionPtr(nullptr){}

            std::string pathTemplate;
            Reply(Controller::*handlerFunctionPtr)();
    };
}

#endif

ControllerHandler.h

#ifndef CONTROLLERHANDLER_H
#define CONTROLLERHANDLER_H

#include <vector>
#include <string>
#include "Controller.h"
#include "Config.h"
#include "Request.h"
#include "Reply.h"

namespace HttpServer {

    class ControllerHandler {
        public:
            ControllerHandler(Config &conf);
            void registerController(Controller &controller);
            bool invokeController(std::string requestPath, Request &req, Reply &rep);

        private:
            Config &config;
            std::vector<Controller *> controllers;

    };

}

#endif // CONTROLLERHANDLER_H

ControllerHandler.cpp

#include "ControllerHandler.h"
#include "Controller.h"
#include "PathHandler.h"

namespace HttpServer {
    ControllerHandler::ControllerHandler(Config &conf)
        : config(conf) {
    }

    void ControllerHandler::registerController(Controller &controller) {
        controllers.push_back(&controller);
    }

    bool ControllerHandler::invokeController(std::string requestPath, Request &req, Reply &rep) {
        PathHandler pathHandler(requestPath, ':');

        for(Controller *controller : controllers) {
            std::vector<ControllerCallback> callbacks = controller->getControllerCallbacks();
            for(ControllerCallback controllerCallback : callbacks) {
                if(pathHandler.compare(controllerCallback.pathTemplate)) {
                    rep = ((*controller).*(controllerCallback.handlerFunctionPtr))();
                    return true;
                }
            }
        }

        return false;
    }
}

I finally found the answer (which was a syntax error I'll have to admit)

While trying different solutions (before posting here) I had the following snippet:

EastereggController.cpp

callback.handlerFunctionPtr = reinterpret_cast<HttpServer::Reply HttpServer::Controller::*>(&EastereggController::easteregg);

which was syntactilly wrong, but i haven't noticed it because i had the following compile error:

error: invalid cast from type 'HttpServer::Reply (Controller::EastereggController::*)()' to type 'HttpServer::Reply HttpServer::Controller::*()'
     callback.handlerFunctionPtr = reinterpret_cast<HttpServer::Reply HttpServer::Controller::*()>(&EastereggController::easteregg);

Now the correct version:

callback.handlerFunctionPtr = reinterpret_cast<HttpServer::Reply(HttpServer::Controller::*)()>(&EastereggController::easteregg);

just added the parentheses and now it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM