MCPcopy Create free account
hub / github.com/BehaviorTree/BehaviorTree.CPP / ValidCast

Function ValidCast

include/behaviortree_cpp/utils/safe_any.hpp:243–273  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

241
242template <typename SRC, typename TO>
243inline bool ValidCast(const SRC& val)
244{
245 // First check numeric limits
246 if constexpr(std::is_arithmetic_v<SRC> && std::is_arithmetic_v<TO>)
247 {
248 // Handle conversion to floating point
249 if constexpr(std::is_floating_point_v<TO>)
250 {
251 if constexpr(std::is_integral_v<SRC>)
252 {
253 // For integral to float, check if we can represent the value exactly
254 TO as_float = static_cast<TO>(val);
255 SRC back_conv = static_cast<SRC>(as_float);
256 return back_conv == val;
257 }
258 }
259 // Handle conversion to integral
260 else if constexpr(std::is_integral_v<TO>)
261 {
262 if(val > static_cast<SRC>(std::numeric_limits<TO>::max()) ||
263 val < static_cast<SRC>(std::numeric_limits<TO>::lowest()))
264 {
265 return false;
266 }
267 }
268 }
269
270 TO as_target = static_cast<TO>(val);
271 SRC back_to_source = static_cast<SRC>(as_target);
272 return val == back_to_source;
273}
274
275template <typename T>
276inline bool isCastingSafe(const std::type_index& type, const T& val)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected