mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-03 03:01:44 +00:00
Replace SFINAE with concepts (#5361)
Concepts are cleaner to use and result in much better error messages for incorrect template use.
This commit is contained in:
@@ -4,17 +4,16 @@
|
||||
|
||||
#include "glass/Storage.h"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include <imgui.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
#include <wpi/concepts.h>
|
||||
#include <wpi/json.h>
|
||||
|
||||
using namespace glass;
|
||||
|
||||
template <typename To>
|
||||
bool ConvertFromString(To* out, std::string_view str) {
|
||||
if constexpr (std::is_same_v<To, bool>) {
|
||||
if constexpr (std::same_as<To, bool>) {
|
||||
if (str == "true") {
|
||||
*out = true;
|
||||
} else if (str == "false") {
|
||||
@@ -24,7 +23,7 @@ bool ConvertFromString(To* out, std::string_view str) {
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else if constexpr (std::is_floating_point_v<To>) {
|
||||
} else if constexpr (std::floating_point<To>) {
|
||||
if (auto val = wpi::parse_float<To>(str)) {
|
||||
*out = val.value();
|
||||
} else {
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/SpanExtras.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
#include <wpi/concepts.h>
|
||||
#include <wpi/mpack.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
@@ -82,9 +83,8 @@ static std::string IntegerArrayToString(std::span<const int64_t> in) {
|
||||
return fmt::format("[{:d}]", fmt::join(in, ","));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <std::floating_point T>
|
||||
static std::string FloatArrayToString(std::span<const T> in) {
|
||||
static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>);
|
||||
return fmt::format("[{:.6f}]", fmt::join(in, ","));
|
||||
}
|
||||
|
||||
@@ -729,9 +729,8 @@ static bool StringToIntegerArray(std::string_view in,
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <std::floating_point T>
|
||||
static bool StringToFloatArray(std::string_view in, std::vector<T>* out) {
|
||||
static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>);
|
||||
in = wpi::trim(in);
|
||||
if (in.empty()) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user