author | František Kučera <franta-hg@frantovo.cz> |
Mon, 27 Jan 2020 00:03:14 +0100 | |
branch | v_0 |
changeset 63 | 8c6885543e2c |
child 66 | 8a8b6434e4bb |
permissions | -rw-r--r-- |
63
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Relational pipes |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info) |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
#pragma once |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <iostream> |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <exception> |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
#include <vector> |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <string> |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include <sstream> |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include <codecvt> |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
#include <locale> |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
#include "../src/StreamletMsg.h" |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
/** |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
* Unlike the protocol and the message format, |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
* these helper classes and functions are not part of the public API. |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
* Thus when writing custom streamlets, it is better to copy this file |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
* and review its changes while upgrading to new upstream version. |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
*/ |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
using S = relpipe::in::filesystem::StreamletMsg; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
class Streamlet { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
private: |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
class Message { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
public: |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
int code; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
std::vector<std::wstring> parameters; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
Message() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
Message(int code) : code(code) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
Message(int code, std::vector<std::wstring> parameters) : code(code), parameters(parameters) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
Message(int code, std::wstring p1) : code(code), parameters({p1}) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
58 |
Message(int code, std::wstring p1, std::wstring p2) : code(code), parameters({p1, p2}) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
}; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
static const char SEPARATOR = '\0'; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
int readInt() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
return std::stoi(readString()); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
std::wstring readString() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
std::stringstream s; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
for (char ch; std::cin.read(&ch, 1).good() && ch != SEPARATOR;) s.put(ch); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
71 |
return convertor.from_bytes(s.str()); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
void writeString(std::wstring s) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
75 |
std::cout << convertor.to_bytes(s.c_str()); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
76 |
std::cout.put(SEPARATOR); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
77 |
if (std::cout.bad()) throw std::runtime_error("Unable to write to sub-process."); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
78 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
79 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
80 |
void writeInt(int i) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
81 |
writeString(std::to_wstring(i)); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
82 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
83 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
84 |
void flush() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
85 |
std::cout.flush(); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
86 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
87 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
88 |
Message read() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
89 |
Message m; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
90 |
m.code = readInt(); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
91 |
int count = readInt(); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
92 |
for (int i = 0; i < count; i++) m.parameters.push_back(readString()); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
93 |
return m; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
94 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
95 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
96 |
void processMessages() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
97 |
while (true) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
98 |
Message m = read(); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
99 |
if (m.code == S::VERSION_SUPPORTED) processVersionSupported(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
100 |
else if (m.code == S::WAITING_FOR_VERSION) processWaitingForVersion(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
101 |
else if (m.code == S::RELATION_START) processRelationStart(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
102 |
else if (m.code == S::INPUT_ATTRIBUTE_METADATA) processInputAttributeMetadata(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
103 |
else if (m.code == S::OUTPUT_ATTRIBUTE_ALIAS) processOutputAttributeAlias(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
104 |
else if (m.code == S::OPTION) processOption(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
105 |
else if (m.code == S::INPUT_ATTRIBUTE) processInputAttribute(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
106 |
else if (m.code == S::WAITING_FOR_OUTPUT_ATTRIBUTES_METADATA) processWaitingForOutputAttributesMetadata(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
107 |
else if (m.code == S::WAITING_FOR_OUTPUT_ATTRIBUTES) processWaitingForOutputAttributes(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
108 |
else if (m.code == S::RELATION_END) break; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
109 |
else processUnsupportedMessage(m); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
110 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
111 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
112 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
113 |
protected: |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
114 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
115 |
class AttributeMetadata { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
116 |
public: |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
117 |
std::wstring name; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
118 |
std::wstring type; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
119 |
}; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
120 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
121 |
class OutputAttribute { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
122 |
public: |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
123 |
std::wstring value; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
124 |
bool isNull; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
125 |
}; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
126 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
127 |
class Option { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
128 |
public: |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
129 |
std::wstring name; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
130 |
std::wstring value; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
131 |
}; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
132 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
133 |
std::vector<std::wstring> versionsSupported; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
134 |
std::vector<AttributeMetadata> inputAttributes; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
135 |
std::vector<std::wstring> outputAttributeAliases; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
136 |
std::vector<Option> options; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
137 |
std::wstring currentRelation; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
138 |
std::wstring currentFile; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
139 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
140 |
std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. Or use always UTF-8 for communication with subprocesses. |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
141 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
142 |
static const std::wstring BOOLEAN; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
143 |
static const std::wstring INTEGER; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
144 |
static const std::wstring STRING; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
145 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
146 |
virtual void write(Message m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
147 |
writeInt(m.code); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
148 |
writeInt(m.parameters.size()); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
149 |
for (auto p : m.parameters) writeString(p); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
150 |
flush(); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
151 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
152 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
153 |
virtual void processVersionSupported(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
154 |
versionsSupported.push_back(m.parameters[0]); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
155 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
156 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
157 |
virtual void processWaitingForVersion(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
158 |
// TODO: check that 1 is supported |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
159 |
write({S::VERSION_ACCEPTED, L"1"}); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
160 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
161 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
162 |
virtual void processRelationStart(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
163 |
currentRelation = m.parameters[0]; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
164 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
165 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
166 |
virtual void processInputAttributeMetadata(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
167 |
inputAttributes.push_back({m.parameters[0], m.parameters[1]}); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
168 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
169 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
170 |
virtual void processOutputAttributeAlias(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
171 |
outputAttributeAliases.push_back(m.parameters[0]); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
172 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
173 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
174 |
virtual void processOption(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
175 |
options.push_back({m.parameters[0], m.parameters[1]}); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
176 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
177 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
178 |
virtual void processInputAttribute(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
179 |
int index = std::stoi(m.parameters[0]); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
180 |
std::wstring value = m.parameters[1]; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
181 |
bool isNull = m.parameters[2] == L"true"; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
182 |
if (inputAttributes[index].name == L"path") currentFile = value; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
183 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
184 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
185 |
virtual void processWaitingForOutputAttributesMetadata(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
186 |
for (AttributeMetadata am : getOutputAttributesMetadata()) write({S::OUTPUT_ATTRIBUTE_METADATA, am.name, am.type}); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
187 |
write({S::WAITING_FOR_INPUT_ATTRIBUTES}); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
188 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
189 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
190 |
virtual void processWaitingForOutputAttributes(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
191 |
for (OutputAttribute oa : getOutputAttributes()) write({S::OUTPUT_ATTRIBUTE, oa.value, oa.isNull ? L"true" : L"false"}); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
192 |
write({S::WAITING_FOR_INPUT_ATTRIBUTES}); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
193 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
194 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
195 |
virtual void processUnsupportedMessage(Message& m) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
196 |
write({S::STREAMLET_ERROR, L"UNSUPPORTED_MESSAGE"}); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
197 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
198 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
199 |
virtual std::wstring getAlias(int index, const std::wstring& defaultValue) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
200 |
if (outputAttributeAliases.size() > index) return outputAttributeAliases[index]; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
201 |
else return defaultValue; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
202 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
203 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
204 |
virtual std::vector<AttributeMetadata> getOutputAttributesMetadata() = 0; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
205 |
virtual std::vector<OutputAttribute> getOutputAttributes() = 0; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
206 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
207 |
public: |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
208 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
209 |
virtual ~Streamlet() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
210 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
211 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
212 |
int run() { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
213 |
try { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
214 |
processMessages(); |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
215 |
return 0; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
216 |
} catch (...) { |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
217 |
return 1; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
218 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
219 |
} |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
220 |
}; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
221 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
222 |
const std::wstring Streamlet::BOOLEAN = L"boolean"; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
223 |
const std::wstring Streamlet::INTEGER = L"integer"; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
224 |
const std::wstring Streamlet::STRING = L"string"; |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
225 |
|
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
226 |
#define STREAMLET_RUN(clazz) \ |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
227 |
int main(int argc, char** argv) { \ |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
228 |
clazz s; \ |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
229 |
return s.run(); \ |
8c6885543e2c
streamlet examples: common functions in C/C++
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
230 |
} |