summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/command/commandHandler.cpp2
-rw-r--r--src/command/commandHandler.hpp14
-rw-r--r--src/core/chatManager.hpp6
-rw-r--r--src/core/chatServer.hpp4
-rw-r--r--src/core/server.cpp2
-rw-r--r--src/core/server.hpp8
-rw-r--r--src/core/session.cpp2
-rw-r--r--src/core/session.hpp10
-rw-r--r--src/core/sessionManager.hpp16
-rw-r--r--src/io/IOManager.hpp2
-rw-r--r--src/io/inputManager.hpp4
-rw-r--r--src/io/renderManager.hpp4
-rw-r--r--src/io/scroller.cpp2
-rw-r--r--src/misc/memory.hpp10
14 files changed, 48 insertions, 38 deletions
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 <algorithm>
-#include <memory>
+#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 <any>
#include <functional>
-#include <memory>
+#include "../misc/memory.hpp"
#include <netinet/in.h>
#include <string>
#include <vector>
@@ -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<CSession> session) const;
- void handleCommand(std::string input, std::shared_ptr<CSession> user = nullptr) const;
+ SResult newCommand(const std::string &command, const std::string &args, SP<CSession> session) const;
+ void handleCommand(std::string input, SP<CSession> user = nullptr) const;
template <typename T>
struct SMakeCommandArgs {
@@ -63,9 +63,9 @@ class CCommandHandler {
}
private:
- std::shared_ptr<CCommandHandler::SCommand> getCommand(const std::string &command) const;
- SResult exeCommand(const std::shared_ptr<SCommand> command, const std::string &args) const;
+ SP<CCommandHandler::SCommand> getCommand(const std::string &command) const;
+ SResult exeCommand(const SP<SCommand> command, const std::string &args) const;
- std::vector<std::shared_ptr<SCommand>> m_vCommands;
+ std::vector<SP<SCommand>> m_vCommands;
};
-inline std::unique_ptr<CCommandHandler> g_pCommandHandler;
+inline UP<CCommandHandler> 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 <memory>
+#include "../misc/memory.hpp"
#include <optional>
#include <string>
#include <vector>
@@ -14,7 +14,7 @@ class CChatManager {
struct SMessage {
std::string msg;
std::string username;
- std::optional<std::weak_ptr<CSession>> sender = std::nullopt;
+ std::optional<WP<CSession>> sender = std::nullopt;
bool admin = false;
};
void newMessage(const SMessage &msg);
@@ -22,4 +22,4 @@ class CChatManager {
std::vector<SMessage> getChat() const;
};
-inline std::unique_ptr<CChatManager> g_pChatManager;
+inline UP<CChatManager> 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 <memory>
+#include "../misc/memory.hpp"
#include <thread>
class CChatServer {
@@ -17,4 +17,4 @@ class CChatServer {
std::jthread m_sessionManagerThread;
};
-inline std::unique_ptr<CChatServer> g_pChatServer;
+inline UP<CChatServer> 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 <cstring>
-#include <memory>
+#include "../misc/memory.hpp"
#include <sys/socket.h>
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 <memory>
+#include "../misc/memory.hpp"
#include <netinet/in.h>
class CServer {
public:
CServer(uint16_t port);
~CServer();
- std::shared_ptr<CFileDescriptor> getSocket() const;
+ SP<CFileDescriptor> getSocket() const;
private:
- std::shared_ptr<CFileDescriptor> m_sockfd;
+ SP<CFileDescriptor> m_sockfd;
sockaddr_in m_addr;
mutable std::mutex m_mutex;
};
-inline std::unique_ptr<CServer> g_pServer;
+inline UP<CServer> 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 <arpa/inet.h>
#include <cerrno>
#include <cstring>
-#include <memory>
+#include "../misc/memory.hpp"
#include <optional>
#include <sys/socket.h>
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 <format>
-#include <memory>
+#include "../misc/memory.hpp"
#include <netinet/in.h>
#include <optional>
#include <string>
@@ -31,8 +31,8 @@ class CSession {
const std::string &getIp() const;
bool isAdmin() const;
- std::unique_ptr<SRecvData> read();
- std::unique_ptr<SRecvData> read(const std::string &msg, bool bypassDeaf = false);
+ UP<SRecvData> read();
+ UP<SRecvData> read(const std::string &msg, bool bypassDeaf = false);
bool write(const std::string &msg);
template <typename... Args>
bool write(std::format_string<Args...> fmt, Args &&...args);
@@ -57,8 +57,8 @@ class CSession {
WRITE
};
- std::weak_ptr<CSession> self;
- std::shared_ptr<CFileDescriptor> m_sockfd;
+ WP<CSession> self;
+ SP<CFileDescriptor> 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 <memory>
#include <thread>
#include <vector>
@@ -15,22 +15,22 @@ class CSessionManager {
void newSession();
void run();
- void broadcast(const std::string &msg, std::optional<std::weak_ptr<CSession>> self = std::nullopt) const; // second param only specified when we want to exclude the sender
- void kick(std::weak_ptr<CSession> session, const bool kill = false, const std::string &reason = "");
+ void broadcast(const std::string &msg, std::optional<WP<CSession>> self = std::nullopt) const; // second param only specified when we want to exclude the sender
+ void kick(WP<CSession> 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<CSession> getByName(const std::string &name) const;
- std::shared_ptr<CSession> getByIp(const std::string &ip) const;
- std::vector<std::shared_ptr<CSession>> getSessions() const;
+ SP<CSession> getByName(const std::string &name) const;
+ SP<CSession> getByIp(const std::string &ip) const;
+ std::vector<SP<CSession>> getSessions() const;
private:
void broadcast(const CChatManager::SMessage &msg) const;
- std::vector<std::pair<std::jthread, std::shared_ptr<CSession>>> m_vSessions;
+ std::vector<std::pair<std::jthread, SP<CSession>>> m_vSessions;
friend class CChatManager;
};
-inline std::shared_ptr<CSessionManager> g_pSessionManager;
+inline SP<CSessionManager> 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<CIOManager> g_pIOManager;
+inline UP<CIOManager> 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 <ftxui/component/component.hpp>
-#include <memory>
+#include "../misc/memory.hpp"
class CInputManager {
public:
@@ -22,4 +22,4 @@ class CInputManager {
int bottomSize = 1;
};
-inline std::unique_ptr<CInputManager> g_pInputManager;
+inline UP<CInputManager> 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 <ftxui/component/screen_interactive.hpp>
-#include <memory>
+#include "../misc/memory.hpp"
class CRenderManager {
public:
@@ -20,4 +20,4 @@ class CRenderManager {
friend class CInputManager;
};
-inline std::unique_ptr<CRenderManager> g_pRenderManager;
+inline UP<CRenderManager> 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 <algorithm> // for max, min
#include <ftxui/component/component_base.hpp> // for Component, ComponentBase
#include <ftxui/component/event.hpp> // for Event, Event::ArrowDown, Event::ArrowUp, Event::End, Event::Home, Event::PageDown, Event::PageUp
-#include <memory> // for shared_ptr, allocator, __shared_ptr_access
+#include "../misc/memory.hpp" // for shared_ptr, allocator, __shared_ptr_access
#include <utility> // 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 <memory>
+
+template <typename T>
+using SP = std::shared_ptr<T>;
+template <typename T>
+using WP = std::weak_ptr<T>;
+template <typename T>
+using UP = std::unique_ptr<T>;