From 8ee8e55ff2b085762dad5ab41269448e7a91757f Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Fri, 13 Jun 2025 20:19:22 +0800 Subject: internal: shortened smart pointer types --- src/command/commandHandler.cpp | 2 +- src/command/commandHandler.hpp | 14 +++++++------- src/core/chatManager.hpp | 6 +++--- src/core/chatServer.hpp | 4 ++-- src/core/server.cpp | 2 +- src/core/server.hpp | 8 ++++---- src/core/session.cpp | 2 +- src/core/session.hpp | 10 +++++----- src/core/sessionManager.hpp | 16 ++++++++-------- src/io/IOManager.hpp | 2 +- src/io/inputManager.hpp | 4 ++-- src/io/renderManager.hpp | 4 ++-- src/io/scroller.cpp | 2 +- src/misc/memory.hpp | 10 ++++++++++ 14 files changed, 48 insertions(+), 38 deletions(-) create mode 100644 src/misc/memory.hpp diff --git a/src/command/commandHandler.cpp b/src/command/commandHandler.cpp index 4aa6723..3baeeb1 100644 --- a/src/command/commandHandler.cpp +++ b/src/command/commandHandler.cpp @@ -2,7 +2,7 @@ #include "../debug/log.hpp" #include "../core/session.hpp" #include -#include +#include "../misc/memory.hpp" CCommandHandler::CCommandHandler() { log(LOG, "CommandHandler: initialized"); diff --git a/src/command/commandHandler.hpp b/src/command/commandHandler.hpp index 7fc20d5..5a8636f 100644 --- a/src/command/commandHandler.hpp +++ b/src/command/commandHandler.hpp @@ -3,7 +3,7 @@ #include "../core/session.hpp" #include #include -#include +#include "../misc/memory.hpp" #include #include #include @@ -34,8 +34,8 @@ class CCommandHandler { bool validCommand(const std::string &command) const; // return false if command struct is invalid bool registerCommand(const SCommand &command); - SResult newCommand(const std::string &command, const std::string &args, std::shared_ptr session) const; - void handleCommand(std::string input, std::shared_ptr user = nullptr) const; + SResult newCommand(const std::string &command, const std::string &args, SP session) const; + void handleCommand(std::string input, SP user = nullptr) const; template struct SMakeCommandArgs { @@ -63,9 +63,9 @@ class CCommandHandler { } private: - std::shared_ptr getCommand(const std::string &command) const; - SResult exeCommand(const std::shared_ptr command, const std::string &args) const; + SP getCommand(const std::string &command) const; + SResult exeCommand(const SP command, const std::string &args) const; - std::vector> m_vCommands; + std::vector> m_vCommands; }; -inline std::unique_ptr g_pCommandHandler; +inline UP g_pCommandHandler; diff --git a/src/core/chatManager.hpp b/src/core/chatManager.hpp index bd305de..d109424 100644 --- a/src/core/chatManager.hpp +++ b/src/core/chatManager.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include "../misc/memory.hpp" #include #include #include @@ -14,7 +14,7 @@ class CChatManager { struct SMessage { std::string msg; std::string username; - std::optional> sender = std::nullopt; + std::optional> sender = std::nullopt; bool admin = false; }; void newMessage(const SMessage &msg); @@ -22,4 +22,4 @@ class CChatManager { std::vector getChat() const; }; -inline std::unique_ptr g_pChatManager; +inline UP g_pChatManager; diff --git a/src/core/chatServer.hpp b/src/core/chatServer.hpp index 00d2979..806030a 100644 --- a/src/core/chatServer.hpp +++ b/src/core/chatServer.hpp @@ -1,4 +1,4 @@ -#include +#include "../misc/memory.hpp" #include class CChatServer { @@ -17,4 +17,4 @@ class CChatServer { std::jthread m_sessionManagerThread; }; -inline std::unique_ptr g_pChatServer; +inline UP g_pChatServer; diff --git a/src/core/server.cpp b/src/core/server.cpp index a78c217..10eb4dd 100644 --- a/src/core/server.cpp +++ b/src/core/server.cpp @@ -1,7 +1,7 @@ #include "server.hpp" #include "../debug/log.hpp" #include -#include +#include "../misc/memory.hpp" #include CServer::CServer(uint16_t port) diff --git a/src/core/server.hpp b/src/core/server.hpp index c42192b..ea0e15b 100644 --- a/src/core/server.hpp +++ b/src/core/server.hpp @@ -1,19 +1,19 @@ #pragma once #include "../misc/FileDescriptor.hpp" -#include +#include "../misc/memory.hpp" #include class CServer { public: CServer(uint16_t port); ~CServer(); - std::shared_ptr getSocket() const; + SP getSocket() const; private: - std::shared_ptr m_sockfd; + SP m_sockfd; sockaddr_in m_addr; mutable std::mutex m_mutex; }; -inline std::unique_ptr g_pServer; +inline UP g_pServer; diff --git a/src/core/session.cpp b/src/core/session.cpp index 7b0b459..c8cceb0 100644 --- a/src/core/session.cpp +++ b/src/core/session.cpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include "../misc/memory.hpp" #include #include diff --git a/src/core/session.hpp b/src/core/session.hpp index ca951d1..9a953fc 100644 --- a/src/core/session.hpp +++ b/src/core/session.hpp @@ -4,7 +4,7 @@ #include "chatManager.hpp" #include "../debug/format.hpp" #include -#include +#include "../misc/memory.hpp" #include #include #include @@ -31,8 +31,8 @@ class CSession { const std::string &getIp() const; bool isAdmin() const; - std::unique_ptr read(); - std::unique_ptr read(const std::string &msg, bool bypassDeaf = false); + UP read(); + UP read(const std::string &msg, bool bypassDeaf = false); bool write(const std::string &msg); template bool write(std::format_string fmt, Args &&...args); @@ -57,8 +57,8 @@ class CSession { WRITE }; - std::weak_ptr self; - std::shared_ptr m_sockfd; + WP self; + SP m_sockfd; sockaddr_in m_addr; socklen_t m_addrLen = sizeof(m_addr); std::string m_name; diff --git a/src/core/sessionManager.hpp b/src/core/sessionManager.hpp index 71bcc5f..e84226c 100644 --- a/src/core/sessionManager.hpp +++ b/src/core/sessionManager.hpp @@ -1,9 +1,9 @@ #pragma once #include "../debug/format.hpp" +#include "../misc/memory.hpp" #include "chatManager.hpp" #include "session.hpp" -#include #include #include @@ -15,22 +15,22 @@ class CSessionManager { void newSession(); void run(); - void broadcast(const std::string &msg, std::optional> self = std::nullopt) const; // second param only specified when we want to exclude the sender - void kick(std::weak_ptr session, const bool kill = false, const std::string &reason = ""); + void broadcast(const std::string &msg, std::optional> self = std::nullopt) const; // second param only specified when we want to exclude the sender + void kick(WP session, const bool kill = false, const std::string &reason = ""); void addSession(const CSession &session); bool nameExists(const std::string &name); void shutdownSessions(); - std::shared_ptr getByName(const std::string &name) const; - std::shared_ptr getByIp(const std::string &ip) const; - std::vector> getSessions() const; + SP getByName(const std::string &name) const; + SP getByIp(const std::string &ip) const; + std::vector> getSessions() const; private: void broadcast(const CChatManager::SMessage &msg) const; - std::vector>> m_vSessions; + std::vector>> m_vSessions; friend class CChatManager; }; -inline std::shared_ptr g_pSessionManager; +inline SP g_pSessionManager; diff --git a/src/io/IOManager.hpp b/src/io/IOManager.hpp index d1d891d..95eb27b 100644 --- a/src/io/IOManager.hpp +++ b/src/io/IOManager.hpp @@ -30,4 +30,4 @@ class CIOManager { friend class CChatManager; }; -inline std::unique_ptr g_pIOManager; +inline UP g_pIOManager; diff --git a/src/io/inputManager.hpp b/src/io/inputManager.hpp index a81e39d..a794ae9 100644 --- a/src/io/inputManager.hpp +++ b/src/io/inputManager.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include "../misc/memory.hpp" class CInputManager { public: @@ -22,4 +22,4 @@ class CInputManager { int bottomSize = 1; }; -inline std::unique_ptr g_pInputManager; +inline UP g_pInputManager; diff --git a/src/io/renderManager.hpp b/src/io/renderManager.hpp index 7a14cc0..3643f3c 100644 --- a/src/io/renderManager.hpp +++ b/src/io/renderManager.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include "../misc/memory.hpp" class CRenderManager { public: @@ -20,4 +20,4 @@ class CRenderManager { friend class CInputManager; }; -inline std::unique_ptr g_pRenderManager; +inline UP g_pRenderManager; diff --git a/src/io/scroller.cpp b/src/io/scroller.cpp index c595a39..9fd1427 100644 --- a/src/io/scroller.cpp +++ b/src/io/scroller.cpp @@ -4,7 +4,7 @@ #include // for max, min #include // for Component, ComponentBase #include // for Event, Event::ArrowDown, Event::ArrowUp, Event::End, Event::Home, Event::PageDown, Event::PageUp -#include // for shared_ptr, allocator, __shared_ptr_access +#include "../misc/memory.hpp" // for shared_ptr, allocator, __shared_ptr_access #include // for move #include "ftxui/component/component.hpp" // for Make diff --git a/src/misc/memory.hpp b/src/misc/memory.hpp new file mode 100644 index 0000000..a90368e --- /dev/null +++ b/src/misc/memory.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include + +template +using SP = std::shared_ptr; +template +using WP = std::weak_ptr; +template +using UP = std::unique_ptr; -- cgit v1.2.3