mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Use std::visit with Visitor helper instead of std::holds_alternatve
This commit is contained in:
@@ -21,14 +21,6 @@
|
||||
|
||||
#include "gfx/main.hpp"
|
||||
|
||||
// Convenience feature for visiting the below.
|
||||
template<typename... Ts>
|
||||
struct Visitor : Ts... {
|
||||
using Ts::operator()...;
|
||||
};
|
||||
template<typename... Ts>
|
||||
Visitor(Ts...) -> Visitor<Ts...>;
|
||||
|
||||
class File {
|
||||
// Construct a `std::streambuf *` by default, since it's probably lighter than a `filebuf`.
|
||||
std::variant<std::streambuf *, std::filebuf> _file;
|
||||
|
||||
@@ -85,4 +85,12 @@
|
||||
// For lack of <ranges>, this adds some more brevity
|
||||
#define RANGE(s) std::begin(s), std::end(s)
|
||||
|
||||
// Convenience feature for visiting variants.
|
||||
template<typename... Ts>
|
||||
struct Visitor : Ts... {
|
||||
using Ts::operator()...;
|
||||
};
|
||||
template<typename... Ts>
|
||||
Visitor(Ts...) -> Visitor<Ts...>;
|
||||
|
||||
#endif // HELPERS_H
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
|
||||
#include "extern/utf8decoder.hpp"
|
||||
|
||||
#include "helpers.hpp"
|
||||
#include "linkdefs.hpp"
|
||||
#include "platform.hpp" // strncasecmp, strdup
|
||||
|
||||
@@ -324,10 +325,10 @@ static void strfmt(char *dest, size_t destLen, char const *fmt,
|
||||
std::variant<uint32_t, char *> &arg = args[a++];
|
||||
static char buf[MAXSTRLEN + 1];
|
||||
|
||||
if (std::holds_alternative<uint32_t>(arg))
|
||||
fmt_PrintNumber(buf, sizeof(buf), &spec, std::get<uint32_t>(arg));
|
||||
else
|
||||
fmt_PrintString(buf, sizeof(buf), &spec, std::get<char *>(arg));
|
||||
std::visit(Visitor{
|
||||
[&](uint32_t num) { fmt_PrintNumber(buf, sizeof(buf), &spec, num); },
|
||||
[&](char *str) { fmt_PrintString(buf, sizeof(buf), &spec, str); },
|
||||
}, arg);
|
||||
|
||||
i += snprintf(&dest[i], destLen - i, "%s", buf);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user