Skip to content

Commit eb7f798

Browse files
committed
core: add fallback for when std::to_chars is not available
1 parent a98fc0b commit eb7f798

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/mavsdk/core/libmav_receiver.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,43 @@
1010
#include <cmath>
1111
#include "log.h"
1212

13+
#ifdef __APPLE__
14+
#include <TargetConditionals.h>
15+
#endif
16+
17+
// Check if std::to_chars supports floating-point types
18+
// - GCC 11+ (libstdc++) - use __GNUC__ version, not __GLIBCXX__ date
19+
// - MSVC 2019 16.4+
20+
// - macOS with libc++ 14+
21+
// - iOS: disabled because Apple marks it unavailable for iOS < 16.3
22+
#if defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_WATCH)
23+
// Disable on iOS/tvOS/watchOS - function is marked unavailable for older deployment targets
24+
#define MAVSDK_HAS_FLOAT_TO_CHARS 0
25+
#elif defined(__GNUC__) && !defined(__clang__)
26+
// GCC: floating-point to_chars was added in GCC 11
27+
#if __GNUC__ >= 11
28+
#define MAVSDK_HAS_FLOAT_TO_CHARS 1
29+
#else
30+
#define MAVSDK_HAS_FLOAT_TO_CHARS 0
31+
#endif
32+
#elif defined(_LIBCPP_VERSION)
33+
// libc++ has had floating-point to_chars since version 14 (macOS 13.3+)
34+
#if _LIBCPP_VERSION >= 14000
35+
#define MAVSDK_HAS_FLOAT_TO_CHARS 1
36+
#else
37+
#define MAVSDK_HAS_FLOAT_TO_CHARS 0
38+
#endif
39+
#elif defined(_MSC_VER)
40+
// MSVC has had it since VS 2019 16.4
41+
#if _MSC_VER >= 1924
42+
#define MAVSDK_HAS_FLOAT_TO_CHARS 1
43+
#else
44+
#define MAVSDK_HAS_FLOAT_TO_CHARS 0
45+
#endif
46+
#else
47+
#define MAVSDK_HAS_FLOAT_TO_CHARS 0
48+
#endif
49+
1350
namespace mavsdk {
1451

1552
// Original implementation using std::ostream << for float/double conversion.
@@ -97,9 +134,14 @@ template<typename T> void value_to_json_stream_fast(std::ostream& json_stream, c
97134
if (!std::isfinite(value)) {
98135
json_stream << "null";
99136
} else {
137+
#if MAVSDK_HAS_FLOAT_TO_CHARS
100138
char buf[32];
101139
auto [ptr, ec] = std::to_chars(buf, buf + sizeof(buf), value);
102140
json_stream.write(buf, ptr - buf);
141+
#else
142+
// Fallback for older compilers without floating-point to_chars
143+
json_stream << value;
144+
#endif
103145
}
104146
} else if constexpr (std::is_same_v<T, std::string>) {
105147
json_stream << "\"" << value << "\"";
@@ -142,9 +184,14 @@ template<typename T> void value_to_json_stream_fast(std::ostream& json_stream, c
142184
if (!std::isfinite(elem)) {
143185
json_stream << "null";
144186
} else {
187+
#if MAVSDK_HAS_FLOAT_TO_CHARS
145188
char buf[32];
146189
auto [ptr, ec] = std::to_chars(buf, buf + sizeof(buf), elem);
147190
json_stream.write(buf, ptr - buf);
191+
#else
192+
// Fallback for older compilers without floating-point to_chars
193+
json_stream << elem;
194+
#endif
148195
}
149196
}
150197
json_stream << "]";

0 commit comments

Comments
 (0)