Temporarily switch GenerateRoute to use shared_ptrs

This commit is contained in:
S David 2024-03-15 23:43:13 -04:00
parent f57dbae491
commit cc456733fd
6 changed files with 21 additions and 26 deletions

View File

@ -21,7 +21,7 @@ class Application {
public:
virtual ~Application() = default;
// @{ Disabled constructors and operators
// Disabled constructors and operators @{
Application(Application&&) = delete;
Application(const Application&) = delete;
auto operator=(const Application&) -> Application& = delete;

View File

@ -10,6 +10,8 @@
#pragma once
#include "Application.hpp"
#include "IRequestHandler.hpp"
#include "types.hpp"
#include <memory>
@ -20,7 +22,9 @@ namespace mdml {
class IRouteHandler;
class CgiApplication : public Application {
class CgiApplication
: public Application
, public IRequestHandler {
public:
using RouteDictionary = Dictionary<std::shared_ptr<IRouteHandler>>;
@ -36,7 +40,7 @@ class CgiApplication : public Application {
// @}
auto run() -> int override;
auto processRequest() -> Result<std::string>;
auto processRequest() -> Result<std::string> override;
/// \todo make this private, and have the unit test inspect it with a
/// helper friend class.

View File

@ -46,7 +46,8 @@ class MarkdownRouteHandler : public IRouteHandler {
// NOLINTNEXTLINE(readability-identifier-naming)
static auto GenerateRoutes(
MarkdownRouteHandler& ref, std::filesystem::path content_dir,
std::shared_ptr<MarkdownRouteHandler> ref,
std::filesystem::path content_dir,
std::filesystem::path main_template
) -> Dictionary<std::shared_ptr<IRouteHandler>>;

View File

@ -25,11 +25,7 @@ using namespace mdml;
CgiApplication::CgiApplication(
int argc, c::const_string argv[], c::const_string env[]
)
: Application(argc, argv, env)
, Routes()
#ifdef TESTING
, Routes(this->Routes)
#endif
: Application(argc, argv, env), Routes()
{
}
CgiApplication::~CgiApplication()
@ -85,13 +81,6 @@ CgiApplication::processRequest() -> Result<std::string>
auto result =
route_handler->process(page_name, request_uri);
/*
if (result.IsError) {
auto except =
Exception(result.ErrorData.c_str()); throw except;
}
*/
return result;
} else {
std::stringstream buffer;

View File

@ -91,16 +91,17 @@ MarkdownRouteHandler::process(
return { NoError, document };
}
auto
auto // NOLINTNEXTLINE(readability-identifier-naming)
MarkdownRouteHandler::GenerateRoutes(
MarkdownRouteHandler& ref, fs::path content_dir, fs::path main_template
std::shared_ptr<MarkdownRouteHandler> ref, fs::path content_dir,
fs::path main_template
) -> Dictionary<std::shared_ptr<IRouteHandler>>
{
if (content_dir.is_relative()) {
content_dir = fs::canonical((ref.work_dir / content_dir));
content_dir = fs::canonical((ref->work_dir / content_dir));
}
if (main_template.is_relative()) {
main_template = fs::canonical(ref.work_dir / main_template);
main_template = fs::canonical(ref->work_dir / main_template);
}
Dictionary<std::shared_ptr<IRouteHandler>> results;
@ -116,13 +117,13 @@ MarkdownRouteHandler::GenerateRoutes(
auto& route_handler = ref;
// Load template and document
route_handler.loadTemplate(main_template.string()
);
route_handler.loadMarkdown(entry.path().string()
route_handler->loadTemplate(main_template.string(
));
route_handler->loadMarkdown(entry.path().string()
);
// Move the routeHandler into the vector
results.emplace(routename, &route_handler);
results.emplace(routename, route_handler);
}
}
}

View File

@ -42,10 +42,10 @@ BEGIN_TEST_SUITE("MarkdownRouteHandler Unit Tests")
TEST("MarkdownRouteHandler Static Route Generator")
{
auto router = new MarkdownRouteHandler("./data");
auto router = std::make_shared<MarkdownRouteHandler>("./data");
REQUIRE_NOTHROW([&]() {
auto routes = MarkdownRouteHandler::GenerateRoutes(
*router, "content", "templates/main.thtml"
router, "content", "templates/main.thtml"
);
/* REQUIRE(routes.size() > 0);