mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +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:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#include <wpi/concepts.h>
|
||||
|
||||
#include "../MockLogger.h"
|
||||
#include "../PubSubOptionsMatcher.h"
|
||||
#include "../SpanMatcher.h"
|
||||
@@ -99,12 +101,12 @@ static std::vector<uint8_t> EncodeServerBinary(const T& msgs) {
|
||||
std::vector<uint8_t> data;
|
||||
wpi::raw_uvector_ostream os{data};
|
||||
for (auto&& msg : msgs) {
|
||||
if constexpr (std::is_same_v<typename T::value_type, net::ServerMessage>) {
|
||||
if constexpr (std::same_as<typename T::value_type, net::ServerMessage>) {
|
||||
if (auto m = std::get_if<net::ServerValueMsg>(&msg.contents)) {
|
||||
net::WireEncodeBinary(os, m->topic, m->value.time(), m->value);
|
||||
}
|
||||
} else if constexpr (std::is_same_v<typename T::value_type,
|
||||
net::ClientMessage>) {
|
||||
} else if constexpr (std::same_as<typename T::value_type,
|
||||
net::ClientMessage>) {
|
||||
if (auto m = std::get_if<net::ClientValueMsg>(&msg.contents)) {
|
||||
net::WireEncodeBinary(os, Handle{m->pubHandle}.GetIndex(),
|
||||
m->value.time(), m->value);
|
||||
|
||||
Reference in New Issue
Block a user