SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
Value.cpp
Go to the documentation of this file.
2
3#include <sstream>
4
5using namespace dmxpp;
6
7// NOLINTNEXTLINE(*-no-recursion)
8std::string Value::IDToString(ID id) {
9 switch (id) {
10 case ID::INVALID:
11 break;
12 case ID::ELEMENT:
13 return "element";
14 case ID::INT:
15 return "int";
16 case ID::FLOAT:
17 return "float";
18 case ID::BOOL:
19 return "bool";
20 case ID::STRING:
21 return "string";
22 case ID::BYTEARRAY:
23 return "binary";
24 case ID::TIME:
25 return "time";
26 case ID::COLOR:
27 return "color";
28 case ID::VECTOR2:
29 return "vector2";
30 case ID::EULER_ANGLE:
31 case ID::VECTOR3:
32 return "vector3";
33 case ID::VECTOR4:
34 return "vector4";
35 case ID::QUATERNION:
36 return "quaternion";
37 case ID::MATRIX_4X4:
38 return "matrix";
39 case ID::ARRAY_ELEMENT:
40 case ID::ARRAY_INT:
41 case ID::ARRAY_FLOAT:
42 case ID::ARRAY_BOOL:
43 case ID::ARRAY_STRING:
44 case ID::ARRAY_BYTEARRAY:
45 case ID::ARRAY_TIME:
46 case ID::ARRAY_COLOR:
47 case ID::ARRAY_VECTOR2:
48 case ID::ARRAY_EULER_ANGLE:
49 case ID::ARRAY_VECTOR3:
50 case ID::ARRAY_VECTOR4:
51 case ID::ARRAY_QUATERNION:
52 case ID::ARRAY_MATRIX_4X4:
53 return IDToString(arrayIDToInnerID(id)) + "_array";
54 }
55 return "invalid";
56}
57
59 return this->type >= Value::ID::ARRAY_START;
60}
61
62std::string DMXAttribute::getValue() const {
63 switch (this->type) {
64 using enum Value::ID;
65 case INVALID:
66 return Value::IDToString(this->type);
67 case ELEMENT: {
68 const auto [index, stubGUID] = this->getValueAs<Value::Element>();
69 if (index == -2) {
70 return "GUID: " + stubGUID;
71 }
72 return '#' + std::to_string(index);
73 }
74 case INT:
75 return std::to_string(this->getValueAs<int32_t>());
76 case FLOAT:
77 return std::to_string(this->getValueAs<float>());
78 case BOOL:
79 return this->getValueAs<bool>() ? "true" : "false";
80 case STRING:
81 return this->getValueAs<std::string>();
82 case BYTEARRAY: {
83 std::string out;
84 for (auto byte : this->getValueAs<Value::ByteArray>()) {
85 std::stringstream ss;
86 ss << std::hex << std::uppercase;
87 ss << static_cast<unsigned char>(byte);
88 out += ss.str();
89 }
90 return out;
91 }
92 case TIME:
93 return std::to_string(this->getValueAs<float>());
94 case COLOR: {
95 const auto [r, g, b, a] = this->getValueAs<Value::Color>();
96 return "rgba(" + std::to_string(r) + ", " + std::to_string(g) + ", " + std::to_string(b) + ", " + std::to_string(a) + ')';
97 }
98 case VECTOR2: {
99 const auto vec2 = this->getValueAs<Value::Vector2>();
100 return '[' + std::to_string(vec2[0]) + ", " + std::to_string(vec2[1]) + ']';
101 }
102 case VECTOR3:
103 case EULER_ANGLE: {
104 const auto vec3 = this->getValueAs<Value::Vector3>();
105 return '[' + std::to_string(vec3[0]) + ", " + std::to_string(vec3[1]) + ", " + std::to_string(vec3[2]) + ']';
106 }
107 case VECTOR4:
108 case QUATERNION: {
109 const auto vec4 = this->getValueAs<Value::Vector4>();
110 return '[' + std::to_string(vec4[0]) + ", " + std::to_string(vec4[1]) + ", " + std::to_string(vec4[2]) + ", " + std::to_string(vec4[3]) + ']';
111 }
112 case MATRIX_4X4: {
113 const auto mat4 = this->getValueAs<Value::Matrix4x4>();
114 std::string out;
115 for (int i = 0; i < 4; i++) {
116 out += (i == 0 ? '[' : ' ');
117 for (int j = 0; j < 4; j++) {
118 out += std::to_string(mat4[i][j]);
119 if (j < 3) {
120 out += ", ";
121 } else if (i < 3) {
122 out += ",\n";
123 } else {
124 out += ']';
125 }
126 }
127 }
128 return out;
129 }
130 case ARRAY_ELEMENT: {
131 const auto elements = this->getValueAs<std::vector<Value::Element>>();
132 std::string out = "[";
133 for (int i = 0; i < elements.size(); i++) {
134 if (elements[i].index == -2) {
135 out += (i == 0 ? "" : " ") + std::string{"GUID: "} + elements[i].stubGUID + (i == elements.size() - 1 ? "" : ",");
136 } else {
137 out += (i == 0 ? "" : " ") + std::string{"#"} + std::to_string(elements[i].index) + (i == elements.size() - 1 ? "" : ",");
138 }
139 }
140 return out + ']';
141 }
142 case ARRAY_INT: {
143 const auto ints = this->getValueAs<std::vector<int>>();
144 std::string out = "[";
145 for (int i = 0; i < ints.size(); i++) {
146 out += (i == 0 ? "" : " ") + std::to_string(ints[i]) + (i == ints.size() - 1 ? "" : ",");
147 }
148 return out + ']';
149 }
150 case ARRAY_FLOAT: {
151 const auto floats = this->getValueAs<std::vector<float>>();
152 std::string out = "[";
153 for (int i = 0; i < floats.size(); i++) {
154 out += (i == 0 ? "" : " ") + std::to_string(floats[i]) + (i == floats.size() - 1 ? "" : ",");
155 }
156 return out + ']';
157 }
158 case ARRAY_BOOL: {
159 const auto bools = this->getValueAs<std::vector<bool>>();
160 std::string out = "[";
161 for (int i = 0; i < bools.size(); i++) {
162 out += (i == 0 ? "" : " ") + std::string{bools[i] ? "true" : "false"} + (i == bools.size() - 1 ? "" : ",");
163 }
164 return out + ']';
165 }
166 case ARRAY_STRING: {
167 const auto strings = this->getValueAs<std::vector<std::string>>();
168 std::string out = "[";
169 for (int i = 0; i < strings.size(); i++) {
170 out += (i == 0 ? "" : " ") + strings[i] + (i == strings.size() - 1 ? "" : ",\n");
171 }
172 return out + ']';
173 }
174 case ARRAY_BYTEARRAY: {
175 const auto bytearrays = this->getValueAs<std::vector<Value::ByteArray>>();
176 std::string out = "[";
177 for (int i = 0; i < bytearrays.size(); i++) {
178 std::string hex;
179 for (auto byte : bytearrays[i]) {
180 std::stringstream ss;
181 ss << std::hex << std::uppercase;
182 ss << static_cast<unsigned char>(byte);
183 hex += ss.str();
184 }
185 out += (i == 0 ? "" : " ") + hex + (i == bytearrays.size() - 1 ? "" : ",\n");
186 }
187 return out + ']';
188 }
189 case ARRAY_TIME: {
190 const auto times = this->getValueAs<std::vector<float>>();
191 std::string out = "[";
192 for (int i = 0; i < times.size(); i++) {
193 out += (i == 0 ? "" : " ") + std::to_string(times[i]) + (i == times.size() - 1 ? "" : ",");
194 }
195 return out + ']';
196 }
197 case ARRAY_COLOR: {
198 const auto colors = this->getValueAs<std::vector<Value::Color>>();
199 std::string out = "[";
200 for (int i = 0; i < colors.size(); i++) {
201 out += (i == 0 ? "" : " ") + std::string{"rgba("} + std::to_string(colors[i].r) + ", " + std::to_string(colors[i].g) + ", " + std::to_string(colors[i].b) + ", " + std::to_string(colors[i].a) + ')' + (i == colors.size() - 1 ? "" : ",\n");
202 }
203 return out + ']';
204 }
205 case ARRAY_VECTOR2: {
206 const auto vectors = this->getValueAs<std::vector<Value::Vector2>>();
207 std::string out = "[";
208 for (int i = 0; i < vectors.size(); i++) {
209 out += (i == 0 ? "" : " ") + std::string{"["} + std::to_string(vectors[i][0]) + ", " + std::to_string(vectors[i][1]) + ']' + (i == vectors.size() - 1 ? "" : ",\n");
210 }
211 return out + ']';
212 }
213 case ARRAY_VECTOR3:
214 case ARRAY_EULER_ANGLE: {
215 const auto vectors = this->getValueAs<std::vector<Value::Vector3>>();
216 std::string out = "[";
217 for (int i = 0; i < vectors.size(); i++) {
218 out += (i == 0 ? "" : " ") + std::string{"["} + std::to_string(vectors[i][0]) + ", " + std::to_string(vectors[i][1]) + ", " + std::to_string(vectors[i][2]) + ']' + (i == vectors.size() - 1 ? "" : ",\n");
219 }
220 return out + ']';
221 }
222 case ARRAY_VECTOR4:
223 case ARRAY_QUATERNION: {
224 const auto vectors = this->getValueAs<std::vector<Value::Vector4>>();
225 std::string out = "[";
226 for (int i = 0; i < vectors.size(); i++) {
227 out += (i == 0 ? "" : " ") + std::string{"["} + std::to_string(vectors[i][0]) + ", " + std::to_string(vectors[i][1]) + ", " + std::to_string(vectors[i][2]) + ']' + ", " + std::to_string(vectors[i][3]) + ']' + (i == vectors.size() - 1 ? "" : ",\n");
228 }
229 return out + ']';
230 }
231 case ARRAY_MATRIX_4X4: {
232 const auto matrices = this->getValueAs<std::vector<Value::Matrix4x4>>();
233 std::string out = "[";
234 for (int m = 0; m < matrices.size(); m++) {
235 out += (m == 0 ? "[" : " [");
236 for (int i = 0; i < 4; i++) {
237 out += (i == 0 ? "" : " ");
238 for (int j = 0; j < 4; j++) {
239 out += std::to_string(matrices[m][i][j]);
240 if (j < 3) {
241 out += ", ";
242 } else if (i < 3) {
243 out += ",\n";
244 } else {
245 out += ']';
246 }
247 }
248 }
249 if (m < matrices.size() - 1) {
250 out += ",\n";
251 }
252 }
253 return out + ']';
254 }
255 default:
256 break;
257 }
258 return "";
259}
std::string IDToString(ID id)
Definition: Value.cpp:8
constexpr ID arrayIDToInnerID(ID id)
Definition: Value.h:128
Definition: dmxpp.h:7
Value::ID type
Definition: Value.h:169
std::string getValue() const
Definition: Value.cpp:62
bool isArray() const
Definition: Value.cpp:58