|
1 /** |
|
2 * Relational pipes |
|
3 * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, version 3 of the License. |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 */ |
|
17 #pragma once |
|
18 |
|
19 #include <vector> |
|
20 #include <map> |
|
21 #include <string> |
|
22 #include <sstream> |
|
23 |
|
24 #include <relpipe/writer/typedefs.h> |
|
25 #include <relpipe/writer/RelpipeWriterException.h> |
|
26 |
|
27 /** |
|
28 * TODO: move to a separate library → can be used later also in relpipe-tr-exec |
|
29 */ |
|
30 class SubProcess { |
|
31 public: |
|
32 |
|
33 class Message { |
|
34 public: |
|
35 int code; |
|
36 std::vector<relpipe::writer::string_t> parameters; |
|
37 |
|
38 Message() { |
|
39 } |
|
40 |
|
41 Message(int code) : code(code) { |
|
42 } |
|
43 |
|
44 Message(int code, std::vector<relpipe::writer::string_t> parameters) : code(code), parameters(parameters) { |
|
45 } |
|
46 |
|
47 Message(int code, relpipe::writer::string_t p1) : code(code), parameters({p1}) { |
|
48 } |
|
49 |
|
50 Message(int code, relpipe::writer::string_t p1, relpipe::writer::string_t p2) : code(code), parameters({p1, p2}) { |
|
51 } |
|
52 |
|
53 Message(int code, relpipe::writer::string_t p1, relpipe::writer::string_t p2, relpipe::writer::string_t p3) : code(code), parameters({p1, p2, p3}) { |
|
54 } |
|
55 |
|
56 relpipe::writer::string_t toString() { |
|
57 std::wstringstream s; |
|
58 s << L"Message(code: " << code << L", parameters: "; |
|
59 for (int i = 0; i < parameters.size(); i++) { |
|
60 if (i < parameters.size() - 1) s << parameters[i] << L","; |
|
61 else s << parameters[i]; |
|
62 } |
|
63 s << L")"; |
|
64 return s.str(); |
|
65 } |
|
66 }; |
|
67 |
|
68 class Exception : public relpipe::writer::RelpipeWriterException { |
|
69 public: |
|
70 |
|
71 Exception(std::wstring message) : relpipe::writer::RelpipeWriterException(message) { |
|
72 } |
|
73 |
|
74 }; |
|
75 |
|
76 static SubProcess* create(std::vector<relpipe::writer::string_t> commandLine, std::map<relpipe::writer::string_t, relpipe::writer::string_t> environment, bool dropErrorOutput = true); |
|
77 |
|
78 virtual Message read() = 0; |
|
79 virtual void write(Message message) = 0; |
|
80 virtual int wait() = 0; |
|
81 |
|
82 virtual ~SubProcess() = default; |
|
83 |
|
84 }; |