switch_node.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2019-2022 Davide Faconti, Eurecat - All Rights Reserved
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
10 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
14 
15 #if __has_include(<charconv>)
16 #include <charconv>
17 #endif
18 
19 namespace BT::details
20 {
21 
22 bool CheckStringEquality(const std::string& v1, const std::string& v2,
23  const ScriptingEnumsRegistry* enums)
24 {
25  // compare strings first
26  if(v1 == v2)
27  {
28  return true;
29  }
30  // compare as integers next
31  auto ToInt = [enums](const std::string& str, auto& result) -> bool {
32  if(enums)
33  {
34  auto it = enums->find(str);
35  if(it != enums->end())
36  {
37  result = it->second;
38  return true;
39  }
40  }
41 #if __cpp_lib_to_chars >= 201611L
42  auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
43  return (ec == std::errc());
44 #else
45  try
46  {
47  result = std::stoi(str);
48  return true;
49  }
50  catch(...)
51  {
52  return false;
53  }
54 #endif
55  };
56  int v1_int = 0;
57  int v2_int = 0;
58  if(ToInt(v1, v1_int) && ToInt(v2, v2_int) && v1_int == v2_int)
59  {
60  return true;
61  }
62  // compare as real numbers next
63  auto ToReal = [](const std::string& str, auto& result) -> bool {
64 #if __cpp_lib_to_chars >= 201611L
65  auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
66  return (ec == std::errc());
67 #else
68  try
69  {
70  result = std::stod(str);
71  return true;
72  }
73  catch(...)
74  {
75  return false;
76  }
77 #endif
78  };
79  double v1_real = 0;
80  double v2_real = 0;
81  constexpr auto eps = double(std::numeric_limits<float>::epsilon());
82  if(ToReal(v1, v1_real) && ToReal(v2, v2_real) && std::abs(v1_real - v2_real) <= eps)
83  {
84  return true;
85  }
86  return false;
87 }
88 
89 } // namespace BT::details
BT::details::CheckStringEquality
bool CheckStringEquality(const std::string &v1, const std::string &v2, const ScriptingEnumsRegistry *enums)
Definition: switch_node.cpp:22
BT::ScriptingEnumsRegistry
std::unordered_map< std::string, int > ScriptingEnumsRegistry
Definition: tree_node.h:71
BT::details
The SwitchNode is equivalent to a switch statement, where a certain branch (child) is executed accord...
Definition: basic_types.h:500
switch_node.h


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:08