|
10 | 10 | #include <cmath> |
11 | 11 | #include "log.h" |
12 | 12 |
|
| 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 | + |
13 | 50 | namespace mavsdk { |
14 | 51 |
|
15 | 52 | // 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 |
97 | 134 | if (!std::isfinite(value)) { |
98 | 135 | json_stream << "null"; |
99 | 136 | } else { |
| 137 | +#if MAVSDK_HAS_FLOAT_TO_CHARS |
100 | 138 | char buf[32]; |
101 | 139 | auto [ptr, ec] = std::to_chars(buf, buf + sizeof(buf), value); |
102 | 140 | json_stream.write(buf, ptr - buf); |
| 141 | +#else |
| 142 | + // Fallback for older compilers without floating-point to_chars |
| 143 | + json_stream << value; |
| 144 | +#endif |
103 | 145 | } |
104 | 146 | } else if constexpr (std::is_same_v<T, std::string>) { |
105 | 147 | json_stream << "\"" << value << "\""; |
@@ -142,9 +184,14 @@ template<typename T> void value_to_json_stream_fast(std::ostream& json_stream, c |
142 | 184 | if (!std::isfinite(elem)) { |
143 | 185 | json_stream << "null"; |
144 | 186 | } else { |
| 187 | +#if MAVSDK_HAS_FLOAT_TO_CHARS |
145 | 188 | char buf[32]; |
146 | 189 | auto [ptr, ec] = std::to_chars(buf, buf + sizeof(buf), elem); |
147 | 190 | json_stream.write(buf, ptr - buf); |
| 191 | +#else |
| 192 | + // Fallback for older compilers without floating-point to_chars |
| 193 | + json_stream << elem; |
| 194 | +#endif |
148 | 195 | } |
149 | 196 | } |
150 | 197 | json_stream << "]"; |
|
0 commit comments