From 4f458b40c39d7c55814da64bcbbe7e1ba6100285 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Fri, 12 Feb 2016 00:01:13 +0100 Subject: [PATCH] Use slots instead of simple callbacks. Need to wait till my second pull request gets accepted though, to be able to compile the code. --- src/Components/Modules/AssetHandler.cpp | 6 +++--- src/Components/Modules/AssetHandler.hpp | 6 +++--- src/Components/Modules/Command.cpp | 24 ++++++++++++------------ src/Components/Modules/Command.hpp | 7 +++---- src/Components/Modules/Network.cpp | 6 +++--- src/Components/Modules/Network.hpp | 6 +++--- src/Components/Modules/UIScript.cpp | 12 ++++++------ src/Components/Modules/UIScript.hpp | 14 +++++++------- 8 files changed, 40 insertions(+), 41 deletions(-) diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index 3a443a80..06094078 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -4,7 +4,7 @@ namespace Components { bool AssetHandler::BypassState = false; std::map AssetHandler::AssetInterfaces; - std::map AssetHandler::TypeCallbacks; + std::map> AssetHandler::TypeCallbacks; wink::signal> AssetHandler::RestrictSignal; std::map AssetHandler::Relocations; @@ -140,12 +140,12 @@ namespace Components } } - void AssetHandler::OnFind(Game::XAssetType type, AssetHandler::Callback callback) + void AssetHandler::OnFind(Game::XAssetType type, AssetHandler::Callback* callback) { AssetHandler::TypeCallbacks[type] = callback; } - void AssetHandler::OnLoad(RestrictCallback* callback) + void AssetHandler::OnLoad(AssetHandler::RestrictCallback* callback) { AssetHandler::RestrictSignal.connect(callback); } diff --git a/src/Components/Modules/AssetHandler.hpp b/src/Components/Modules/AssetHandler.hpp index ad0fdc36..469316ec 100644 --- a/src/Components/Modules/AssetHandler.hpp +++ b/src/Components/Modules/AssetHandler.hpp @@ -13,14 +13,14 @@ namespace Components virtual void Load(Game::XAssetHeader* header, std::string name, ZoneBuilder::Zone* builder) { /*ErrorTypeNotSupported(this);*/ }; }; - typedef Game::XAssetHeader(*Callback)(Game::XAssetType type, std::string name); + typedef Game::XAssetHeader(Callback)(Game::XAssetType type, std::string name); typedef void(RestrictCallback)(Game::XAssetType type, Game::XAssetHeader asset, std::string name, bool* restrict); AssetHandler(); ~AssetHandler(); const char* GetName() { return "AssetHandler"; }; - static void OnFind(Game::XAssetType type, Callback callback); + static void OnFind(Game::XAssetType type, Callback* callback); static void OnLoad(RestrictCallback* callback); static void Relocate(void* start, void* to, DWORD size = 4); @@ -49,7 +49,7 @@ namespace Components static std::map TemporaryAssets[Game::XAssetType::ASSET_TYPE_COUNT]; static std::map AssetInterfaces; - static std::map TypeCallbacks; + static std::map> TypeCallbacks; static wink::signal> RestrictSignal; static std::map Relocations; diff --git a/src/Components/Modules/Command.cpp b/src/Components/Modules/Command.cpp index 9978f826..0b76bdcd 100644 --- a/src/Components/Modules/Command.cpp +++ b/src/Components/Modules/Command.cpp @@ -3,7 +3,7 @@ namespace Components { std::vector Command::Functions; - std::map Command::FunctionMap; + std::map> Command::FunctionMap; char* Command::Params::operator[](size_t index) { @@ -20,17 +20,7 @@ namespace Components return Game::cmd_argc[this->CommandId]; } - Command::~Command() - { - for (auto command : Command::Functions) - { - delete command; - } - - Command::Functions.clear(); - } - - void Command::Add(const char* name, Command::Callback callback) + void Command::Add(const char* name, Command::Callback* callback) { Command::FunctionMap[Utils::StrToLower(name)] = callback; Game::Cmd_AddCommand(name, Command::MainCallback, Command::Allocate(), 0); @@ -74,4 +64,14 @@ namespace Components { // TODO: Add commands here? } + + Command::~Command() + { + for (auto command : Command::Functions) + { + delete command; + } + + Command::Functions.clear(); + } } diff --git a/src/Components/Modules/Command.hpp b/src/Components/Modules/Command.hpp index 6d173e62..9fac3c70 100644 --- a/src/Components/Modules/Command.hpp +++ b/src/Components/Modules/Command.hpp @@ -17,20 +17,19 @@ namespace Components DWORD CommandId; }; - typedef void(*Callback)(Command::Params params); + typedef void(Callback)(Command::Params params); Command(); ~Command(); const char* GetName() { return "Command"; }; - static void Add(const char* name, Callback callback); - + static void Add(const char* name, Callback* callback); static void Execute(std::string command, bool sync = true); private: static Game::cmd_function_t* Allocate(); static std::vector Functions; - static std::map FunctionMap; + static std::map> FunctionMap; static void MainCallback(); }; } diff --git a/src/Components/Modules/Network.cpp b/src/Components/Modules/Network.cpp index 7076e334..b82d6d6a 100644 --- a/src/Components/Modules/Network.cpp +++ b/src/Components/Modules/Network.cpp @@ -3,7 +3,7 @@ namespace Components { std::string Network::SelectedPacket; - std::map Network::PacketHandlers; + std::map> Network::PacketHandlers; Network::Address::Address(std::string addrString) { @@ -16,7 +16,7 @@ namespace Components void Network::Address::SetPort(unsigned short port) { this->address.port = htons(port); - }; + } unsigned short Network::Address::GetPort() { return ntohs(this->address.port); @@ -82,7 +82,7 @@ namespace Components return false; } - void Network::Handle(std::string packet, Network::Callback callback) + void Network::Handle(std::string packet, Network::Callback* callback) { Network::PacketHandlers[Utils::StrToLower(packet)] = callback; } diff --git a/src/Components/Modules/Network.hpp b/src/Components/Modules/Network.hpp index 85f5f8f2..2efbe26b 100644 --- a/src/Components/Modules/Network.hpp +++ b/src/Components/Modules/Network.hpp @@ -34,13 +34,13 @@ namespace Components Game::netadr_t address; }; - typedef void(*Callback)(Address address, std::string data); + typedef void(Callback)(Address address, std::string data); Network(); ~Network(); const char* GetName() { return "Network"; }; - static void Handle(std::string packet, Callback callback); + static void Handle(std::string packet, Callback* callback); // Send quake-styled binary data static void Send(Address target, std::string data); @@ -61,7 +61,7 @@ namespace Components private: static SOCKET TcpSocket; static std::string SelectedPacket; - static std::map PacketHandlers; + static std::map> PacketHandlers; static int PacketInterceptionHandler(const char* packet); static void DeployPacket(Game::netadr_t* from, Game::msg_t* msg); static void DeployPacketStub(); diff --git a/src/Components/Modules/UIScript.cpp b/src/Components/Modules/UIScript.cpp index 18479378..9f72bbc3 100644 --- a/src/Components/Modules/UIScript.cpp +++ b/src/Components/Modules/UIScript.cpp @@ -2,8 +2,8 @@ namespace Components { - std::map UIScript::UIScripts; - std::map UIScript::UIOwnerDraws; + std::map> UIScript::UIScripts; + std::map> UIScript::UIOwnerDraws; template<> int UIScript::Token::Get() { @@ -48,17 +48,17 @@ namespace Components } } - void UIScript::Add(std::string name, UIScript::Callback callback) + void UIScript::Add(std::string name, UIScript::Callback* callback) { UIScript::UIScripts[name] = callback; } - void UIScript::Add(std::string name, UIScript::CallbackRaw callback) + void UIScript::Add(std::string name, UIScript::CallbackRaw* callback) { - UIScript::Add(name, reinterpret_cast(callback)); + UIScript::Add(name, reinterpret_cast(callback)); } - void UIScript::AddOwnerDraw(int ownerdraw, UIScript::CallbackRaw callback) + void UIScript::AddOwnerDraw(int ownerdraw, UIScript::CallbackRaw* callback) { UIScript::UIOwnerDraws[ownerdraw] = callback; } diff --git a/src/Components/Modules/UIScript.hpp b/src/Components/Modules/UIScript.hpp index d6641add..43ec341e 100644 --- a/src/Components/Modules/UIScript.hpp +++ b/src/Components/Modules/UIScript.hpp @@ -23,20 +23,20 @@ namespace Components void Parse(const char** args); }; - typedef void(*Callback)(Token token); - typedef void(*CallbackRaw)(); + typedef void(Callback)(Token token); + typedef void(CallbackRaw)(); - static void Add(std::string name, Callback callback); - static void Add(std::string name, CallbackRaw callback); + static void Add(std::string name, Callback* callback); + static void Add(std::string name, CallbackRaw* callback); - static void AddOwnerDraw(int ownerdraw, CallbackRaw callback); + static void AddOwnerDraw(int ownerdraw, CallbackRaw* callback); private: static void OwnerDrawHandleKeyStub(int ownerDraw, int flags, float *special, int key); static bool RunMenuScript(const char* name, const char** args); static void RunMenuScriptStub(); - static std::map UIScripts; - static std::map UIOwnerDraws; + static std::map> UIScripts; + static std::map> UIOwnerDraws; }; }