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

@@ -14,9 +14,9 @@
#include <string_view>
#include <utility>
#include "wpi/ArrayRef.h"
#include "wpi/Signal.h"
#include "wpi/SmallVector.h"
#include "wpi/span.h"
#include "wpi/uv/Buffer.h"
#include "wpi/uv/Error.h"
#include "wpi/uv/Timer.h"
@@ -78,7 +78,7 @@ class WebSocket : public std::enable_shared_from_this<WebSocket> {
uv::Timer::Time handshakeTimeout; // NOLINT
/** Additional headers to include in handshake. */
ArrayRef<std::pair<std::string_view, std::string_view>> extraHeaders;
span<const std::pair<std::string_view, std::string_view>> extraHeaders;
};
/**
@@ -93,7 +93,7 @@ class WebSocket : public std::enable_shared_from_this<WebSocket> {
*/
static std::shared_ptr<WebSocket> CreateClient(
uv::Stream& stream, std::string_view uri, std::string_view host,
ArrayRef<std::string_view> protocols = {},
span<const std::string_view> protocols = {},
const ClientOptions& options = {});
/**
@@ -110,8 +110,7 @@ class WebSocket : public std::enable_shared_from_this<WebSocket> {
uv::Stream& stream, std::string_view uri, std::string_view host,
std::initializer_list<std::string_view> protocols,
const ClientOptions& options = {}) {
return CreateClient(stream, uri, host,
makeArrayRef(protocols.begin(), protocols.end()),
return CreateClient(stream, uri, host, {protocols.begin(), protocols.end()},
options);
}
@@ -182,23 +181,41 @@ class WebSocket : public std::enable_shared_from_this<WebSocket> {
* @param data UTF-8 encoded data to send
* @param callback Callback which is invoked when the write completes.
*/
void SendText(
ArrayRef<uv::Buffer> data,
std::function<void(MutableArrayRef<uv::Buffer>, uv::Error)> callback) {
void SendText(span<const uv::Buffer> data,
std::function<void(span<uv::Buffer>, uv::Error)> callback) {
Send(kFlagFin | kOpText, data, callback);
}
/**
* Send a text message.
* @param data UTF-8 encoded data to send
* @param callback Callback which is invoked when the write completes.
*/
void SendText(std::initializer_list<uv::Buffer> data,
std::function<void(span<uv::Buffer>, uv::Error)> callback) {
SendText({data.begin(), data.end()}, callback);
}
/**
* Send a binary message.
* @param data Data to send
* @param callback Callback which is invoked when the write completes.
*/
void SendBinary(
ArrayRef<uv::Buffer> data,
std::function<void(MutableArrayRef<uv::Buffer>, uv::Error)> callback) {
void SendBinary(span<const uv::Buffer> data,
std::function<void(span<uv::Buffer>, uv::Error)> callback) {
Send(kFlagFin | kOpBinary, data, callback);
}
/**
* Send a binary message.
* @param data Data to send
* @param callback Callback which is invoked when the write completes.
*/
void SendBinary(std::initializer_list<uv::Buffer> data,
std::function<void(span<uv::Buffer>, uv::Error)> callback) {
SendBinary({data.begin(), data.end()}, callback);
}
/**
* Send a text message fragment. This must be followed by one or more
* SendFragment() calls, where the last one has fin=True, to complete the
@@ -207,11 +224,24 @@ class WebSocket : public std::enable_shared_from_this<WebSocket> {
* @param callback Callback which is invoked when the write completes.
*/
void SendTextFragment(
ArrayRef<uv::Buffer> data,
std::function<void(MutableArrayRef<uv::Buffer>, uv::Error)> callback) {
span<const uv::Buffer> data,
std::function<void(span<uv::Buffer>, uv::Error)> callback) {
Send(kOpText, data, callback);
}
/**
* Send a text message fragment. This must be followed by one or more
* SendFragment() calls, where the last one has fin=True, to complete the
* message.
* @param data UTF-8 encoded data to send
* @param callback Callback which is invoked when the write completes.
*/
void SendTextFragment(
std::initializer_list<uv::Buffer> data,
std::function<void(span<uv::Buffer>, uv::Error)> callback) {
SendTextFragment({data.begin(), data.end()}, callback);
}
/**
* Send a text message fragment. This must be followed by one or more
* SendFragment() calls, where the last one has fin=True, to complete the
@@ -220,11 +250,24 @@ class WebSocket : public std::enable_shared_from_this<WebSocket> {
* @param callback Callback which is invoked when the write completes.
*/
void SendBinaryFragment(
ArrayRef<uv::Buffer> data,
std::function<void(MutableArrayRef<uv::Buffer>, uv::Error)> callback) {
span<const uv::Buffer> data,
std::function<void(span<uv::Buffer>, uv::Error)> callback) {
Send(kOpBinary, data, callback);
}
/**
* Send a text message fragment. This must be followed by one or more
* SendFragment() calls, where the last one has fin=True, to complete the
* message.
* @param data Data to send
* @param callback Callback which is invoked when the write completes.
*/
void SendBinaryFragment(
std::initializer_list<uv::Buffer> data,
std::function<void(span<uv::Buffer>, uv::Error)> callback) {
SendBinaryFragment({data.begin(), data.end()}, callback);
}
/**
* Send a continuation frame. This is used to send additional parts of a
* message started with SendTextFragment() or SendBinaryFragment().
@@ -232,19 +275,30 @@ class WebSocket : public std::enable_shared_from_this<WebSocket> {
* @param fin Set to true if this is the final fragment of the message
* @param callback Callback which is invoked when the write completes.
*/
void SendFragment(
ArrayRef<uv::Buffer> data, bool fin,
std::function<void(MutableArrayRef<uv::Buffer>, uv::Error)> callback) {
void SendFragment(span<const uv::Buffer> data, bool fin,
std::function<void(span<uv::Buffer>, uv::Error)> callback) {
Send(kOpCont | (fin ? kFlagFin : 0), data, callback);
}
/**
* Send a continuation frame. This is used to send additional parts of a
* message started with SendTextFragment() or SendBinaryFragment().
* @param data Data to send
* @param fin Set to true if this is the final fragment of the message
* @param callback Callback which is invoked when the write completes.
*/
void SendFragment(std::initializer_list<uv::Buffer> data, bool fin,
std::function<void(span<uv::Buffer>, uv::Error)> callback) {
SendFragment({data.begin(), data.end()}, fin, callback);
}
/**
* Send a ping frame with no data.
* @param callback Optional callback which is invoked when the ping frame
* write completes.
*/
void SendPing(std::function<void(uv::Error)> callback = nullptr) {
SendPing(ArrayRef<uv::Buffer>{}, [callback](auto bufs, uv::Error err) {
SendPing({}, [callback](auto bufs, uv::Error err) {
if (callback) {
callback(err);
}
@@ -257,19 +311,29 @@ class WebSocket : public std::enable_shared_from_this<WebSocket> {
* @param callback Callback which is invoked when the ping frame
* write completes.
*/
void SendPing(
ArrayRef<uv::Buffer> data,
std::function<void(MutableArrayRef<uv::Buffer>, uv::Error)> callback) {
void SendPing(span<const uv::Buffer> data,
std::function<void(span<uv::Buffer>, uv::Error)> callback) {
Send(kFlagFin | kOpPing, data, callback);
}
/**
* Send a ping frame.
* @param data Data to send in the ping frame
* @param callback Callback which is invoked when the ping frame
* write completes.
*/
void SendPing(std::initializer_list<uv::Buffer> data,
std::function<void(span<uv::Buffer>, uv::Error)> callback) {
SendPing({data.begin(), data.end()}, callback);
}
/**
* Send a pong frame with no data.
* @param callback Optional callback which is invoked when the pong frame
* write completes.
*/
void SendPong(std::function<void(uv::Error)> callback = nullptr) {
SendPong(ArrayRef<uv::Buffer>{}, [callback](auto bufs, uv::Error err) {
SendPong({}, [callback](auto bufs, uv::Error err) {
if (callback) {
callback(err);
}
@@ -282,12 +346,22 @@ class WebSocket : public std::enable_shared_from_this<WebSocket> {
* @param callback Callback which is invoked when the pong frame
* write completes.
*/
void SendPong(
ArrayRef<uv::Buffer> data,
std::function<void(MutableArrayRef<uv::Buffer>, uv::Error)> callback) {
void SendPong(span<const uv::Buffer> data,
std::function<void(span<uv::Buffer>, uv::Error)> callback) {
Send(kFlagFin | kOpPong, data, callback);
}
/**
* Send a pong frame.
* @param data Data to send in the pong frame
* @param callback Callback which is invoked when the pong frame
* write completes.
*/
void SendPong(std::initializer_list<uv::Buffer> data,
std::function<void(span<uv::Buffer>, uv::Error)> callback) {
SendPong({data.begin(), data.end()}, callback);
}
/**
* Fail the connection.
*/
@@ -339,17 +413,17 @@ class WebSocket : public std::enable_shared_from_this<WebSocket> {
* The first parameter is the data, the second parameter is true if the
* data is the last fragment of the message.
*/
sig::Signal<ArrayRef<uint8_t>, bool> binary;
sig::Signal<span<const uint8_t>, bool> binary;
/**
* Ping event. Emitted when a ping message is received.
*/
sig::Signal<ArrayRef<uint8_t>> ping;
sig::Signal<span<const uint8_t>> ping;
/**
* Pong event. Emitted when a pong message is received.
*/
sig::Signal<ArrayRef<uint8_t>> pong;
sig::Signal<span<const uint8_t>> pong;
private:
// user data
@@ -382,7 +456,7 @@ class WebSocket : public std::enable_shared_from_this<WebSocket> {
std::unique_ptr<ClientHandshakeData> m_clientHandshake;
void StartClient(std::string_view uri, std::string_view host,
ArrayRef<std::string_view> protocols,
span<const std::string_view> protocols,
const ClientOptions& options);
void StartServer(std::string_view key, std::string_view version,
std::string_view protocol);
@@ -390,9 +464,8 @@ class WebSocket : public std::enable_shared_from_this<WebSocket> {
void SetClosed(uint16_t code, std::string_view reason, bool failed = false);
void Shutdown();
void HandleIncoming(uv::Buffer& buf, size_t size);
void Send(
uint8_t opcode, ArrayRef<uv::Buffer> data,
std::function<void(MutableArrayRef<uv::Buffer>, uv::Error)> callback);
void Send(uint8_t opcode, span<const uv::Buffer> data,
std::function<void(span<uv::Buffer>, uv::Error)> callback);
};
} // namespace wpi