Remove f2, add ofats::any_invocable
This commit is contained in:
@@ -70,7 +70,7 @@ public:
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
TemplatedApp &&missingServerName(fu2::unique_function<void(const char *hostname)> handler) {
|
||||
TemplatedApp &&missingServerName(MoveOnlyFunction<void(const char *hostname)> handler) {
|
||||
|
||||
if (!constructorFailed()) {
|
||||
httpContext->getSocketContextData()->missingServerNameHandler = std::move(handler);
|
||||
@@ -92,7 +92,7 @@ public:
|
||||
}
|
||||
|
||||
/* Attaches a "filter" function to track socket connections/disconnections */
|
||||
void filter(fu2::unique_function<void(HttpResponse<SSL> *, int)> &&filterHandler) {
|
||||
void filter(MoveOnlyFunction<void(HttpResponse<SSL> *, int)> &&filterHandler) {
|
||||
httpContext->filter(std::move(filterHandler));
|
||||
}
|
||||
|
||||
@@ -148,13 +148,13 @@ public:
|
||||
bool resetIdleTimeoutOnSend = false;
|
||||
/* A good default, esp. for newcomers */
|
||||
bool sendPingsAutomatically = true;
|
||||
fu2::unique_function<void(HttpResponse<SSL> *, HttpRequest *, struct us_socket_context_t *)> upgrade = nullptr;
|
||||
fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> open = nullptr;
|
||||
fu2::unique_function<void(uWS::WebSocket<SSL, true> *, std::string_view, uWS::OpCode)> message = nullptr;
|
||||
fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> drain = nullptr;
|
||||
fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> ping = nullptr;
|
||||
fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> pong = nullptr;
|
||||
fu2::unique_function<void(uWS::WebSocket<SSL, true> *, int, std::string_view)> close = nullptr;
|
||||
MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *, struct us_socket_context_t *)> upgrade = nullptr;
|
||||
MoveOnlyFunction<void(uWS::WebSocket<SSL, true> *)> open = nullptr;
|
||||
MoveOnlyFunction<void(uWS::WebSocket<SSL, true> *, std::string_view, uWS::OpCode)> message = nullptr;
|
||||
MoveOnlyFunction<void(uWS::WebSocket<SSL, true> *)> drain = nullptr;
|
||||
MoveOnlyFunction<void(uWS::WebSocket<SSL, true> *)> ping = nullptr;
|
||||
MoveOnlyFunction<void(uWS::WebSocket<SSL, true> *)> pong = nullptr;
|
||||
MoveOnlyFunction<void(uWS::WebSocket<SSL, true> *, int, std::string_view)> close = nullptr;
|
||||
};
|
||||
|
||||
template <typename UserData>
|
||||
@@ -255,63 +255,63 @@ public:
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
TemplatedApp &&get(std::string pattern, fu2::unique_function<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
TemplatedApp &&get(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
if (httpContext) {
|
||||
httpContext->onHttp("get", pattern, std::move(handler));
|
||||
}
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
TemplatedApp &&post(std::string pattern, fu2::unique_function<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
TemplatedApp &&post(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
if (httpContext) {
|
||||
httpContext->onHttp("post", pattern, std::move(handler));
|
||||
}
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
TemplatedApp &&options(std::string pattern, fu2::unique_function<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
TemplatedApp &&options(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
if (httpContext) {
|
||||
httpContext->onHttp("options", pattern, std::move(handler));
|
||||
}
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
TemplatedApp &&del(std::string pattern, fu2::unique_function<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
TemplatedApp &&del(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
if (httpContext) {
|
||||
httpContext->onHttp("delete", pattern, std::move(handler));
|
||||
}
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
TemplatedApp &&patch(std::string pattern, fu2::unique_function<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
TemplatedApp &&patch(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
if (httpContext) {
|
||||
httpContext->onHttp("patch", pattern, std::move(handler));
|
||||
}
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
TemplatedApp &&put(std::string pattern, fu2::unique_function<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
TemplatedApp &&put(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
if (httpContext) {
|
||||
httpContext->onHttp("put", pattern, std::move(handler));
|
||||
}
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
TemplatedApp &&head(std::string pattern, fu2::unique_function<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
TemplatedApp &&head(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
if (httpContext) {
|
||||
httpContext->onHttp("head", pattern, std::move(handler));
|
||||
}
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
TemplatedApp &&connect(std::string pattern, fu2::unique_function<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
TemplatedApp &&connect(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
if (httpContext) {
|
||||
httpContext->onHttp("connect", pattern, std::move(handler));
|
||||
}
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
TemplatedApp &&trace(std::string pattern, fu2::unique_function<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
TemplatedApp &&trace(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
if (httpContext) {
|
||||
httpContext->onHttp("trace", pattern, std::move(handler));
|
||||
}
|
||||
@@ -319,7 +319,7 @@ public:
|
||||
}
|
||||
|
||||
/* This one catches any method */
|
||||
TemplatedApp &&any(std::string pattern, fu2::unique_function<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
TemplatedApp &&any(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||
if (httpContext) {
|
||||
httpContext->onHttp("*", pattern, std::move(handler));
|
||||
}
|
||||
@@ -327,7 +327,7 @@ public:
|
||||
}
|
||||
|
||||
/* Host, port, callback */
|
||||
TemplatedApp &&listen(std::string host, int port, fu2::unique_function<void(us_listen_socket_t *)> &&handler) {
|
||||
TemplatedApp &&listen(std::string host, int port, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
|
||||
if (!host.length()) {
|
||||
return listen(port, std::move(handler));
|
||||
}
|
||||
@@ -336,7 +336,7 @@ public:
|
||||
}
|
||||
|
||||
/* Host, port, options, callback */
|
||||
TemplatedApp &&listen(std::string host, int port, int options, fu2::unique_function<void(us_listen_socket_t *)> &&handler) {
|
||||
TemplatedApp &&listen(std::string host, int port, int options, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
|
||||
if (!host.length()) {
|
||||
return listen(port, options, std::move(handler));
|
||||
}
|
||||
@@ -345,13 +345,13 @@ public:
|
||||
}
|
||||
|
||||
/* Port, callback */
|
||||
TemplatedApp &&listen(int port, fu2::unique_function<void(us_listen_socket_t *)> &&handler) {
|
||||
TemplatedApp &&listen(int port, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
|
||||
handler(httpContext ? httpContext->listen(nullptr, port, 0) : nullptr);
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
/* Port, options, callback */
|
||||
TemplatedApp &&listen(int port, int options, fu2::unique_function<void(us_listen_socket_t *)> &&handler) {
|
||||
TemplatedApp &&listen(int port, int options, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
|
||||
handler(httpContext ? httpContext->listen(nullptr, port, options) : nullptr);
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
+3
-3
@@ -28,7 +28,7 @@
|
||||
|
||||
#include <string_view>
|
||||
#include <iostream>
|
||||
#include "f2/function2.hpp"
|
||||
#include "MoveOnlyFunction.h"
|
||||
|
||||
namespace uWS {
|
||||
template<bool> struct HttpResponse;
|
||||
@@ -387,12 +387,12 @@ public:
|
||||
us_socket_context_free(SSL, getSocketContext());
|
||||
}
|
||||
|
||||
void filter(fu2::unique_function<void(HttpResponse<SSL> *, int)> &&filterHandler) {
|
||||
void filter(MoveOnlyFunction<void(HttpResponse<SSL> *, int)> &&filterHandler) {
|
||||
getSocketContextData()->filterHandlers.emplace_back(std::move(filterHandler));
|
||||
}
|
||||
|
||||
/* Register an HTTP route handler acording to URL pattern */
|
||||
void onHttp(std::string method, std::string pattern, fu2::unique_function<void(HttpResponse<SSL> *, HttpRequest *)> &&handler, bool upgrade = false) {
|
||||
void onHttp(std::string method, std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler, bool upgrade = false) {
|
||||
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
||||
|
||||
/* Todo: This is ugly, fix */
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
#include "HttpRouter.h"
|
||||
|
||||
#include <vector>
|
||||
#include "f2/function2.hpp"
|
||||
#include "MoveOnlyFunction.h"
|
||||
|
||||
namespace uWS {
|
||||
template<bool> struct HttpResponse;
|
||||
@@ -33,9 +33,9 @@ struct alignas(16) HttpContextData {
|
||||
template <bool> friend struct HttpResponse;
|
||||
template <bool> friend struct TemplatedApp;
|
||||
private:
|
||||
std::vector<fu2::unique_function<void(HttpResponse<SSL> *, int)>> filterHandlers;
|
||||
std::vector<MoveOnlyFunction<void(HttpResponse<SSL> *, int)>> filterHandlers;
|
||||
|
||||
fu2::unique_function<void(const char *hostname)> missingServerNameHandler;
|
||||
MoveOnlyFunction<void(const char *hostname)> missingServerNameHandler;
|
||||
|
||||
struct RouterData {
|
||||
HttpResponse<SSL> *httpResponse;
|
||||
|
||||
+3
-3
@@ -25,7 +25,7 @@
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include "f2/function2.hpp"
|
||||
#include "MoveOnlyFunction.h"
|
||||
|
||||
#include "BloomFilter.h"
|
||||
#include "ProxyParser.h"
|
||||
@@ -220,7 +220,7 @@ private:
|
||||
|
||||
// the only caller of getHeaders
|
||||
template <int CONSUME_MINIMALLY>
|
||||
std::pair<unsigned int, void *> fenceAndConsumePostPadded(char *data, unsigned int length, void *user, void *reserved, HttpRequest *req, fu2::unique_function<void *(void *, HttpRequest *)> &requestHandler, fu2::unique_function<void *(void *, std::string_view, bool)> &dataHandler) {
|
||||
std::pair<unsigned int, void *> fenceAndConsumePostPadded(char *data, unsigned int length, void *user, void *reserved, HttpRequest *req, MoveOnlyFunction<void *(void *, HttpRequest *)> &requestHandler, MoveOnlyFunction<void *(void *, std::string_view, bool)> &dataHandler) {
|
||||
|
||||
/* How much data we CONSUMED (to throw away) */
|
||||
unsigned int consumedTotal = 0;
|
||||
@@ -286,7 +286,7 @@ private:
|
||||
}
|
||||
|
||||
public:
|
||||
void *consumePostPadded(char *data, unsigned int length, void *user, void *reserved, fu2::unique_function<void *(void *, HttpRequest *)> &&requestHandler, fu2::unique_function<void *(void *, std::string_view, bool)> &&dataHandler, fu2::unique_function<void *(void *)> &&errorHandler) {
|
||||
void *consumePostPadded(char *data, unsigned int length, void *user, void *reserved, MoveOnlyFunction<void *(void *, HttpRequest *)> &&requestHandler, MoveOnlyFunction<void *(void *, std::string_view, bool)> &&dataHandler, MoveOnlyFunction<void *(void *)> &&errorHandler) {
|
||||
|
||||
/* This resets BloomFilter by construction, but later we also reset it again.
|
||||
* Optimize this to skip resetting twice (req could be made global) */
|
||||
|
||||
+5
-5
@@ -30,7 +30,7 @@
|
||||
#include "WebSocket.h"
|
||||
#include "WebSocketContextData.h"
|
||||
|
||||
#include "f2/function2.hpp"
|
||||
#include "MoveOnlyFunction.h"
|
||||
|
||||
/* todo: tryWrite is missing currently, only send smaller segments with write */
|
||||
|
||||
@@ -427,7 +427,7 @@ public:
|
||||
}
|
||||
|
||||
/* Corks the response if possible. Leaves already corked socket be. */
|
||||
HttpResponse *cork(fu2::unique_function<void()> &&handler) {
|
||||
HttpResponse *cork(MoveOnlyFunction<void()> &&handler) {
|
||||
if (!Super::isCorked() && Super::canCork()) {
|
||||
Super::cork();
|
||||
handler();
|
||||
@@ -448,7 +448,7 @@ public:
|
||||
}
|
||||
|
||||
/* Attach handler for writable HTTP response */
|
||||
HttpResponse *onWritable(fu2::unique_function<bool(size_t)> &&handler) {
|
||||
HttpResponse *onWritable(MoveOnlyFunction<bool(size_t)> &&handler) {
|
||||
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
|
||||
|
||||
httpResponseData->onWritable = std::move(handler);
|
||||
@@ -456,7 +456,7 @@ public:
|
||||
}
|
||||
|
||||
/* Attach handler for aborted HTTP request */
|
||||
HttpResponse *onAborted(fu2::unique_function<void()> &&handler) {
|
||||
HttpResponse *onAborted(MoveOnlyFunction<void()> &&handler) {
|
||||
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
|
||||
|
||||
httpResponseData->onAborted = std::move(handler);
|
||||
@@ -464,7 +464,7 @@ public:
|
||||
}
|
||||
|
||||
/* Attach a read handler for data sent. Will be called with FIN set true if last segment. */
|
||||
void onData(fu2::unique_function<void(std::string_view, bool)> &&handler) {
|
||||
void onData(MoveOnlyFunction<void(std::string_view, bool)> &&handler) {
|
||||
HttpResponseData<SSL> *data = getHttpResponseData();
|
||||
data->inStream = std::move(handler);
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#include "AsyncSocketData.h"
|
||||
#include "ProxyParser.h"
|
||||
|
||||
#include "f2/function2.hpp"
|
||||
#include "MoveOnlyFunction.h"
|
||||
|
||||
namespace uWS {
|
||||
|
||||
@@ -43,9 +43,9 @@ private:
|
||||
};
|
||||
|
||||
/* Per socket event handlers */
|
||||
fu2::unique_function<bool(size_t)> onWritable;
|
||||
fu2::unique_function<void()> onAborted;
|
||||
fu2::unique_function<void(std::string_view, bool)> inStream; // onData
|
||||
MoveOnlyFunction<bool(size_t)> onWritable;
|
||||
MoveOnlyFunction<void()> onAborted;
|
||||
MoveOnlyFunction<void(std::string_view, bool)> inStream; // onData
|
||||
/* Outgoing offset */
|
||||
size_t offset = 0;
|
||||
|
||||
|
||||
+3
-3
@@ -27,7 +27,7 @@
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "f2/function2.hpp"
|
||||
#include "MoveOnlyFunction.h"
|
||||
|
||||
namespace uWS {
|
||||
|
||||
@@ -48,7 +48,7 @@ private:
|
||||
std::map<std::string, int> priority;
|
||||
|
||||
/* List of handlers */
|
||||
std::vector<fu2::unique_function<bool(HttpRouter *)>> handlers;
|
||||
std::vector<MoveOnlyFunction<bool(HttpRouter *)>> handlers;
|
||||
|
||||
/* Current URL cache */
|
||||
std::string_view currentUrl;
|
||||
@@ -229,7 +229,7 @@ public:
|
||||
}
|
||||
|
||||
/* Adds the corresponding entires in matching tree and handler list */
|
||||
void add(std::vector<std::string> methods, std::string pattern, fu2::unique_function<bool(HttpRouter *)> &&handler, uint32_t priority = MEDIUM_PRIORITY) {
|
||||
void add(std::vector<std::string> methods, std::string pattern, MoveOnlyFunction<bool(HttpRouter *)> &&handler, uint32_t priority = MEDIUM_PRIORITY) {
|
||||
for (std::string method : methods) {
|
||||
/* Lookup method */
|
||||
Node *node = getNode(&root, method, false);
|
||||
|
||||
+3
-3
@@ -116,7 +116,7 @@ public:
|
||||
getLazyLoop().loop = nullptr;
|
||||
}
|
||||
|
||||
void addPostHandler(void *key, fu2::unique_function<void(Loop *)> &&handler) {
|
||||
void addPostHandler(void *key, MoveOnlyFunction<void(Loop *)> &&handler) {
|
||||
LoopData *loopData = (LoopData *) us_loop_ext((us_loop_t *) this);
|
||||
|
||||
loopData->postHandlers.emplace(key, std::move(handler));
|
||||
@@ -129,7 +129,7 @@ public:
|
||||
loopData->postHandlers.erase(key);
|
||||
}
|
||||
|
||||
void addPreHandler(void *key, fu2::unique_function<void(Loop *)> &&handler) {
|
||||
void addPreHandler(void *key, MoveOnlyFunction<void(Loop *)> &&handler) {
|
||||
LoopData *loopData = (LoopData *) us_loop_ext((us_loop_t *) this);
|
||||
|
||||
loopData->preHandlers.emplace(key, std::move(handler));
|
||||
@@ -143,7 +143,7 @@ public:
|
||||
}
|
||||
|
||||
/* Defer this callback on Loop's thread of execution */
|
||||
void defer(fu2::unique_function<void()> &&cb) {
|
||||
void defer(MoveOnlyFunction<void()> &&cb) {
|
||||
LoopData *loopData = (LoopData *) us_loop_ext((us_loop_t *) this);
|
||||
|
||||
//if (std::thread::get_id() == ) // todo: add fast path for same thread id
|
||||
|
||||
+3
-3
@@ -26,7 +26,7 @@
|
||||
|
||||
#include "PerMessageDeflate.h"
|
||||
|
||||
#include "f2/function2.hpp"
|
||||
#include "MoveOnlyFunction.h"
|
||||
|
||||
namespace uWS {
|
||||
|
||||
@@ -37,10 +37,10 @@ struct alignas(16) LoopData {
|
||||
private:
|
||||
std::mutex deferMutex;
|
||||
int currentDeferQueue = 0;
|
||||
std::vector<fu2::unique_function<void()>> deferQueues[2];
|
||||
std::vector<MoveOnlyFunction<void()>> deferQueues[2];
|
||||
|
||||
/* Map from void ptr to handler */
|
||||
std::map<void *, fu2::unique_function<void(Loop *)>> postHandlers, preHandlers;
|
||||
std::map<void *, MoveOnlyFunction<void(Loop *)>> postHandlers, preHandlers;
|
||||
|
||||
public:
|
||||
~LoopData() {
|
||||
|
||||
@@ -0,0 +1,377 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020 Oleg Fatkhiev
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
/* Sources fetched from https://github.com/ofats/any_invocable on 2021-02-19. */
|
||||
|
||||
#ifndef _ANY_INVOKABLE_H_
|
||||
#define _ANY_INVOKABLE_H_
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
// clang-format off
|
||||
/*
|
||||
namespace std {
|
||||
template<class Sig> class any_invocable; // never defined
|
||||
|
||||
template<class R, class... ArgTypes>
|
||||
class any_invocable<R(ArgTypes...) cv ref noexcept(noex)> {
|
||||
public:
|
||||
using result_type = R;
|
||||
|
||||
// SECTION.3, construct/copy/destroy
|
||||
any_invocable() noexcept;
|
||||
any_invocable(nullptr_t) noexcept;
|
||||
any_invocable(any_invocable&&) noexcept;
|
||||
template<class F> any_invocable(F&&);
|
||||
|
||||
template<class T, class... Args>
|
||||
explicit any_invocable(in_place_type_t<T>, Args&&...);
|
||||
template<class T, class U, class... Args>
|
||||
explicit any_invocable(in_place_type_t<T>, initializer_list<U>, Args&&...);
|
||||
|
||||
any_invocable& operator=(any_invocable&&) noexcept;
|
||||
any_invocable& operator=(nullptr_t) noexcept;
|
||||
template<class F> any_invocable& operator=(F&&);
|
||||
template<class F> any_invocable& operator=(reference_wrapper<F>) noexcept;
|
||||
|
||||
~any_invocable();
|
||||
|
||||
// SECTION.4, any_invocable modifiers
|
||||
void swap(any_invocable&) noexcept;
|
||||
|
||||
// SECTION.5, any_invocable capacity
|
||||
explicit operator bool() const noexcept;
|
||||
|
||||
// SECTION.6, any_invocable invocation
|
||||
R operator()(ArgTypes...) cv ref noexcept(noex);
|
||||
|
||||
// SECTION.7, null pointer comparisons
|
||||
friend bool operator==(const any_invocable&, nullptr_t) noexcept;
|
||||
|
||||
// SECTION.8, specialized algorithms
|
||||
friend void swap(any_invocable&, any_invocable&) noexcept;
|
||||
};
|
||||
}
|
||||
*/
|
||||
// clang-format on
|
||||
|
||||
namespace ofats {
|
||||
|
||||
namespace any_detail {
|
||||
|
||||
using buffer = std::aligned_storage_t<sizeof(void*) * 2, alignof(void*)>;
|
||||
|
||||
template <class T>
|
||||
inline constexpr bool is_small_object_v =
|
||||
sizeof(T) <= sizeof(buffer) && alignof(buffer) % alignof(T) == 0 &&
|
||||
std::is_nothrow_move_constructible_v<T>;
|
||||
|
||||
union storage {
|
||||
void* ptr_ = nullptr;
|
||||
buffer buf_;
|
||||
};
|
||||
|
||||
enum class action { destroy, move };
|
||||
|
||||
template <class R, class... ArgTypes>
|
||||
struct handler_traits {
|
||||
template <class Derived>
|
||||
struct handler_base {
|
||||
static void handle(action act, storage* current, storage* other = nullptr) {
|
||||
switch (act) {
|
||||
case (action::destroy):
|
||||
Derived::destroy(*current);
|
||||
break;
|
||||
case (action::move):
|
||||
Derived::move(*current, *other);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct small_handler : handler_base<small_handler<T>> {
|
||||
template <class... Args>
|
||||
static void create(storage& s, Args&&... args) {
|
||||
new (static_cast<void*>(&s.buf_)) T(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
static void destroy(storage& s) noexcept {
|
||||
T& value = *static_cast<T*>(static_cast<void*>(&s.buf_));
|
||||
value.~T();
|
||||
}
|
||||
|
||||
static void move(storage& dst, storage& src) noexcept {
|
||||
create(dst, std::move(*static_cast<T*>(static_cast<void*>(&src.buf_))));
|
||||
destroy(src);
|
||||
}
|
||||
|
||||
static R call(storage& s, ArgTypes... args) {
|
||||
return std::invoke(*static_cast<T*>(static_cast<void*>(&s.buf_)),
|
||||
std::forward<ArgTypes>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct large_handler : handler_base<large_handler<T>> {
|
||||
template <class... Args>
|
||||
static void create(storage& s, Args&&... args) {
|
||||
s.ptr_ = new T(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
static void destroy(storage& s) noexcept { delete static_cast<T*>(s.ptr_); }
|
||||
|
||||
static void move(storage& dst, storage& src) noexcept {
|
||||
dst.ptr_ = src.ptr_;
|
||||
}
|
||||
|
||||
static R call(storage& s, ArgTypes... args) {
|
||||
return std::invoke(*static_cast<T*>(s.ptr_),
|
||||
std::forward<ArgTypes>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
using handler = std::conditional_t<is_small_object_v<T>, small_handler<T>,
|
||||
large_handler<T>>;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct is_in_place_type : std::false_type {};
|
||||
|
||||
template <class T>
|
||||
struct is_in_place_type<std::in_place_type_t<T>> : std::true_type {};
|
||||
|
||||
template <class T>
|
||||
inline constexpr auto is_in_place_type_v = is_in_place_type<T>::value;
|
||||
|
||||
template <class R, bool is_noexcept, class... ArgTypes>
|
||||
class any_invocable_impl {
|
||||
template <class T>
|
||||
using handler =
|
||||
typename any_detail::handler_traits<R, ArgTypes...>::template handler<T>;
|
||||
|
||||
using storage = any_detail::storage;
|
||||
using action = any_detail::action;
|
||||
using handle_func = void (*)(any_detail::action, any_detail::storage*,
|
||||
any_detail::storage*);
|
||||
using call_func = R (*)(any_detail::storage&, ArgTypes...);
|
||||
|
||||
public:
|
||||
using result_type = R;
|
||||
|
||||
any_invocable_impl() noexcept = default;
|
||||
any_invocable_impl(std::nullptr_t) noexcept {}
|
||||
any_invocable_impl(any_invocable_impl&& rhs) noexcept {
|
||||
if (rhs.handle_) {
|
||||
handle_ = rhs.handle_;
|
||||
handle_(action::move, &storage_, &rhs.storage_);
|
||||
call_ = rhs.call_;
|
||||
rhs.handle_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
any_invocable_impl& operator=(any_invocable_impl&& rhs) noexcept {
|
||||
any_invocable_impl{std::move(rhs)}.swap(*this);
|
||||
return *this;
|
||||
}
|
||||
any_invocable_impl& operator=(std::nullptr_t) noexcept {
|
||||
destroy();
|
||||
return *this;
|
||||
}
|
||||
|
||||
~any_invocable_impl() { destroy(); }
|
||||
|
||||
void swap(any_invocable_impl& rhs) noexcept {
|
||||
if (handle_) {
|
||||
if (rhs.handle_) {
|
||||
storage tmp;
|
||||
handle_(action::move, &tmp, &storage_);
|
||||
rhs.handle_(action::move, &storage_, &rhs.storage_);
|
||||
handle_(action::move, &rhs.storage_, &tmp);
|
||||
std::swap(handle_, rhs.handle_);
|
||||
std::swap(call_, rhs.call_);
|
||||
} else {
|
||||
rhs.swap(*this);
|
||||
}
|
||||
} else if (rhs.handle_) {
|
||||
rhs.handle_(action::move, &storage_, &rhs.storage_);
|
||||
handle_ = rhs.handle_;
|
||||
call_ = rhs.call_;
|
||||
rhs.handle_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
explicit operator bool() const noexcept { return handle_ != nullptr; }
|
||||
|
||||
protected:
|
||||
template <class F, class... Args>
|
||||
void create(Args&&... args) {
|
||||
using hdl = handler<F>;
|
||||
hdl::create(storage_, std::forward<Args>(args)...);
|
||||
handle_ = &hdl::handle;
|
||||
call_ = &hdl::call;
|
||||
}
|
||||
|
||||
void destroy() noexcept {
|
||||
if (handle_) {
|
||||
handle_(action::destroy, &storage_, nullptr);
|
||||
handle_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
R call(ArgTypes... args) noexcept(is_noexcept) {
|
||||
return call_(storage_, std::forward<ArgTypes>(args)...);
|
||||
}
|
||||
|
||||
friend bool operator==(const any_invocable_impl& f, std::nullptr_t) noexcept {
|
||||
return !f;
|
||||
}
|
||||
friend bool operator==(std::nullptr_t, const any_invocable_impl& f) noexcept {
|
||||
return !f;
|
||||
}
|
||||
friend bool operator!=(const any_invocable_impl& f, std::nullptr_t) noexcept {
|
||||
return static_cast<bool>(f);
|
||||
}
|
||||
friend bool operator!=(std::nullptr_t, const any_invocable_impl& f) noexcept {
|
||||
return static_cast<bool>(f);
|
||||
}
|
||||
|
||||
friend void swap(any_invocable_impl& lhs, any_invocable_impl& rhs) noexcept {
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
storage storage_;
|
||||
handle_func handle_ = nullptr;
|
||||
call_func call_;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
|
||||
|
||||
template <class AI, class F, bool noex, class R, class FCall, class... ArgTypes>
|
||||
using can_convert = std::conjunction<
|
||||
std::negation<std::is_same<remove_cvref_t<F>, AI>>,
|
||||
std::negation<any_detail::is_in_place_type<remove_cvref_t<F>>>,
|
||||
std::is_invocable_r<R, FCall, ArgTypes...>,
|
||||
std::bool_constant<(!noex ||
|
||||
std::is_nothrow_invocable_r_v<R, FCall, ArgTypes...>)>,
|
||||
std::is_constructible<std::decay_t<F>, F>>;
|
||||
|
||||
} // namespace any_detail
|
||||
|
||||
template <class Signature>
|
||||
class any_invocable;
|
||||
|
||||
#define __OFATS_ANY_INVOCABLE(cv, ref, noex, inv_quals) \
|
||||
template <class R, class... ArgTypes> \
|
||||
class any_invocable<R(ArgTypes...) cv ref noexcept(noex)> \
|
||||
: public any_detail::any_invocable_impl<R, noex, ArgTypes...> { \
|
||||
using base_type = any_detail::any_invocable_impl<R, noex, ArgTypes...>; \
|
||||
\
|
||||
public: \
|
||||
using base_type::base_type; \
|
||||
\
|
||||
template < \
|
||||
class F, \
|
||||
class = std::enable_if_t<any_detail::can_convert< \
|
||||
any_invocable, F, noex, R, F inv_quals, ArgTypes...>::value>> \
|
||||
any_invocable(F&& f) { \
|
||||
base_type::template create<std::decay_t<F>>(std::forward<F>(f)); \
|
||||
} \
|
||||
\
|
||||
template <class T, class... Args, class VT = std::decay_t<T>, \
|
||||
class = std::enable_if_t< \
|
||||
std::is_move_constructible_v<VT> && \
|
||||
std::is_constructible_v<VT, Args...> && \
|
||||
std::is_invocable_r_v<R, VT inv_quals, ArgTypes...> && \
|
||||
(!noex || std::is_nothrow_invocable_r_v<R, VT inv_quals, \
|
||||
ArgTypes...>)>> \
|
||||
explicit any_invocable(std::in_place_type_t<T>, Args&&... args) { \
|
||||
base_type::template create<VT>(std::forward<Args>(args)...); \
|
||||
} \
|
||||
\
|
||||
template < \
|
||||
class T, class U, class... Args, class VT = std::decay_t<T>, \
|
||||
class = std::enable_if_t< \
|
||||
std::is_move_constructible_v<VT> && \
|
||||
std::is_constructible_v<VT, std::initializer_list<U>&, Args...> && \
|
||||
std::is_invocable_r_v<R, VT inv_quals, ArgTypes...> && \
|
||||
(!noex || \
|
||||
std::is_nothrow_invocable_r_v<R, VT inv_quals, ArgTypes...>)>> \
|
||||
explicit any_invocable(std::in_place_type_t<T>, \
|
||||
std::initializer_list<U> il, Args&&... args) { \
|
||||
base_type::template create<VT>(il, std::forward<Args>(args)...); \
|
||||
} \
|
||||
\
|
||||
template <class F, class FDec = std::decay_t<F>> \
|
||||
std::enable_if_t<!std::is_same_v<FDec, any_invocable> && \
|
||||
std::is_move_constructible_v<FDec>, \
|
||||
any_invocable&> \
|
||||
operator=(F&& f) { \
|
||||
any_invocable{std::forward<F>(f)}.swap(*this); \
|
||||
return *this; \
|
||||
} \
|
||||
template <class F> \
|
||||
any_invocable& operator=(std::reference_wrapper<F> f) { \
|
||||
any_invocable{f}.swap(*this); \
|
||||
return *this; \
|
||||
} \
|
||||
\
|
||||
R operator()(ArgTypes... args) cv ref noexcept(noex) { \
|
||||
return base_type::call(std::forward<ArgTypes>(args)...); \
|
||||
} \
|
||||
};
|
||||
|
||||
// cv -> {`empty`, const}
|
||||
// ref -> {`empty`, &, &&}
|
||||
// noex -> {true, false}
|
||||
// inv_quals -> (is_empty(ref) ? & : ref)
|
||||
__OFATS_ANY_INVOCABLE(, , false, &); // 000
|
||||
__OFATS_ANY_INVOCABLE(, , true, &); // 001
|
||||
__OFATS_ANY_INVOCABLE(, &, false, &); // 010
|
||||
__OFATS_ANY_INVOCABLE(, &, true, &); // 011
|
||||
__OFATS_ANY_INVOCABLE(, &&, false, &&); // 020
|
||||
__OFATS_ANY_INVOCABLE(, &&, true, &&); // 021
|
||||
__OFATS_ANY_INVOCABLE(const, , false, const&); // 100
|
||||
__OFATS_ANY_INVOCABLE(const, , true, const&); // 101
|
||||
__OFATS_ANY_INVOCABLE(const, &, false, const&); // 110
|
||||
__OFATS_ANY_INVOCABLE(const, &, true, const&); // 111
|
||||
__OFATS_ANY_INVOCABLE(const, &&, false, const&&); // 120
|
||||
__OFATS_ANY_INVOCABLE(const, &&, true, const&&); // 121
|
||||
|
||||
#undef __OFATS_ANY_INVOCABLE
|
||||
|
||||
} // namespace ofats
|
||||
|
||||
/* We, uWebSockets define our own type */
|
||||
namespace uWS {
|
||||
template <class T>
|
||||
using MoveOnlyFunction = ofats::any_invocable<T>;
|
||||
}
|
||||
|
||||
#endif // _ANY_INVOKABLE_H_
|
||||
+1
-1
@@ -28,7 +28,7 @@
|
||||
#include <list>
|
||||
#include <cstring>
|
||||
|
||||
/* We use std::function here, not fu2::unique_function */
|
||||
/* We use std::function here, not MoveOnlyFunction */
|
||||
#include <functional>
|
||||
|
||||
namespace uWS {
|
||||
|
||||
+1
-1
@@ -176,7 +176,7 @@ public:
|
||||
}
|
||||
|
||||
/* Corks the response if possible. Leaves already corked socket be. */
|
||||
void cork(fu2::unique_function<void()> &&handler) {
|
||||
void cork(MoveOnlyFunction<void()> &&handler) {
|
||||
if (!Super::isCorked() && Super::canCork()) {
|
||||
Super::cork();
|
||||
handler();
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
#ifndef UWS_WEBSOCKETCONTEXTDATA_H
|
||||
#define UWS_WEBSOCKETCONTEXTDATA_H
|
||||
|
||||
#include "f2/function2.hpp"
|
||||
#include "MoveOnlyFunction.h"
|
||||
#include <string_view>
|
||||
|
||||
#include "WebSocketProtocol.h"
|
||||
@@ -47,13 +47,13 @@ private:
|
||||
|
||||
public:
|
||||
/* The callbacks for this context */
|
||||
fu2::unique_function<void(uWS::WebSocket<SSL, true> *)> openHandler = nullptr;
|
||||
fu2::unique_function<void(WebSocket<SSL, true> *, std::string_view, uWS::OpCode)> messageHandler = nullptr;
|
||||
fu2::unique_function<void(WebSocket<SSL, true> *)> drainHandler = nullptr;
|
||||
fu2::unique_function<void(WebSocket<SSL, true> *, int, std::string_view)> closeHandler = nullptr;
|
||||
MoveOnlyFunction<void(uWS::WebSocket<SSL, true> *)> openHandler = nullptr;
|
||||
MoveOnlyFunction<void(WebSocket<SSL, true> *, std::string_view, uWS::OpCode)> messageHandler = nullptr;
|
||||
MoveOnlyFunction<void(WebSocket<SSL, true> *)> drainHandler = nullptr;
|
||||
MoveOnlyFunction<void(WebSocket<SSL, true> *, int, std::string_view)> closeHandler = nullptr;
|
||||
/* Todo: these should take message also; breaking change for v0.18 */
|
||||
fu2::unique_function<void(WebSocket<SSL, true> *)> pingHandler = nullptr;
|
||||
fu2::unique_function<void(WebSocket<SSL, true> *)> pongHandler = nullptr;
|
||||
MoveOnlyFunction<void(WebSocket<SSL, true> *)> pingHandler = nullptr;
|
||||
MoveOnlyFunction<void(WebSocket<SSL, true> *)> pongHandler = nullptr;
|
||||
|
||||
/* Settings for this context */
|
||||
size_t maxPayloadLength = 0;
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
Boost Software License - Version 1.0 - August 17th, 2003
|
||||
|
||||
Permission is hereby granted, free of charge, to any person or organization
|
||||
obtaining a copy of the software and accompanying documentation covered by
|
||||
this license (the "Software") to use, reproduce, display, distribute,
|
||||
execute, and transmit the Software, and to prepare derivative works of the
|
||||
Software, and to permit third-parties to whom the Software is furnished to
|
||||
do so, all subject to the following:
|
||||
|
||||
The copyright notices in the Software and this entire statement, including
|
||||
the above license grant, this restriction and the following disclaimer,
|
||||
must be included in all copies of the Software, in whole or in part, and
|
||||
all derivative works of the Software, unless such copies or derivative
|
||||
works are solely in the form of machine-executable object code generated by
|
||||
a source language processor.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user