forked from BehaviorTree/BehaviorTree.CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_types.cpp
More file actions
596 lines (544 loc) · 12.8 KB
/
basic_types.cpp
File metadata and controls
596 lines (544 loc) · 12.8 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#include "behaviortree_cpp/basic_types.h"
#include "behaviortree_cpp/json_export.h"
#include "behaviortree_cpp/tree_node.h"
#include <algorithm>
#include <array>
#include <charconv>
#if __cpp_lib_to_chars < 201611L
#include <clocale>
#endif
#include <cstdlib>
#include <cstring>
#include <tuple>
namespace BT
{
template <>
std::string toStr<NodeStatus>(const NodeStatus& status)
{
switch(status)
{
case NodeStatus::SUCCESS:
return "SUCCESS";
case NodeStatus::FAILURE:
return "FAILURE";
case NodeStatus::RUNNING:
return "RUNNING";
case NodeStatus::IDLE:
return "IDLE";
case NodeStatus::SKIPPED:
return "SKIPPED";
}
return "";
}
template <>
[[nodiscard]] std::string toStr<bool>(const bool& value)
{
return value ? "true" : "false";
}
template <>
std::string toStr<std::string>(const std::string& value)
{
return value;
}
std::string toStr(NodeStatus status, bool colored)
{
if(!colored)
{
return toStr(status);
}
switch(status)
{
case NodeStatus::SUCCESS:
return "\x1b[32m"
"SUCCESS"
"\x1b[0m"; // GREEN
case NodeStatus::FAILURE:
return "\x1b[31m"
"FAILURE"
"\x1b[0m"; // RED
case NodeStatus::RUNNING:
return "\x1b[33m"
"RUNNING"
"\x1b[0m"; // YELLOW
case NodeStatus::SKIPPED:
return "\x1b[34m"
"SKIPPED"
"\x1b[0m"; // BLUE
case NodeStatus::IDLE:
return "\x1b[36m"
"IDLE"
"\x1b[0m"; // CYAN
}
return "Undefined";
}
template <>
std::string toStr<PortDirection>(const PortDirection& direction)
{
switch(direction)
{
case PortDirection::INPUT:
return "Input";
case PortDirection::OUTPUT:
return "Output";
case PortDirection::INOUT:
return "InOut";
}
return "InOut";
}
template <>
std::string toStr<NodeType>(const NodeType& type)
{
switch(type)
{
case NodeType::ACTION:
return "Action";
case NodeType::CONDITION:
return "Condition";
case NodeType::DECORATOR:
return "Decorator";
case NodeType::CONTROL:
return "Control";
case NodeType::SUBTREE:
return "SubTree";
default:
return "Undefined";
}
}
template <>
std::string convertFromString<std::string>(StringView str)
{
return std::string(str.data(), str.size());
}
template <>
int64_t convertFromString<int64_t>(StringView str)
{
int64_t result = 0;
const auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
std::ignore = ptr;
if(ec != std::errc())
{
throw RuntimeError(StrCat("Can't convert string [", str, "] to integer"));
}
return result;
}
template <>
uint64_t convertFromString<uint64_t>(StringView str)
{
uint64_t result = 0;
const auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
std::ignore = ptr;
if(ec != std::errc())
{
throw RuntimeError(StrCat("Can't convert string [", str, "] to integer"));
}
return result;
}
namespace
{
template <typename T>
T ConvertWithBoundCheck(StringView str)
{
auto res = convertFromString<int64_t>(str);
if(res < std::numeric_limits<T>::lowest() || res > std::numeric_limits<T>::max())
{
throw RuntimeError(
StrCat("Value out of bound when converting [", str, "] to integer"));
}
return res;
}
} // namespace
template <>
int8_t convertFromString<int8_t>(StringView str)
{
return ConvertWithBoundCheck<int8_t>(str);
}
template <>
int16_t convertFromString<int16_t>(StringView str)
{
return ConvertWithBoundCheck<int16_t>(str);
}
template <>
int32_t convertFromString<int32_t>(StringView str)
{
return ConvertWithBoundCheck<int32_t>(str);
}
template <>
uint8_t convertFromString<uint8_t>(StringView str)
{
return ConvertWithBoundCheck<uint8_t>(str);
}
template <>
uint16_t convertFromString<uint16_t>(StringView str)
{
return ConvertWithBoundCheck<uint16_t>(str);
}
template <>
uint32_t convertFromString<uint32_t>(StringView str)
{
return ConvertWithBoundCheck<uint32_t>(str);
}
template <>
double convertFromString<double>(StringView str)
{
#if __cpp_lib_to_chars >= 201611L
// from_chars is locale-independent and thread-safe
double result = 0;
const auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
if(ec != std::errc())
{
throw RuntimeError(StrCat("Can't convert string [", str, "] to double"));
}
return result;
#else
// Fallback: stod is locale-dependent, so force "C" locale.
// See issue #120. Note: setlocale is not thread-safe.
const std::string old_locale = setlocale(LC_NUMERIC, nullptr);
std::ignore = setlocale(LC_NUMERIC, "C");
const std::string str_copy(str.data(), str.size());
const double val = std::stod(str_copy);
std::ignore = setlocale(LC_NUMERIC, old_locale.c_str());
return val;
#endif
}
template <>
float convertFromString<float>(StringView str)
{
#if __cpp_lib_to_chars >= 201611L
float result = 0;
const auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
if(ec != std::errc())
{
throw RuntimeError(StrCat("Can't convert string [", str, "] to float"));
}
return result;
#else
const std::string old_locale = setlocale(LC_NUMERIC, nullptr);
std::ignore = setlocale(LC_NUMERIC, "C");
const std::string str_copy(str.data(), str.size());
const double val = std::stod(str_copy);
std::ignore = setlocale(LC_NUMERIC, old_locale.c_str());
return static_cast<float>(val);
#endif
}
template <>
std::vector<int> convertFromString<std::vector<int>>(StringView str)
{
if(StartWith(str, "json:"))
{
str.remove_prefix(5);
return nlohmann::json::parse(str).get<std::vector<int>>();
}
auto parts = splitString(str, ';');
std::vector<int> output;
output.reserve(parts.size());
for(const StringView& part : parts)
{
output.push_back(convertFromString<int>(part));
}
return output;
}
template <>
std::vector<double> convertFromString<std::vector<double>>(StringView str)
{
if(StartWith(str, "json:"))
{
str.remove_prefix(5);
return nlohmann::json::parse(str).get<std::vector<double>>();
}
auto parts = splitString(str, ';');
std::vector<double> output;
output.reserve(parts.size());
for(const StringView& part : parts)
{
output.push_back(convertFromString<double>(part));
}
return output;
}
template <>
std::vector<bool> convertFromString<std::vector<bool>>(StringView str)
{
if(StartWith(str, "json:"))
{
str.remove_prefix(5);
return nlohmann::json::parse(str).get<std::vector<bool>>();
}
auto parts = splitString(str, ';');
std::vector<bool> output;
output.reserve(parts.size());
for(const StringView& part : parts)
{
output.push_back(convertFromString<bool>(part));
}
return output;
}
template <>
std::vector<std::string> convertFromString<std::vector<std::string>>(StringView str)
{
if(StartWith(str, "json:"))
{
str.remove_prefix(5);
return nlohmann::json::parse(str).get<std::vector<std::string>>();
}
auto parts = splitString(str, ';');
std::vector<std::string> output;
output.reserve(parts.size());
for(const StringView& part : parts)
{
output.push_back(convertFromString<std::string>(part));
}
return output;
}
template <>
bool convertFromString<bool>(StringView str)
{
if(str.size() == 1)
{
if(str[0] == '0')
{
return false;
}
if(str[0] == '1')
{
return true;
}
}
else if(str.size() == 4)
{
if(str == "true" || str == "TRUE" || str == "True")
{
return true;
}
}
else if(str.size() == 5)
{
if(str == "false" || str == "FALSE" || str == "False")
{
return false;
}
}
throw RuntimeError("convertFromString(): invalid bool conversion");
}
template <>
NodeStatus convertFromString<NodeStatus>(StringView str)
{
if(str == "IDLE")
return NodeStatus::IDLE;
if(str == "RUNNING")
return NodeStatus::RUNNING;
if(str == "SUCCESS")
return NodeStatus::SUCCESS;
if(str == "FAILURE")
return NodeStatus::FAILURE;
if(str == "SKIPPED")
return NodeStatus::SKIPPED;
throw RuntimeError(std::string("Cannot convert this to NodeStatus: ") +
static_cast<std::string>(str));
}
template <>
NodeType convertFromString<NodeType>(StringView str)
{
if(str == "Action")
return NodeType::ACTION;
if(str == "Condition")
return NodeType::CONDITION;
if(str == "Control")
return NodeType::CONTROL;
if(str == "Decorator")
return NodeType::DECORATOR;
if(str == "SubTree")
return NodeType::SUBTREE;
return NodeType::UNDEFINED;
}
template <>
PortDirection convertFromString<PortDirection>(StringView str)
{
if(str == "Input" || str == "INPUT")
return PortDirection::INPUT;
if(str == "Output" || str == "OUTPUT")
return PortDirection::OUTPUT;
if(str == "InOut" || str == "INOUT")
return PortDirection::INOUT;
throw RuntimeError(std::string("Cannot convert this to PortDirection: ") +
static_cast<std::string>(str));
}
std::ostream& operator<<(std::ostream& os, const NodeType& type)
{
os << toStr(type);
return os;
}
std::ostream& operator<<(std::ostream& os, const NodeStatus& status)
{
os << toStr(status);
return os;
}
std::ostream& operator<<(std::ostream& os, const PortDirection& type)
{
os << toStr(type);
return os;
}
std::vector<StringView> splitString(const StringView& strToSplit, char delimeter)
{
std::vector<StringView> splitted_strings;
splitted_strings.reserve(4);
size_t pos = 0;
while(pos < strToSplit.size())
{
size_t new_pos = strToSplit.find_first_of(delimeter, pos);
if(new_pos == std::string::npos)
{
new_pos = strToSplit.size();
}
const auto sv = StringView{ &strToSplit.data()[pos], new_pos - pos };
splitted_strings.push_back(sv);
pos = new_pos + 1;
}
return splitted_strings;
}
PortDirection PortInfo::direction() const
{
return direction_;
}
const std::type_index& TypeInfo::type() const
{
return type_info_;
}
const std::string& TypeInfo::typeName() const
{
return type_str_;
}
Any TypeInfo::parseString(const char* str) const
{
if(converter_)
{
return converter_(str);
}
return {};
}
Any TypeInfo::parseString(const std::string& str) const
{
if(converter_)
{
return converter_(str);
}
return {};
}
void PortInfo::setDescription(StringView description)
{
description_ = static_cast<std::string>(description);
}
const std::string& PortInfo::description() const
{
return description_;
}
const Any& PortInfo::defaultValue() const
{
return default_value_;
}
const std::string& PortInfo::defaultValueString() const
{
return default_value_str_;
}
bool IsAllowedPortName(StringView str)
{
if(str.empty())
{
return false;
}
const char first_char = str.data()[0];
// Port name cannot start with a digit
if(std::isalpha(static_cast<unsigned char>(first_char)) == 0)
{
return false;
}
// Check for forbidden characters
if(findForbiddenChar(str) != '\0')
{
return false;
}
return !IsReservedAttribute(str);
}
bool IsReservedAttribute(StringView str)
{
for(const auto& name : PreCondNames)
{
if(name == str)
{
return true;
}
}
for(const auto& name : PostCondNames)
{
if(name == str)
{
return true;
}
}
return str == "name" || str == "ID" || str == "_autoremap";
}
char findForbiddenChar(StringView name)
{
// Forbidden characters that break XML serialization or cause filesystem issues
static constexpr std::array<char, 16> kForbiddenChars = {
' ', '\t', '\n', '\r', '<', '>', '&', '"', '\'', '/', '\\', ':', '*', '?', '|', '.'
};
for(const char c : name)
{
const auto uc = static_cast<unsigned char>(c);
// Allow UTF-8 multibyte sequences (high bit set)
if(uc >= 0x80)
{
continue;
}
// Block control characters (ASCII 0-31 and 127)
if(uc < 32 || uc == 127)
{
return c;
}
// Check forbidden character list
if(std::find(kForbiddenChars.begin(), kForbiddenChars.end(), c) !=
kForbiddenChars.end())
{
return c;
}
}
return '\0';
}
Any convertFromJSON(StringView json_text, std::type_index type)
{
const nlohmann::json json = nlohmann::json::parse(json_text);
auto res = JsonExporter::get().fromJson(json, type);
if(!res)
{
throw std::runtime_error(res.error());
}
return res->first;
}
Expected<std::string> toJsonString(const Any& value)
{
nlohmann::json json;
if(JsonExporter::get().toJson(value, json))
{
return StrCat("json:", json.dump());
}
return nonstd::make_unexpected("toJsonString failed");
}
bool StartWith(StringView str, StringView prefix)
{
if(str.size() < prefix.size())
{
return false;
}
for(size_t i = 0; i < prefix.size(); ++i)
{
if(str[i] != prefix[i])
{
return false;
}
}
return true;
}
bool StartWith(StringView str, char prefix)
{
return str.size() >= 1 && str[0] == prefix;
}
} // namespace BT