From afdd7e9b69b26aa7fadb65a0da6aa9a21341bc4c Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Wed, 26 Aug 2020 14:38:20 +0200 Subject: :( --- include/Handler.hpp | 28 +++++++--------------------- src/Handler.cpp | 16 ++++++++++++++++ src/SimpleHandlers.cpp | 4 +++- src/main.cpp | 22 +++++++--------------- 4 files changed, 33 insertions(+), 37 deletions(-) diff --git a/include/Handler.hpp b/include/Handler.hpp index 63efa5d..7bbefcd 100644 --- a/include/Handler.hpp +++ b/include/Handler.hpp @@ -13,30 +13,16 @@ namespace Handler { void *payload)> handler_function; struct CommandHandler { - CommandHandler(std::string command, handler_function func, std::vector *description, void *payload = nullptr) - : command{std::move(command)}, func{std::move(func)}, description{description}, payload{payload} {} - - ~CommandHandler() { - delete description; - description = nullptr; - } - - CommandHandler(CommandHandler &&o) noexcept { - command = std::move(o.command); - func = std::move(o.func); - description = o.description; - o.description = nullptr; - payload = o.payload; - o.payload = nullptr; - } - - [[nodiscard]] json exec(std::string const &arguments, std::string const &session) const { - return func(arguments, session, payload); - } + CommandHandler(std::string command, handler_function func, std::vector *description, + void *payload = nullptr); + + CommandHandler(CommandHandler &&o) noexcept; + + [[nodiscard]] json exec(std::string const &arguments, std::string const &session) const; std::string command; handler_function func{nullptr}; - // handler takes ownership of the description, using it after passing is unsafe + // handler does NOT take ownership of the description, you have to delete it - after this object is destroyed std::vector *description{nullptr}; void *payload{nullptr}; }; diff --git a/src/Handler.cpp b/src/Handler.cpp index 1c0cd12..b5ed9f0 100644 --- a/src/Handler.cpp +++ b/src/Handler.cpp @@ -14,3 +14,19 @@ std::vector Handler::tokenizeArguments(const std::string &arguments return tokens; } + +Handler::CommandHandler::CommandHandler(Handler::CommandHandler &&o) noexcept { + // this is a copy constructor, disguised as a move constructor.. I am sorry + command = o.command; + func = o.func; + description = o.description; // this is the main problem, any "moved" object will point to the same description TODO: maybe smart pointer can help? + payload = o.payload; +} + +Handler::json Handler::CommandHandler::exec(const std::string &arguments, const std::string &session) const { + return func(arguments, session, payload); +} + +Handler::CommandHandler::CommandHandler(std::string command, Handler::handler_function func, + std::vector *description, void *payload) + : command{std::move(command)}, func{std::move(func)}, description{description}, payload{payload} {} diff --git a/src/SimpleHandlers.cpp b/src/SimpleHandlers.cpp index 0ebdd79..0b10247 100644 --- a/src/SimpleHandlers.cpp +++ b/src/SimpleHandlers.cpp @@ -36,7 +36,9 @@ Handler::json Handler::helpHandler(std::string const &arguments, std::string con for (auto const &itor : *commands) { std::vector commandAnnotations; - commandAnnotations.emplace_back(create_annotation(Reply::AnnotationType::command, std::move(*itor.description))); + + commandAnnotations.emplace_back( + create_annotation(Reply::AnnotationType::command, std::move(*itor.description))); reply_vec.emplace_back(create_text("- ")); reply_vec.emplace_back(create_text(itor.command, std::move(commandAnnotations))); diff --git a/src/main.cpp b/src/main.cpp index 8289584..562e20b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include "crow.h" @@ -13,7 +12,6 @@ #include "GetEssen.hpp" #include "SimpleHandlers.hpp" #include "RelationshipHandler.hpp" - /* END Handlers */ int main() { @@ -47,9 +45,7 @@ int main() { CROW_ROUTE(app, "/") .methods("POST"_method) - ([&commands]( - crow::request const &request - ) { + ([&commands](crow::request const &request) { auto data = crow::json::load(request.body); if (!data) @@ -58,8 +54,8 @@ int main() { if (!data.count("command")) return crow::response(400, "malformed request: missing `command` field\n"); - std::string command = (data.count("command") ? data["command"].s() : std::string( - "No such command!")); + std::string command = (data.count("command") ? data["command"].s() : + std::string("No such command!")); std::string arguments = (data.count("arguments") ? data["arguments"].s() : std::string("")); std::string session = (data.count("session") ? data["session"].s() : std::string("null")); @@ -77,14 +73,10 @@ int main() { Response::simple_response("No such command!", "null", false)}; }); - app.port(18080). - - multithreaded() - - . + app.port(18080).multithreaded().run(); - run(); + for (auto &&it : commands) + delete it.description; - std::cout << "Stopped successfully" << - std::endl; + std::cout << "Stopped successfully" << std::endl; } -- cgit v1.2.3-54-g00ecf