Check for missing <format> header. (#8176)

This commit is contained in:
MarcelPiNacy-CVC 2024-09-20 12:07:51 +02:00 committed by GitHub
parent f8d95c2e8f
commit df514f94b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 39 additions and 10 deletions

View File

@ -12,7 +12,7 @@
#include <ctime>
#include <sstream>
#include <format>
#include <string_view>
#include <string>
#include <compiler/disable-ue4-macros.h>
@ -22,26 +22,53 @@
#include <Carla/Vehicle/CarlaWheeledVehicle.h>
#if __has_include(<format>)
#define HAS_FORMAT
#include <format>
#endif
template <typename V>
requires (
std::remove_reference_t<V>::Dim <= 3)
requires (
std::remove_reference_t<V>::Dim <= 3)
static std::string FormatVectorLike(V&& v)
{
char buffer[256];
constexpr auto Dim = std::remove_reference_t<V>::Dim;
if constexpr (Dim == 1)
{
const auto& [x] = v;
#ifdef HAS_FORMAT
return std::format("({})", x);
#else
auto n = snprintf(buffer, sizeof(buffer), "(%f)", (double)x);
if (n < 0)
return "<FORMAT-ERRROR>";
return std::string(buffer, n);
#endif
}
else if constexpr (Dim == 2)
{
const auto& [x, y] = v;
#ifdef HAS_FORMAT
return std::format("({}, {})", x, y);
#else
auto n = snprintf(buffer, sizeof(buffer), "(%f, %f)", (double)x, (double)y);
if (n < 0)
return "<FORMAT-ERRROR>";
return std::string(buffer, n);
#endif
}
else if constexpr (Dim == 3)
{
const auto& [x, y, z] = v;
#ifdef HAS_FORMAT
return std::format("({}, {}, {})", x, y, z);
#else
auto n = snprintf(buffer, sizeof(buffer), "(%f, %f, %f)", (double)x, (double)y, (double)z);
if (n < 0)
return "<FORMAT-ERRROR>";
return std::string(buffer, n);
#endif
}
}
@ -49,10 +76,12 @@ template <typename V>
static std::string FormatCurveLike(V&& v)
{
std::string r;
r.reserve(4096);
r += "[";
for (auto& [x, y] : v)
r += std::format("({}, {}),", x, y);
for (auto& point : v)
{
r += FormatVectorLike(point);
r += ',';
}
r += "]";
return r;
}