diff --git a/src/tawashi/main.cpp b/src/tawashi/main.cpp index 1074f08..c9d4241 100644 --- a/src/tawashi/main.cpp +++ b/src/tawashi/main.cpp @@ -54,8 +54,14 @@ namespace { } template - std::unique_ptr make_response (const Kakoune::SafePtr& parSettings, const Kakoune::SafePtr& parCgiEnv) { - return static_cast>(std::make_unique(parSettings, &std::cout, parCgiEnv)); + std::unique_ptr make_response ( + const Kakoune::SafePtr& parFactory, + const Kakoune::SafePtr& parSettings, + const Kakoune::SafePtr& parCgiEnv + ) { + return static_cast>( + std::make_unique(parFactory, parSettings, &std::cout, parCgiEnv) + ); } void fill_defaults (tawashi::SettingsBag& parSettings) { diff --git a/src/tawashi_implem/index_response.cpp b/src/tawashi_implem/index_response.cpp index 76847f0..2bc58d0 100644 --- a/src/tawashi_implem/index_response.cpp +++ b/src/tawashi_implem/index_response.cpp @@ -20,11 +20,12 @@ namespace tawashi { IndexResponse::IndexResponse ( + const Kakoune::SafePtr& parFactory, const Kakoune::SafePtr& parSettings, std::ostream* parStreamOut, const Kakoune::SafePtr& parCgiEnv ) : - Response(parSettings, parStreamOut, parCgiEnv, false) + Response(parFactory, parSettings, parStreamOut, parCgiEnv, false) { } } //namespace tawashi diff --git a/src/tawashi_implem/index_response.hpp b/src/tawashi_implem/index_response.hpp index 66963ae..fd6899a 100644 --- a/src/tawashi_implem/index_response.hpp +++ b/src/tawashi_implem/index_response.hpp @@ -24,6 +24,7 @@ namespace tawashi { class IndexResponse : public Response { public: IndexResponse ( + const Kakoune::SafePtr& parFactory, const Kakoune::SafePtr& parSettings, std::ostream* parStreamOut, const Kakoune::SafePtr& parCgiEnv diff --git a/src/tawashi_implem/pastie_response.cpp b/src/tawashi_implem/pastie_response.cpp index 18df2b2..005b209 100644 --- a/src/tawashi_implem/pastie_response.cpp +++ b/src/tawashi_implem/pastie_response.cpp @@ -31,11 +31,12 @@ namespace tawashi { } //unnamed namespace PastieResponse::PastieResponse ( + const Kakoune::SafePtr& parFactory, const Kakoune::SafePtr& parSettings, std::ostream* parStreamOut, const Kakoune::SafePtr& parCgiEnv ) : - Response(parSettings, parStreamOut, parCgiEnv, true), + Response(parFactory, parSettings, parStreamOut, parCgiEnv, true), m_langmap_dir(parSettings->as("langmap_dir")), m_plain_text(false), m_syntax_highlight(true) diff --git a/src/tawashi_implem/pastie_response.hpp b/src/tawashi_implem/pastie_response.hpp index 8301f99..f373b8b 100644 --- a/src/tawashi_implem/pastie_response.hpp +++ b/src/tawashi_implem/pastie_response.hpp @@ -25,6 +25,7 @@ namespace tawashi { class PastieResponse : public Response { public: PastieResponse ( + const Kakoune::SafePtr& parFactory, const Kakoune::SafePtr& parSettings, std::ostream* parStreamOut, const Kakoune::SafePtr& parCgiEnv diff --git a/src/tawashi_implem/response.cpp b/src/tawashi_implem/response.cpp index fe32365..386855c 100644 --- a/src/tawashi_implem/response.cpp +++ b/src/tawashi_implem/response.cpp @@ -23,6 +23,7 @@ #include "pathname/pathname.hpp" #include "list_highlight_langs.hpp" #include "cgi_env.hpp" +#include "response_factory.hpp" #include #include #include @@ -108,12 +109,14 @@ namespace tawashi { } //unnamed namespace Response::Response ( + const Kakoune::SafePtr& parFactory, const Kakoune::SafePtr& parSettings, std::ostream* parStreamOut, const Kakoune::SafePtr& parCgiEnv, bool parWantRedis ) : m_resp_value(g_def_response_type), + m_factory(parFactory), //m_page_basename(fetch_page_basename(m_cgi_env)), m_cgi_env(parCgiEnv), m_settings(parSettings), @@ -124,6 +127,7 @@ namespace tawashi { { assert(m_cgi_env); assert(m_stream_out); + assert(m_factory); if (parWantRedis) { m_redis = std::make_unique(make_incredis(*parSettings)); diff --git a/src/tawashi_implem/response.hpp b/src/tawashi_implem/response.hpp index b84f01b..910930e 100644 --- a/src/tawashi_implem/response.hpp +++ b/src/tawashi_implem/response.hpp @@ -30,6 +30,7 @@ namespace redis { namespace tawashi { class SettingsBag; + class ResponseFactory; namespace cgi { class Env; @@ -48,6 +49,7 @@ namespace tawashi { }; Response ( + const Kakoune::SafePtr& parFactory, const Kakoune::SafePtr& parSettings, std::ostream* parStreamOut, const Kakoune::SafePtr& parCgiEnv, @@ -67,6 +69,7 @@ namespace tawashi { virtual std::string on_mustache_retrieve(); std::string m_resp_value; + Kakoune::SafePtr m_factory; Kakoune::SafePtr m_cgi_env; Kakoune::SafePtr m_settings; std::string m_website_root; diff --git a/src/tawashi_implem/response_factory.cpp b/src/tawashi_implem/response_factory.cpp index 208e2cd..aabf6f1 100644 --- a/src/tawashi_implem/response_factory.cpp +++ b/src/tawashi_implem/response_factory.cpp @@ -45,11 +45,12 @@ namespace tawashi { //spdlog::get("statuslog")->info("making response object for \"{}\"", parName); auto maker_it = m_local_data->makers.find(std::string(parName.data(), parName.size())); + Kakoune::SafePtr self(this); if (m_local_data->makers.end() != maker_it) { - return maker_it->second(m_local_data->settings, m_local_data->cgi_env); + return maker_it->second(self, m_local_data->settings, m_local_data->cgi_env); } else if (m_local_data->jolly_maker) { - return m_local_data->jolly_maker(m_local_data->settings, m_local_data->cgi_env); + return m_local_data->jolly_maker(self, m_local_data->settings, m_local_data->cgi_env); } else { assert(false); diff --git a/src/tawashi_implem/response_factory.hpp b/src/tawashi_implem/response_factory.hpp index bc42e92..2129468 100644 --- a/src/tawashi_implem/response_factory.hpp +++ b/src/tawashi_implem/response_factory.hpp @@ -28,9 +28,13 @@ namespace tawashi { class Env; } //namespace cgi - class ResponseFactory { + class ResponseFactory : public Kakoune::SafeCountable { public: - typedef std::function(const Kakoune::SafePtr&, const Kakoune::SafePtr& parCgiEnv)> ResponseMakerFunc; + typedef std::function( + const Kakoune::SafePtr&, + const Kakoune::SafePtr&, + const Kakoune::SafePtr& + )> ResponseMakerFunc; explicit ResponseFactory (const Kakoune::SafePtr& parSettings, const Kakoune::SafePtr& parCgiEnv); ~ResponseFactory() noexcept; diff --git a/src/tawashi_implem/submit_paste_response.cpp b/src/tawashi_implem/submit_paste_response.cpp index 1b0820d..a93d3e5 100644 --- a/src/tawashi_implem/submit_paste_response.cpp +++ b/src/tawashi_implem/submit_paste_response.cpp @@ -90,11 +90,12 @@ namespace tawashi { } //unnamed namespace SubmitPasteResponse::SubmitPasteResponse ( + const Kakoune::SafePtr& parFactory, const Kakoune::SafePtr& parSettings, std::ostream* parStreamOut, const Kakoune::SafePtr& parCgiEnv ) : - Response(parSettings, parStreamOut, parCgiEnv, true) + Response(parFactory, parSettings, parStreamOut, parCgiEnv, true) { this->change_type(Response::ContentType, "text/plain"); } diff --git a/src/tawashi_implem/submit_paste_response.hpp b/src/tawashi_implem/submit_paste_response.hpp index 55ad0f5..d40d09a 100644 --- a/src/tawashi_implem/submit_paste_response.hpp +++ b/src/tawashi_implem/submit_paste_response.hpp @@ -26,6 +26,7 @@ namespace tawashi { class SubmitPasteResponse : public Response { public: SubmitPasteResponse ( + const Kakoune::SafePtr& parFactory, const Kakoune::SafePtr& parSettings, std::ostream* parStreamOut, const Kakoune::SafePtr& parCgiEnv diff --git a/test/unit/test_index_response.cpp b/test/unit/test_index_response.cpp index 1e4bc95..8cf1051 100644 --- a/test/unit/test_index_response.cpp +++ b/test/unit/test_index_response.cpp @@ -21,6 +21,7 @@ #include "ini_file.hpp" #include "settings_bag.hpp" #include "safe_stack_object.hpp" +#include "response_factory.hpp" #include #include #include @@ -57,11 +58,12 @@ namespace tawashi { class IndexResponseCustomMustache : public IndexResponse { public: IndexResponseCustomMustache ( + const Kakoune::SafePtr& parFactory, const Kakoune::SafePtr& parSettings, std::ostream* parStreamOut, const Kakoune::SafePtr& parCgiEnv ) : - IndexResponse(parSettings, parStreamOut, parCgiEnv) + IndexResponse(parFactory, parSettings, parStreamOut, parCgiEnv) { } @@ -106,7 +108,8 @@ TEST_CASE ("Index response", "[index][response]") { std::stringstream response_stream; - tawashi::IndexResponseCustomMustache response(settings, &response_stream, fake_env); + SafeStackObject resp_factory(settings, fake_env); + tawashi::IndexResponseCustomMustache response(resp_factory, settings, &response_stream, fake_env); response.send(); response_stream.seekg(0);