Skip to content

Commit a6d6588

Browse files
committed
add more convertToString for integers
1 parent 6501206 commit a6d6588

File tree

3 files changed

+61
-39
lines changed

3 files changed

+61
-39
lines changed

include/behaviortree_cpp/basic_types.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,28 @@ template <>
144144
[[nodiscard]] const char* convertFromString<const char*>(StringView str);
145145

146146
template <>
147-
[[nodiscard]] int convertFromString<int>(StringView str);
147+
[[nodiscard]] int8_t convertFromString<int8_t>(StringView str);
148148

149149
template <>
150-
[[nodiscard]] unsigned convertFromString<unsigned>(StringView str);
150+
[[nodiscard]] int16_t convertFromString<int16_t>(StringView str);
151151

152152
template <>
153-
[[nodiscard]] long convertFromString<long>(StringView str);
153+
[[nodiscard]] int32_t convertFromString<int32_t>(StringView str);
154154

155155
template <>
156-
[[nodiscard]] unsigned long convertFromString<unsigned long>(StringView str);
156+
[[nodiscard]] int64_t convertFromString<int64_t>(StringView str);
157+
158+
template <>
159+
[[nodiscard]] uint8_t convertFromString<uint8_t>(StringView str);
160+
161+
template <>
162+
[[nodiscard]] uint16_t convertFromString<uint16_t>(StringView str);
163+
164+
template <>
165+
[[nodiscard]] uint32_t convertFromString<uint32_t>(StringView str);
166+
167+
template <>
168+
[[nodiscard]] uint64_t convertFromString<uint64_t>(StringView str);
157169

158170
template <>
159171
[[nodiscard]] float convertFromString<float>(StringView str);

src/basic_types.cpp

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -116,51 +116,75 @@ std::string convertFromString<std::string>(StringView str)
116116
}
117117

118118
template <>
119-
int convertFromString<int>(StringView str)
119+
int64_t convertFromString<int64_t>(StringView str)
120120
{
121-
int result = 0;
121+
long result = 0;
122122
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
123123
if(ec != std::errc())
124124
{
125-
throw RuntimeError(StrCat("Can't convert string [", str, "] to int"));
125+
throw RuntimeError(StrCat("Can't convert string [", str, "] to integer"));
126126
}
127127
return result;
128128
}
129129

130130
template <>
131-
long convertFromString<long>(StringView str)
131+
uint64_t convertFromString<unsigned long>(StringView str)
132132
{
133-
long result = 0;
133+
unsigned long result = 0;
134134
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
135135
if(ec != std::errc())
136136
{
137-
throw RuntimeError(StrCat("Can't convert string [", str, "] to long"));
137+
throw RuntimeError(StrCat("Can't convert string [", str, "] to integer"));
138138
}
139139
return result;
140140
}
141141

142-
template <>
143-
unsigned convertFromString<unsigned>(StringView str)
142+
template <typename T>
143+
T ConvertWithBoundCheck(StringView str)
144144
{
145-
unsigned result = 0;
146-
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
147-
if(ec != std::errc())
145+
auto res = convertFromString<int64_t>(str);
146+
if(res < std::numeric_limits<T>::lowest() || res > std::numeric_limits<T>::max())
148147
{
149-
throw RuntimeError(StrCat("Can't convert string [", str, "] to unsigned"));
148+
throw RuntimeError(
149+
StrCat("Value out of bound when converting [", str, "] to integer"));
150150
}
151-
return result;
151+
return res;
152152
}
153153

154154
template <>
155-
unsigned long convertFromString<unsigned long>(StringView str)
155+
int8_t convertFromString<int8_t>(StringView str)
156156
{
157-
unsigned long result = 0;
158-
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
159-
if(ec != std::errc())
160-
{
161-
throw RuntimeError(StrCat("Can't convert string [", str, "] to unsigned long"));
162-
}
163-
return result;
157+
return ConvertWithBoundCheck<int8_t>(str);
158+
}
159+
160+
template <>
161+
int16_t convertFromString<int16_t>(StringView str)
162+
{
163+
return ConvertWithBoundCheck<int16_t>(str);
164+
}
165+
166+
template <>
167+
int32_t convertFromString<int32_t>(StringView str)
168+
{
169+
return ConvertWithBoundCheck<int32_t>(str);
170+
}
171+
172+
template <>
173+
uint8_t convertFromString<uint8_t>(StringView str)
174+
{
175+
return ConvertWithBoundCheck<uint8_t>(str);
176+
}
177+
178+
template <>
179+
uint16_t convertFromString<uint16_t>(StringView str)
180+
{
181+
return ConvertWithBoundCheck<uint16_t>(str);
182+
}
183+
184+
template <>
185+
uint32_t convertFromString<uint32_t>(StringView str)
186+
{
187+
return ConvertWithBoundCheck<uint32_t>(str);
164188
}
165189

166190
template <>

src/bt_factory.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -674,20 +674,6 @@ NodeStatus Tree::tickRoot(TickOption opt, std::chrono::milliseconds sleep_time)
674674
return status;
675675
}
676676

677-
// void BlackboardClone(const Blackboard& src, Blackboard& dst)
678-
// {
679-
// dst.clear();
680-
// for(auto const key_name : src.getKeys())
681-
// {
682-
// const auto key = std::string(key_name);
683-
// const auto entry = src.getEntry(key);
684-
// dst.createEntry(key, entry->info);
685-
// auto new_entry = dst.getEntry(key);
686-
// new_entry->value = entry->value;
687-
// new_entry->string_converter = entry->string_converter;
688-
// }
689-
// }
690-
691677
void BlackboardRestore(const std::vector<Blackboard::Ptr>& backup, Tree& tree)
692678
{
693679
assert(backup.size() == tree.subtrees.size());

0 commit comments

Comments
 (0)