From e72fc2391d532319f9d7719be20e5888c84bd01b Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 9 Jan 2024 22:50:34 +0400 Subject: [PATCH] Enter ContentResponseBlueprint::Data ContentResponseBlueprint::m_data is now an opaque data member implemented in the .cpp and ready to be switched from kainjow::mustache::data to a different implementation. --- src/server/response.cpp | 16 +++++++++++++--- src/server/response.h | 7 ++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/server/response.cpp b/src/server/response.cpp index 918b04161..6ae29a3f9 100644 --- a/src/server/response.cpp +++ b/src/server/response.cpp @@ -151,6 +151,13 @@ std::unique_ptr Response::build_304(const ETag& etag) return response; } +class ContentResponseBlueprint::Data : public kainjow::mustache::data +{ +public: + Data() {} + template Data(const T& t) : kainjow::mustache::data(t) {} +}; + ContentResponseBlueprint::ContentResponseBlueprint(const RequestContext* request, int httpStatusCode, const std::string& mimeType, @@ -159,8 +166,11 @@ ContentResponseBlueprint::ContentResponseBlueprint(const RequestContext* request , m_httpStatusCode(httpStatusCode) , m_mimeType(mimeType) , m_template(templateStr) + , m_data(new Data) {} +ContentResponseBlueprint::~ContentResponseBlueprint() = default; + std::string ContentResponseBlueprint::getMessage(const std::string& msgId) const { return getTranslatedString(m_request.get_user_language(), msgId); @@ -168,7 +178,7 @@ std::string ContentResponseBlueprint::getMessage(const std::string& msgId) const std::unique_ptr ContentResponseBlueprint::generateResponseObject() const { - auto r = ContentResponse::build(m_template, m_data, m_mimeType); + auto r = ContentResponse::build(m_template, *m_data, m_mimeType); r->set_code(m_httpStatusCode); return r; } @@ -184,7 +194,7 @@ HTTPErrorResponse::HTTPErrorResponse(const RequestContext& request, request.get_requested_format() == "html" ? RESOURCE::templates::error_html : RESOURCE::templates::error_xml) { kainjow::mustache::list emptyList; - this->m_data = kainjow::mustache::object{ + *this->m_data = kainjow::mustache::object{ {"CSS_URL", onlyAsNonEmptyMustacheValue(cssUrl) }, {"PAGE_TITLE", getMessage(pageTitleMsgId)}, {"PAGE_HEADING", getMessage(headingMsgId)}, @@ -210,7 +220,7 @@ UrlNotFoundResponse::UrlNotFoundResponse(const RequestContext& request) HTTPErrorResponse& HTTPErrorResponse::operator+(const ParameterizedMessage& details) { const std::string msg = details.getText(m_request.get_user_language()); - m_data["details"].push_back({"p", msg}); + (*m_data)["details"].push_back({"p", msg}); return *this; } diff --git a/src/server/response.h b/src/server/response.h index c44010766..726a51668 100644 --- a/src/server/response.h +++ b/src/server/response.h @@ -123,6 +123,8 @@ public: // functions const std::string& mimeType, const std::string& templateStr); + ~ContentResponseBlueprint(); + operator std::unique_ptr() const { return generateResponseObject(); @@ -130,6 +132,9 @@ public: // functions std::unique_ptr generateResponseObject() const; +protected: // types + class Data; + protected: // functions std::string getMessage(const std::string& msgId) const; @@ -138,7 +143,7 @@ protected: //data const int m_httpStatusCode; const std::string m_mimeType; const std::string m_template; - kainjow::mustache::data m_data; + std::unique_ptr m_data; }; struct HTTPErrorResponse : ContentResponseBlueprint