mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-02-20 15:14:26 +01:00
Added a head and body string to Request, and removed themes. I think themes added unnecessary complexity, and code non-locality (The view methods would have been in a different class.). I think in a language which have headers this is unwanted (The code will be broken up with the mvc pattern, but the methods don't need to be in separate classes.). Themes are still easily implementable by either adding different css links to the new head variable, or by inheritance.
This commit is contained in:
parent
a6b29c04a4
commit
195f8db964
@ -7,6 +7,8 @@
|
||||
|
||||
class Request;
|
||||
|
||||
//This might be converted to a form validator
|
||||
|
||||
class FormField {
|
||||
public:
|
||||
std::string name;
|
||||
|
@ -1,5 +1,32 @@
|
||||
#include "request.h"
|
||||
|
||||
void Request::compile_body() {
|
||||
compiled_body.reserve(body.size() + head.size() + 13 + 14 + 15);
|
||||
|
||||
//13
|
||||
compiled_body += "<html>"
|
||||
"<head>";
|
||||
|
||||
compiled_body += head;
|
||||
|
||||
//14
|
||||
compiled_body += "</head>"
|
||||
"<body>";
|
||||
|
||||
compiled_body += body;
|
||||
|
||||
//15
|
||||
compiled_body += "</body>"
|
||||
"</html>";
|
||||
|
||||
response->setBody(compiled_body);
|
||||
}
|
||||
|
||||
void Request::compile_and_send_body() {
|
||||
compile_body();
|
||||
send();
|
||||
}
|
||||
|
||||
void Request::next_stage() {
|
||||
if (current_middleware_index == (*middleware_stack).size()) {
|
||||
handler_instance.handler_func(handler_instance.instance, this);
|
||||
@ -37,6 +64,10 @@ void Request::reset() {
|
||||
current_middleware_index = 0;
|
||||
middleware_stack = nullptr;
|
||||
|
||||
head.clear();
|
||||
body.clear();
|
||||
compiled_body.clear();
|
||||
|
||||
if (response)
|
||||
delete response;
|
||||
|
||||
|
@ -23,6 +23,12 @@ public:
|
||||
HandlerInstance handler_instance;
|
||||
std::vector<HandlerInstance> *middleware_stack;
|
||||
|
||||
std::string head;
|
||||
std::string body;
|
||||
std::string compiled_body;
|
||||
|
||||
void compile_body();
|
||||
void compile_and_send_body();
|
||||
void next_stage();
|
||||
void send();
|
||||
void reset();
|
||||
|
@ -1,55 +0,0 @@
|
||||
#include "theme.h"
|
||||
|
||||
#include "form.h"
|
||||
|
||||
Theme::Theme() {
|
||||
}
|
||||
|
||||
Theme::~Theme() {
|
||||
}
|
||||
|
||||
THEME_CORE_STATIC_DEFINES(SiteTheme);
|
||||
|
||||
void SiteTheme::add_header(Request *request, std::string *output) {
|
||||
output->append(
|
||||
"<html>"
|
||||
"<head>"
|
||||
"</head>"
|
||||
"<body>");
|
||||
}
|
||||
|
||||
void SiteTheme::add_footer(Request *request, std::string *output) {
|
||||
output->append(
|
||||
"</body>"
|
||||
"</html>");
|
||||
}
|
||||
|
||||
void SiteTheme::render_index_page(Request *request, std::string *output) {
|
||||
add_header(request, output);
|
||||
|
||||
output->append("<p>Test HTML Body</p>");
|
||||
|
||||
add_footer(request, output);
|
||||
}
|
||||
|
||||
void SiteTheme::render_form(Request *request, Form* form, std::string *output) {
|
||||
|
||||
}
|
||||
|
||||
SiteTheme::SiteTheme() {
|
||||
}
|
||||
|
||||
SiteTheme::~SiteTheme() {
|
||||
}
|
||||
|
||||
TestSiteTheme::TestSiteTheme() {
|
||||
}
|
||||
|
||||
TestSiteTheme::~TestSiteTheme() {
|
||||
}
|
||||
|
||||
TestSiteTheme2::TestSiteTheme2() {
|
||||
}
|
||||
|
||||
TestSiteTheme2::~TestSiteTheme2() {
|
||||
}
|
80
core/theme.h
80
core/theme.h
@ -1,80 +0,0 @@
|
||||
#ifndef THEME_H
|
||||
#define THEME_H
|
||||
|
||||
#include "request.h"
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
class Form;
|
||||
|
||||
#define THEME_CORE(_class_name) \
|
||||
public: \
|
||||
std::string theme_name; \
|
||||
static _class_name *default_theme; \
|
||||
static std::map<std::string, _class_name *> theme_map; \
|
||||
\
|
||||
void register_theme(const std::string &p_theme_name) { \
|
||||
theme_name = p_theme_name; \
|
||||
theme_map[theme_name] = this; \
|
||||
} \
|
||||
\
|
||||
void set_theme_as_default() { \
|
||||
default_theme = this; \
|
||||
} \
|
||||
\
|
||||
void register_theme() { \
|
||||
register_theme(#_class_name); \
|
||||
} \
|
||||
\
|
||||
void unregister_theme() { \
|
||||
theme_map.erase(theme_name); \
|
||||
if (default_theme == this) \
|
||||
default_theme = nullptr; \
|
||||
} \
|
||||
\
|
||||
_class_name *get_theme(const std::string &p_theme_name) { \
|
||||
_class_name *t = theme_map[p_theme_name]; \
|
||||
if (t) \
|
||||
return t; \
|
||||
else \
|
||||
return default_theme; \
|
||||
}
|
||||
|
||||
#define THEME_CORE_STATIC_DEFINES(_class_name) \
|
||||
_class_name *_class_name::default_theme = nullptr; \
|
||||
std::map<std::string, _class_name *> _class_name::theme_map;
|
||||
|
||||
class Theme {
|
||||
public:
|
||||
Theme();
|
||||
virtual ~Theme();
|
||||
};
|
||||
|
||||
class SiteTheme : public Theme {
|
||||
THEME_CORE(SiteTheme);
|
||||
|
||||
public:
|
||||
virtual void add_header(Request *request, std::string *output);
|
||||
virtual void add_footer(Request *request, std::string *output);
|
||||
|
||||
virtual void render_index_page(Request *request, std::string *output);
|
||||
|
||||
virtual void render_form(Request *request, Form* form, std::string *output);
|
||||
|
||||
SiteTheme();
|
||||
~SiteTheme();
|
||||
};
|
||||
|
||||
class TestSiteTheme : public SiteTheme {
|
||||
public:
|
||||
TestSiteTheme();
|
||||
~TestSiteTheme();
|
||||
};
|
||||
|
||||
class TestSiteTheme2 : public SiteTheme {
|
||||
public:
|
||||
TestSiteTheme2();
|
||||
~TestSiteTheme2();
|
||||
};
|
||||
|
||||
#endif
|
@ -19,11 +19,11 @@ void RDNApplication::index(Object *instance, Request *request) {
|
||||
return;
|
||||
}
|
||||
|
||||
SiteTheme::default_theme->render_index_page(request, &body);
|
||||
request->body.append("<p>Test HTML Body</p>");
|
||||
request->compile_body();
|
||||
|
||||
FileCache::get_singleton()->set_cached_body("index", body);
|
||||
FileCache::get_singleton()->set_cached_body("index", request->compiled_body);
|
||||
|
||||
request->response->setBody(body);
|
||||
request->send();
|
||||
}
|
||||
|
||||
@ -62,27 +62,10 @@ void RDNApplication::migrate() {
|
||||
RDNApplication::RDNApplication() :
|
||||
Application() {
|
||||
|
||||
SiteTheme *t = new TestSiteTheme();
|
||||
t->register_theme();
|
||||
t->set_theme_as_default();
|
||||
|
||||
themes.push_back(t);
|
||||
|
||||
t = new TestSiteTheme2();
|
||||
t->register_theme();
|
||||
|
||||
themes.push_back(t);
|
||||
|
||||
message_page = new MessagePage();
|
||||
message_page->db = DatabaseManager::get_singleton()->databases[0];
|
||||
}
|
||||
|
||||
RDNApplication::~RDNApplication() {
|
||||
for (uint32_t i = 0; i < themes.size(); ++i) {
|
||||
delete themes[i];
|
||||
}
|
||||
|
||||
themes.clear();
|
||||
|
||||
delete message_page;
|
||||
}
|
@ -2,7 +2,6 @@
|
||||
#define RDN_APPLICATION_H
|
||||
|
||||
#include "core/application.h"
|
||||
#include "core/theme.h"
|
||||
#include "core/object.h"
|
||||
|
||||
#include "modules/message_page/message_page.h"
|
||||
@ -23,8 +22,6 @@ public:
|
||||
RDNApplication();
|
||||
~RDNApplication();
|
||||
|
||||
std::vector<Theme *> themes;
|
||||
|
||||
MessagePage *message_page;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user