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?
– Maniero
I added the examples.
– Pedro Soares