aboutsummaryrefslogtreecommitdiffstats
path: root/include/Handler.hpp
blob: 3328d9145db998eeaaf164c7a31a22ba1a4e2625 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#pragma once

#include <queue>
#include <string>
#include <functional>
#include <utility>

#include "crow.h"

namespace Handler {
    typedef crow::json::wvalue json;
    typedef std::function<json(std::string const &arguments, std::string const &session,
                               void *payload)> handler_function;

    struct CommandHandler {
        CommandHandler(std::string command, handler_function func, std::queue<json> *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 {
            func = o.func;
            description = o.description;
            o.description = nullptr;
            payload = o.payload;
        }

        [[nodiscard]] json exec(std::string const &arguments, std::string const &session) const {
            return func(arguments, session, payload);
        }

        std::string command;
        handler_function func{nullptr};
        // handler takes ownership of the description, using it after passing is unsafe
        std::queue<json> *description{nullptr};
        void *payload{nullptr};
    };

    std::vector<std::string> tokenizeArguments(std::string const &arguments, std::string const &delimiter = " ");
}