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:
Tyler Veness
2023-06-07 09:50:09 -07:00
committed by GitHub
parent d57d1a4598
commit 91cbcea841
42 changed files with 397 additions and 361 deletions

View File

@@ -9,6 +9,7 @@
#include <fmt/format.h>
#include <wpi/Logger.h>
#include <wpi/SpanExtras.h>
#include <wpi/concepts.h>
#include <wpi/json.h>
#include <wpi/mpack.h>
@@ -104,12 +105,10 @@ static bool ObjGetStringArray(wpi::json::object_t& obj, std::string_view key,
#endif
template <typename T>
requires(std::same_as<T, ClientMessageHandler> ||
std::same_as<T, ServerMessageHandler>)
static void WireDecodeTextImpl(std::string_view in, T& out,
wpi::Logger& logger) {
static_assert(std::is_same_v<T, ClientMessageHandler> ||
std::is_same_v<T, ServerMessageHandler>,
"T must be ClientMessageHandler or ServerMessageHandler");
wpi::json j;
try {
j = wpi::json::parse(in);
@@ -150,7 +149,7 @@ static void WireDecodeTextImpl(std::string_view in, T& out,
goto err;
}
if constexpr (std::is_same_v<T, ClientMessageHandler>) {
if constexpr (std::same_as<T, ClientMessageHandler>) {
if (*method == PublishMsg::kMethodStr) {
// name
auto name = ObjGetString(*params, "name", &error);
@@ -302,7 +301,7 @@ static void WireDecodeTextImpl(std::string_view in, T& out,
error = fmt::format("unrecognized method '{}'", *method);
goto err;
}
} else if constexpr (std::is_same_v<T, ServerMessageHandler>) {
} else if constexpr (std::same_as<T, ServerMessageHandler>) {
if (*method == AnnounceMsg::kMethodStr) {
// name
auto name = ObjGetString(*params, "name", &error);

View File

@@ -12,10 +12,11 @@
#include <span>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
#include <wpi/concepts.h>
#include "ntcore_c.h"
namespace nt {
@@ -377,11 +378,10 @@ class Value final {
* time)
* @return The entry value
*/
template <typename T,
typename std::enable_if<std::is_same<T, std::string>::value>::type>
template <std::same_as<std::string> T>
static Value MakeString(T&& value, int64_t time = 0) {
Value val{NT_STRING, time, private_init{}};
auto data = std::make_shared<std::string>(std::forward(value));
auto data = std::make_shared<std::string>(std::forward<T>(value));
val.m_val.data.v_string.str = const_cast<char*>(data->c_str());
val.m_val.data.v_string.len = data->size();
val.m_storage = std::move(data);
@@ -414,11 +414,10 @@ class Value final {
* time)
* @return The entry value
*/
template <typename T, typename std::enable_if<
std::is_same<T, std::vector<uint8_t>>::value>::type>
template <std::same_as<std::vector<uint8_t>> T>
static Value MakeRaw(T&& value, int64_t time = 0) {
Value val{NT_RAW, time, private_init{}};
auto data = std::make_shared<std::vector<uint8_t>>(std::forward(value));
auto data = std::make_shared<std::vector<uint8_t>>(std::forward<T>(value));
val.m_val.data.v_raw.data = const_cast<uint8_t*>(data->data());
val.m_val.data.v_raw.size = data->size();
val.m_storage = std::move(data);