std::string_view 的使用
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <drogon/HttpAppFramework.h>
|
||||
#include <drogon/WebSocketController.h>
|
||||
#include <unordered_set>
|
||||
#include <string_view>
|
||||
class DSP_Ws_Mgr : public drogon::WebSocketController<DSP_Ws_Mgr> {
|
||||
public:
|
||||
static DSP_Ws_Mgr *instance() { return inst_; }
|
||||
@@ -26,8 +27,8 @@ struct WebSocket_Connection {
|
||||
std::string type;
|
||||
};
|
||||
|
||||
WebSocket_Manager::WebSocket_Manager(std::string path)
|
||||
: path(std::move(path)) {}
|
||||
WebSocket_Manager::WebSocket_Manager(std::string_view path)
|
||||
: path(path) {}
|
||||
void WebSocket_Manager::remove(const drogon::WebSocketConnectionPtr &conn) {
|
||||
std::lock_guard<std::mutex> g(mtx);
|
||||
connections.erase(conn);
|
||||
@@ -55,20 +56,21 @@ size_t WebSocket_Manager::get_websocket_connect_num() {
|
||||
return connections.size();
|
||||
}
|
||||
|
||||
std::shared_ptr<WebSocket_Manager> Ws_Mgr::register_ws(std::string path) {
|
||||
auto it = map.find(path);
|
||||
std::shared_ptr<WebSocket_Manager> Ws_Mgr::register_ws(std::string_view path) {
|
||||
auto path_string = std::string(path);
|
||||
auto it = map.find(path_string);
|
||||
if (it != map.end()) {
|
||||
Psc::fail_fast_core_dump("register_ws:" + VAR_STR_1(path) + "重复创建!");
|
||||
Psc::fail_fast_core_dump("register_ws:" + VAR_STR_1(path_string) + "重复创建!");
|
||||
return it->second;
|
||||
}
|
||||
std::shared_ptr<WebSocket_Manager> cur =
|
||||
std::make_shared<WebSocket_Manager>(path);
|
||||
map[path] = cur;
|
||||
std::make_shared<WebSocket_Manager>(path_string);
|
||||
map[path_string] = cur;
|
||||
return cur;
|
||||
}
|
||||
|
||||
std::shared_ptr<WebSocket_Manager> Ws_Mgr::get_ws(std::string path) {
|
||||
auto it = map.find(path);
|
||||
std::shared_ptr<WebSocket_Manager> Ws_Mgr::get_ws(std::string_view path) {
|
||||
auto it = map.find(std::string(path));
|
||||
if (it != map.end()) {
|
||||
return it->second;
|
||||
}
|
||||
@@ -111,4 +113,4 @@ void DSP_Ws_Mgr::handleNewConnection(
|
||||
conn->setContext(std::make_shared<WebSocket_Connection>(std::move(s)));
|
||||
ws->add(conn);
|
||||
}
|
||||
DSP_Ws_Mgr::DSP_Ws_Mgr() { inst_ = this; }
|
||||
DSP_Ws_Mgr::DSP_Ws_Mgr() { inst_ = this; }
|
||||
|
||||
Reference in New Issue
Block a user