Use wpi::span instead of wpi::ArrayRef across all libraries (#3414)

- Remove ArrayRef.h
- Add SpanExtras.h for a couple of convenience functions
This commit is contained in:
Peter Johnson
2021-06-06 19:51:14 -07:00
committed by GitHub
parent 2abbbd9e70
commit 64f5413253
167 changed files with 974 additions and 1433 deletions

View File

@@ -7,6 +7,7 @@
#include <algorithm>
#include <iterator>
#include <wpi/SmallVector.h>
#include <wpi/StringExtras.h>
#include <wpi/TCPAcceptor.h>
#include <wpi/TCPConnector.h>
@@ -37,7 +38,7 @@ void Dispatcher::SetServer(const char* server_name, unsigned int port) {
}
void Dispatcher::SetServer(
wpi::ArrayRef<std::pair<std::string_view, unsigned int>> servers) {
wpi::span<const std::pair<std::string_view, unsigned int>> servers) {
wpi::SmallVector<std::pair<std::string, int>, 16> servers_copy;
for (const auto& server : servers) {
servers_copy.emplace_back(std::string{wpi::trim(server.first)},
@@ -512,7 +513,7 @@ void DispatcherBase::ClientThreadMain() {
bool DispatcherBase::ClientHandshake(
NetworkConnection& conn, std::function<std::shared_ptr<Message>()> get_msg,
std::function<void(wpi::ArrayRef<std::shared_ptr<Message>>)> send_msgs) {
std::function<void(wpi::span<std::shared_ptr<Message>>)> send_msgs) {
// get identity
std::string self_id;
{
@@ -522,10 +523,11 @@ bool DispatcherBase::ClientHandshake(
// send client hello
DEBUG0("{}", "client: sending hello");
send_msgs(Message::ClientHello(self_id));
auto msg = Message::ClientHello(self_id);
send_msgs(wpi::span(&msg, 1));
// wait for response
auto msg = get_msg();
msg = get_msg();
if (!msg) {
// disconnected, retry
DEBUG0("{}", "client: server disconnected before first response");
@@ -604,7 +606,7 @@ bool DispatcherBase::ClientHandshake(
bool DispatcherBase::ServerHandshake(
NetworkConnection& conn, std::function<std::shared_ptr<Message>()> get_msg,
std::function<void(wpi::ArrayRef<std::shared_ptr<Message>>)> send_msgs) {
std::function<void(wpi::span<std::shared_ptr<Message>>)> send_msgs) {
// Wait for the client to send us a hello.
auto msg = get_msg();
if (!msg) {
@@ -620,7 +622,8 @@ bool DispatcherBase::ServerHandshake(
unsigned int proto_rev = msg->id();
if (proto_rev > 0x0300) {
DEBUG0("{}", "server: client requested proto > 0x0300");
send_msgs(Message::ProtoUnsup());
auto toSend = Message::ProtoUnsup();
send_msgs(wpi::span(&toSend, 1));
return false;
}

View File

@@ -16,6 +16,7 @@
#include <wpi/condition_variable.h>
#include <wpi/mutex.h>
#include <wpi/span.h>
#include "IDispatcher.h"
#include "INetworkConnection.h"
@@ -77,11 +78,11 @@ class DispatcherBase : public IDispatcher {
bool ClientHandshake(
NetworkConnection& conn,
std::function<std::shared_ptr<Message>()> get_msg,
std::function<void(wpi::ArrayRef<std::shared_ptr<Message>>)> send_msgs);
std::function<void(wpi::span<std::shared_ptr<Message>>)> send_msgs);
bool ServerHandshake(
NetworkConnection& conn,
std::function<std::shared_ptr<Message>()> get_msg,
std::function<void(wpi::ArrayRef<std::shared_ptr<Message>>)> send_msgs);
std::function<void(wpi::span<std::shared_ptr<Message>>)> send_msgs);
void ClientReconnect(unsigned int proto_rev = 0x0300);
@@ -136,7 +137,7 @@ class Dispatcher : public DispatcherBase {
void SetServer(const char* server_name, unsigned int port);
void SetServer(
wpi::ArrayRef<std::pair<std::string_view, unsigned int>> servers);
wpi::span<const std::pair<std::string_view, unsigned int>> servers);
void SetServerTeam(unsigned int team, unsigned int port);
void SetServerOverride(const char* server_name, unsigned int port);

View File

@@ -10,7 +10,7 @@
#include <string_view>
#include <vector>
#include <wpi/ArrayRef.h>
#include <wpi/span.h>
#include "Message.h"
#include "ntcore_cpp.h"
@@ -45,7 +45,7 @@ class IStorage {
INetworkConnection& conn,
std::vector<std::shared_ptr<Message>>* msgs) = 0;
virtual void ApplyInitialAssignments(
INetworkConnection& conn, wpi::ArrayRef<std::shared_ptr<Message>> msgs,
INetworkConnection& conn, wpi::span<std::shared_ptr<Message>> msgs,
bool new_server, std::vector<std::shared_ptr<Message>>* out_msgs) = 0;
// Filename-based save/load functions. Used both by periodic saves and

View File

@@ -162,8 +162,9 @@ void NetworkConnection::ReadThreadMain() {
}
return msg;
},
[&](wpi::ArrayRef<std::shared_ptr<Message>> msgs) {
m_outgoing.emplace(msgs);
[&](auto msgs) {
m_outgoing.emplace(std::vector<std::shared_ptr<Message>>(
msgs.begin(), msgs.end()));
})) {
set_state(kDead);
m_active = false;

View File

@@ -19,6 +19,7 @@
#include <wpi/ConcurrentQueue.h>
#include <wpi/condition_variable.h>
#include <wpi/mutex.h>
#include <wpi/span.h>
#include "INetworkConnection.h"
#include "Message.h"
@@ -38,7 +39,7 @@ class NetworkConnection : public INetworkConnection {
typedef std::function<bool(
NetworkConnection& conn,
std::function<std::shared_ptr<Message>()> get_msg,
std::function<void(wpi::ArrayRef<std::shared_ptr<Message>>)> send_msgs)>
std::function<void(wpi::span<std::shared_ptr<Message>>)> send_msgs)>
HandshakeFunc;
using ProcessIncomingFunc =
std::function<void(std::shared_ptr<Message>, NetworkConnection*)>;

View File

@@ -410,7 +410,7 @@ void Storage::GetInitialAssignments(
}
void Storage::ApplyInitialAssignments(
INetworkConnection& conn, wpi::ArrayRef<std::shared_ptr<Message>> msgs,
INetworkConnection& conn, wpi::span<std::shared_ptr<Message>> msgs,
bool /*new_server*/, std::vector<std::shared_ptr<Message>>* out_msgs) {
std::unique_lock lock(m_mutex);
if (m_server) {

View File

@@ -21,6 +21,7 @@
#include <wpi/StringMap.h>
#include <wpi/condition_variable.h>
#include <wpi/mutex.h>
#include <wpi/span.h>
#include "IStorage.h"
#include "Message.h"
@@ -67,7 +68,7 @@ class Storage : public IStorage {
INetworkConnection& conn,
std::vector<std::shared_ptr<Message>>* msgs) override;
void ApplyInitialAssignments(
INetworkConnection& conn, wpi::ArrayRef<std::shared_ptr<Message>> msgs,
INetworkConnection& conn, wpi::span<std::shared_ptr<Message>> msgs,
bool new_server,
std::vector<std::shared_ptr<Message>>* out_msgs) override;

View File

@@ -25,12 +25,12 @@ class SavePersistentImpl {
explicit SavePersistentImpl(wpi::raw_ostream& os) : m_os(os) {}
void Save(wpi::ArrayRef<Entry> entries);
void Save(wpi::span<const Entry> entries);
private:
void WriteString(std::string_view str);
void WriteHeader();
void WriteEntries(wpi::ArrayRef<Entry> entries);
void WriteEntries(wpi::span<const Entry> entries);
void WriteEntry(std::string_view name, const Value& value);
bool WriteType(NT_Type type);
void WriteValue(const Value& value);
@@ -72,7 +72,7 @@ void SavePersistentImpl::WriteString(std::string_view str) {
m_os << '"';
}
void SavePersistentImpl::Save(wpi::ArrayRef<Entry> entries) {
void SavePersistentImpl::Save(wpi::span<const Entry> entries) {
WriteHeader();
WriteEntries(entries);
}
@@ -81,7 +81,7 @@ void SavePersistentImpl::WriteHeader() {
m_os << "[NetworkTables Storage 3.0]\n";
}
void SavePersistentImpl::WriteEntries(wpi::ArrayRef<Entry> entries) {
void SavePersistentImpl::WriteEntries(wpi::span<const Entry> entries) {
for (auto& i : entries) {
if (!i.second) {
continue;

View File

@@ -4,6 +4,8 @@
#include <stdint.h>
#include <cstring>
#include <wpi/MemAlloc.h>
#include <wpi/timestamp.h>
@@ -43,7 +45,7 @@ Value::~Value() {
}
}
std::shared_ptr<Value> Value::MakeBooleanArray(wpi::ArrayRef<bool> value,
std::shared_ptr<Value> Value::MakeBooleanArray(wpi::span<const bool> value,
uint64_t time) {
auto val = std::make_shared<Value>(NT_BOOLEAN_ARRAY, time, private_init());
val->m_val.data.arr_boolean.arr = new int[value.size()];
@@ -52,7 +54,7 @@ std::shared_ptr<Value> Value::MakeBooleanArray(wpi::ArrayRef<bool> value,
return val;
}
std::shared_ptr<Value> Value::MakeBooleanArray(wpi::ArrayRef<int> value,
std::shared_ptr<Value> Value::MakeBooleanArray(wpi::span<const int> value,
uint64_t time) {
auto val = std::make_shared<Value>(NT_BOOLEAN_ARRAY, time, private_init());
val->m_val.data.arr_boolean.arr = new int[value.size()];
@@ -61,7 +63,7 @@ std::shared_ptr<Value> Value::MakeBooleanArray(wpi::ArrayRef<int> value,
return val;
}
std::shared_ptr<Value> Value::MakeDoubleArray(wpi::ArrayRef<double> value,
std::shared_ptr<Value> Value::MakeDoubleArray(wpi::span<const double> value,
uint64_t time) {
auto val = std::make_shared<Value>(NT_DOUBLE_ARRAY, time, private_init());
val->m_val.data.arr_double.arr = new double[value.size()];
@@ -70,10 +72,10 @@ std::shared_ptr<Value> Value::MakeDoubleArray(wpi::ArrayRef<double> value,
return val;
}
std::shared_ptr<Value> Value::MakeStringArray(wpi::ArrayRef<std::string> value,
uint64_t time) {
std::shared_ptr<Value> Value::MakeStringArray(
wpi::span<const std::string> value, uint64_t time) {
auto val = std::make_shared<Value>(NT_STRING_ARRAY, time, private_init());
val->m_string_array = value;
val->m_string_array.assign(value.begin(), value.end());
// point NT_Value to the contents in the vector.
val->m_val.data.arr_string.arr = new NT_String[value.size()];
val->m_val.data.arr_string.size = val->m_string_array.size();
@@ -175,11 +177,11 @@ std::shared_ptr<Value> nt::ConvertFromC(const NT_Value& value) {
case NT_RPC:
return Value::MakeRpc(ConvertFromC(value.data.v_raw));
case NT_BOOLEAN_ARRAY:
return Value::MakeBooleanArray(wpi::ArrayRef<int>(
value.data.arr_boolean.arr, value.data.arr_boolean.size));
return Value::MakeBooleanArray(
wpi::span(value.data.arr_boolean.arr, value.data.arr_boolean.size));
case NT_DOUBLE_ARRAY:
return Value::MakeDoubleArray(wpi::ArrayRef<double>(
value.data.arr_double.arr, value.data.arr_double.size));
return Value::MakeDoubleArray(
wpi::span(value.data.arr_double.arr, value.data.arr_double.size));
case NT_STRING_ARRAY: {
std::vector<std::string> v;
v.reserve(value.data.arr_string.size);

View File

@@ -139,7 +139,7 @@ std::shared_ptr<nt::Value> FromJavaBooleanArray(JNIEnv* env, jbooleanArray jarr,
if (!ref) {
return nullptr;
}
wpi::ArrayRef<jboolean> elements{ref};
wpi::span<const jboolean> elements{ref};
size_t len = elements.size();
std::vector<int> arr;
arr.reserve(len);
@@ -306,8 +306,9 @@ static jobject MakeJObject(JNIEnv* env, jobject inst,
static_cast<jint>(answer.call), name.obj(), params.obj(), conn.obj());
}
static jobjectArray MakeJObject(JNIEnv* env, jobject inst,
wpi::ArrayRef<nt::ConnectionNotification> arr) {
static jobjectArray MakeJObject(
JNIEnv* env, jobject inst,
wpi::span<const nt::ConnectionNotification> arr) {
jobjectArray jarr =
env->NewObjectArray(arr.size(), connectionNotificationCls, nullptr);
if (!jarr) {
@@ -321,7 +322,7 @@ static jobjectArray MakeJObject(JNIEnv* env, jobject inst,
}
static jobjectArray MakeJObject(JNIEnv* env, jobject inst,
wpi::ArrayRef<nt::EntryNotification> arr) {
wpi::span<const nt::EntryNotification> arr) {
jobjectArray jarr =
env->NewObjectArray(arr.size(), entryNotificationCls, nullptr);
if (!jarr) {
@@ -335,7 +336,7 @@ static jobjectArray MakeJObject(JNIEnv* env, jobject inst,
}
static jobjectArray MakeJObject(JNIEnv* env, jobject inst,
wpi::ArrayRef<nt::LogMessage> arr) {
wpi::span<const nt::LogMessage> arr) {
jobjectArray jarr = env->NewObjectArray(arr.size(), logMessageCls, nullptr);
if (!jarr) {
return nullptr;
@@ -348,7 +349,7 @@ static jobjectArray MakeJObject(JNIEnv* env, jobject inst,
}
static jobjectArray MakeJObject(JNIEnv* env, jobject inst,
wpi::ArrayRef<nt::RpcAnswer> arr) {
wpi::span<const nt::RpcAnswer> arr) {
jobjectArray jarr = env->NewObjectArray(arr.size(), rpcAnswerCls, nullptr);
if (!jarr) {
return nullptr;

View File

@@ -288,47 +288,47 @@ bool NetworkTable::GetBoolean(std::string_view key, bool defaultValue) const {
}
bool NetworkTable::PutBooleanArray(std::string_view key,
wpi::ArrayRef<int> value) {
wpi::span<const int> value) {
return GetEntry(key).SetBooleanArray(value);
}
bool NetworkTable::SetDefaultBooleanArray(std::string_view key,
wpi::ArrayRef<int> defaultValue) {
wpi::span<const int> defaultValue) {
return GetEntry(key).SetDefaultBooleanArray(defaultValue);
}
std::vector<int> NetworkTable::GetBooleanArray(
std::string_view key, wpi::ArrayRef<int> defaultValue) const {
std::string_view key, wpi::span<const int> defaultValue) const {
return GetEntry(key).GetBooleanArray(defaultValue);
}
bool NetworkTable::PutNumberArray(std::string_view key,
wpi::ArrayRef<double> value) {
wpi::span<const double> value) {
return GetEntry(key).SetDoubleArray(value);
}
bool NetworkTable::SetDefaultNumberArray(std::string_view key,
wpi::ArrayRef<double> defaultValue) {
wpi::span<const double> defaultValue) {
return GetEntry(key).SetDefaultDoubleArray(defaultValue);
}
std::vector<double> NetworkTable::GetNumberArray(
std::string_view key, wpi::ArrayRef<double> defaultValue) const {
std::string_view key, wpi::span<const double> defaultValue) const {
return GetEntry(key).GetDoubleArray(defaultValue);
}
bool NetworkTable::PutStringArray(std::string_view key,
wpi::ArrayRef<std::string> value) {
wpi::span<const std::string> value) {
return GetEntry(key).SetStringArray(value);
}
bool NetworkTable::SetDefaultStringArray(
std::string_view key, wpi::ArrayRef<std::string> defaultValue) {
std::string_view key, wpi::span<const std::string> defaultValue) {
return GetEntry(key).SetDefaultStringArray(defaultValue);
}
std::vector<std::string> NetworkTable::GetStringArray(
std::string_view key, wpi::ArrayRef<std::string> defaultValue) const {
std::string_view key, wpi::span<const std::string> defaultValue) const {
return GetEntry(key).GetStringArray(defaultValue);
}

View File

@@ -23,8 +23,8 @@ std::shared_ptr<NetworkTable> NetworkTableInstance::GetTable(
}
}
void NetworkTableInstance::StartClient(wpi::ArrayRef<std::string_view> servers,
unsigned int port) {
void NetworkTableInstance::StartClient(
wpi::span<const std::string_view> servers, unsigned int port) {
wpi::SmallVector<std::pair<std::string_view, unsigned int>, 8> server_ports;
for (const auto& server : servers) {
server_ports.emplace_back(std::make_pair(server, port));
@@ -32,7 +32,7 @@ void NetworkTableInstance::StartClient(wpi::ArrayRef<std::string_view> servers,
StartClient(server_ports);
}
void NetworkTableInstance::SetServer(wpi::ArrayRef<std::string_view> servers,
void NetworkTableInstance::SetServer(wpi::span<const std::string_view> servers,
unsigned int port) {
wpi::SmallVector<std::pair<std::string_view, unsigned int>, 8> server_ports;
for (const auto& server : servers) {

View File

@@ -6,6 +6,7 @@
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <string_view>
#include <wpi/MemAlloc.h>
@@ -550,8 +551,7 @@ char* NT_PackRpcValues(const NT_Value** values, size_t values_len,
NT_Value** NT_UnpackRpcValues(const char* packed, size_t packed_len,
const NT_Type* types, size_t types_len) {
auto values_v = nt::UnpackRpcValues({packed, packed_len},
wpi::ArrayRef<NT_Type>(types, types_len));
auto values_v = nt::UnpackRpcValues({packed, packed_len}, {types, types_len});
if (values_v.size() == 0) {
return nullptr;
}
@@ -972,24 +972,22 @@ NT_Bool NT_SetEntryBooleanArray(NT_Entry entry, uint64_t time,
const NT_Bool* arr, size_t size,
NT_Bool force) {
if (force != 0) {
nt::SetEntryTypeValue(
entry, Value::MakeBooleanArray(wpi::makeArrayRef(arr, size), time));
nt::SetEntryTypeValue(entry,
Value::MakeBooleanArray(wpi::span(arr, size), time));
return 1;
} else {
return nt::SetEntryValue(
entry, Value::MakeBooleanArray(wpi::makeArrayRef(arr, size), time));
entry, Value::MakeBooleanArray(wpi::span(arr, size), time));
}
}
NT_Bool NT_SetEntryDoubleArray(NT_Entry entry, uint64_t time, const double* arr,
size_t size, NT_Bool force) {
if (force != 0) {
nt::SetEntryTypeValue(
entry, Value::MakeDoubleArray(wpi::makeArrayRef(arr, size), time));
nt::SetEntryTypeValue(entry, Value::MakeDoubleArray({arr, size}, time));
return 1;
} else {
return nt::SetEntryValue(
entry, Value::MakeDoubleArray(wpi::makeArrayRef(arr, size), time));
return nt::SetEntryValue(entry, Value::MakeDoubleArray({arr, size}, time));
}
}
@@ -1138,16 +1136,15 @@ NT_Bool NT_SetDefaultEntryBooleanArray(NT_Entry entry, uint64_t time,
const NT_Bool* default_value,
size_t default_size) {
return nt::SetDefaultEntryValue(
entry, Value::MakeBooleanArray(
wpi::makeArrayRef(default_value, default_size), time));
entry,
Value::MakeBooleanArray(wpi::span(default_value, default_size), time));
}
NT_Bool NT_SetDefaultEntryDoubleArray(NT_Entry entry, uint64_t time,
const double* default_value,
size_t default_size) {
return nt::SetDefaultEntryValue(
entry, Value::MakeDoubleArray(
wpi::makeArrayRef(default_value, default_size), time));
entry, Value::MakeDoubleArray({default_value, default_size}, time));
}
NT_Bool NT_SetDefaultEntryStringArray(NT_Entry entry, uint64_t time,

View File

@@ -792,7 +792,7 @@ bool UnpackRpcDefinition(std::string_view packed, RpcDefinition* def) {
return true;
}
std::string PackRpcValues(wpi::ArrayRef<std::shared_ptr<Value>> values) {
std::string PackRpcValues(wpi::span<const std::shared_ptr<Value>> values) {
WireEncoder enc(0x0300);
for (auto& value : values) {
enc.WriteValue(*value);
@@ -801,7 +801,7 @@ std::string PackRpcValues(wpi::ArrayRef<std::shared_ptr<Value>> values) {
}
std::vector<std::shared_ptr<Value>> UnpackRpcValues(
std::string_view packed, wpi::ArrayRef<NT_Type> types) {
std::string_view packed, wpi::span<const NT_Type> types) {
wpi::raw_mem_istream is(packed.data(), packed.size());
wpi::Logger logger;
WireDecoder dec(is, 0x0300, logger);
@@ -900,7 +900,7 @@ void StartClient(NT_Inst inst, const char* server_name, unsigned int port) {
void StartClient(
NT_Inst inst,
wpi::ArrayRef<std::pair<std::string_view, unsigned int>> servers) {
wpi::span<const std::pair<std::string_view, unsigned int>> servers) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
if (!ii) {
return;
@@ -940,7 +940,7 @@ void SetServer(NT_Inst inst, const char* server_name, unsigned int port) {
void SetServer(
NT_Inst inst,
wpi::ArrayRef<std::pair<std::string_view, unsigned int>> servers) {
wpi::span<const std::pair<std::string_view, unsigned int>> servers) {
auto ii = InstanceImpl::Get(Handle{inst}.GetTypedInst(Handle::kInstance));
if (!ii) {
return;

View File

@@ -4,6 +4,8 @@
#include "ntcore_test.h"
#include <cstring>
#include <wpi/MemAlloc.h>
#include "Value_internal.h"