Pass Class Name by C++ parameter

Asked

Viewed 664 times

1

I am having a problem, I need to pass the name of a Class via parameter, so that the function can create a new instance, this Class is daughter of another Class called Control. The problem is that there is no way to know how many children this class will have, because if it had I could make an IF for each one of her. But as who will create these Classes will not be I have no way to control, not to mention that it would be horrible to have to change the function every time a new Class was added or removed.
What I need to do in synthesize is this:
I’ll call a function Add class Validation, and pass via parameter o Authentication who is the son of Control. The Class Validation will add this Class Authentication in a list, it is only going to call it at a certain time. At this time the Class Validation will instantiate the Class Authentication and call a superscript Class method Control calling for void load().
How can I do this? This idea of mine is correct or has a better way of doing this?
Grateful from now on.

Current class:

#include "Routes.h"

void Routes::path(){

    this->GET("/", IndexController::GetIndex());
    this->POST("/Login", LoginController::GetIndex());
    this->GET("/Entrar", IndexController::GetIndex());


    this->GET("/Dasboard", IndexController::GetIndex());


}

Class As I need it to be:

#include "Routes.h"

void Routes::path(){

    this->GET("/", IndexController::GetIndex(), Autenticacao);
    this->POST("/Login", LoginController::GetIndex(), Autenticacao);
    this->GET("/Entrar", IndexController::GetIndex(), Autenticacao);


    this->GET("/Dasboard", IndexController::GetIndex(), Autenticacao);


}

Authentication class:

void Autenticacao::controlar(){
    if(Session::getInstance()->get("usuario") != NULL){
        Header::getInstance()->goTo("/Dashboard");
    }else{
        if(Request::getInstance()->page != "/Entrar"){
            Header::getInstance()->goTo("/Entrar");
        }
    }
}

Route class responsible for the POST and GET method (Current):

void Route::POST(string Path, View* page){
    map<string, map<string, View* > > page_;
    map<string, View*> request_;

    request_["POST"] = page;

    page_[Path] = request_;

    this->routes.push_back(page_);
}

Route class responsible for the POST and GET method (How it should look):

void Route::POST(string Path, View* page, Middleware controle){
    map<string, map<string, View* > > page_;
    map<string, View*> request_;

    request_["POST"] = page;

    page_[Path] = request_;

    {Fazer alguma coisa com "controle"}

    this->routes.push_back(page_);
}

Route class when loading a function (Current):

View * Route::load(string nome){

    for (auto& item: routes) {
        for (auto& mapa: item) {
            if(nome == mapa.first){
                for(auto& mapa_ : mapa.second){
                    if(mapa_.first == Server::METHOD()){
                        return mapa_.second;
                    }
                }
            }
        }
    }

    return new class Erro404();

}

Route class when you load a function (How it should look):

View * Route::load(string nome){

    for (auto& item: routes) {
        for (auto& mapa: item) {
            if(nome == mapa.first){
                for(auto& mapa_ : mapa.second){
                    if(mapa_.first == Server::METHOD()){
                        {pega de algum jeito a classe filho da "Middleware"}->controle();
                        return mapa_.second;
                    }
                }
            }
        }
    }

    return new class Erro404();

}
  • How about setting an example and show where your difficulty lies?

  • I added the examples.

1 answer

0

I solved the problem as follows.
Create a Control Class and Inherit it in the Authentication Class and other controls. I pass an instance of it via parameter and call it when necessary as well as Control because I overwrite some methods of it.

#include "Routes.h"

void Routes::path(){

    this->GET("/", IndexController::GetIndex(), new Autenticacao);
    this->POST("/Login", LoginController::GetIndex(), new Autenticacao);
    this->GET("/Entrar", IndexController::GetIndex(), new Autenticacao);


    this->GET("/Dasboard", IndexController::GetIndex(), new Autenticacao);
}


#ifndef Autenticacao_CLASS_H
#define Autenticacao_CLASS_H

using namespace std;

class Autenticacao : public Controle {

    public:
        Autenticacao();
        ~Autenticacao();

        virtual void controlar();

    private:
        string login = "/Entrar";
        string dashboard = "/Dashboard";


};

#endif


#ifndef Controle_CLASS_H
#define Controle_CLASS_H

using namespace std;

class Controle {

    public:
        Controle();
        ~Controle();

        virtual void controlar() = 0;
};

#endif

Browser other questions tagged

You are not signed in. Login or sign up in order to post.