-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask.h
87 lines (57 loc) · 1.88 KB
/
Task.h
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Copyright (C) 2021 Ilya Entin
*/
#pragma once
#include <atomic>
#include <future>
#include <variant>
#include <vector>
#include <boost/core/noncopyable.hpp>
#include "Header.h"
using Response = std::vector<std::string>;
using TaskPtr = std::shared_ptr<class Task>;
using SIZETUPLE = std::tuple<unsigned, unsigned>;
using PreprocessRequest = SIZETUPLE (*)(std::string_view);
using ProcessRequestSort = std::string_view (*)(const SIZETUPLE&, std::string_view, bool diagnostics);
using ProcessRequestNoSort = std::string_view (*)(std::string_view, bool diagnostics);
using ProcessRequestEcho = std::string_view (*)(std::string_view);
using ProcessRequest = std::variant<ProcessRequestSort, ProcessRequestNoSort, ProcessRequestEcho>;
struct RequestRow {
RequestRow(std::string_view::const_iterator beg, std::string_view::const_iterator end);
~RequestRow() = default;
SIZETUPLE _sizeKey;
std::string_view _value;
unsigned _orgIndex = 0;
};
class Task : private boost::noncopyable {
enum Functions {
SORTFUNCTION,
NOSORTFUNCTION,
ECHOFUNCTION
};
std::vector<RequestRow> _rows;
std::vector<unsigned> _indices;
Response& _response;
std::promise<void> _promise;
std::atomic<unsigned> _index = 0;
bool _diagnostics;
HEADER _header;
static ProcessRequest _function;
static Response _emptyResponse;
public:
explicit Task(Response& response = _emptyResponse);
~Task() = default;
void update(std::string_view request);
void sortIndices();
void resetIndex() { _index = 0; }
std::promise<void>& getPromise() { return _promise; }
bool preprocessNext();
bool processNext();
void finish();
HEADER& header() { return _header; }
static void setProcessFunction(ProcessRequest function);
static void setPreprocessFunction(PreprocessRequest function) {
_preprocessRequest = function;
}
static PreprocessRequest _preprocessRequest;
};