Use slots instead of simple callbacks.

Need to wait till my second pull request gets accepted though, to be able to compile the code.
This commit is contained in:
momo5502 2016-02-12 00:01:13 +01:00
parent 95e188cb7a
commit 4f458b40c3
8 changed files with 40 additions and 41 deletions

View File

@ -4,7 +4,7 @@ namespace Components
{ {
bool AssetHandler::BypassState = false; bool AssetHandler::BypassState = false;
std::map<Game::XAssetType, AssetHandler::IAsset*> AssetHandler::AssetInterfaces; std::map<Game::XAssetType, AssetHandler::IAsset*> AssetHandler::AssetInterfaces;
std::map<Game::XAssetType, AssetHandler::Callback> AssetHandler::TypeCallbacks; std::map<Game::XAssetType, wink::slot<AssetHandler::Callback>> AssetHandler::TypeCallbacks;
wink::signal<wink::slot<AssetHandler::RestrictCallback>> AssetHandler::RestrictSignal; wink::signal<wink::slot<AssetHandler::RestrictCallback>> AssetHandler::RestrictSignal;
std::map<void*, void*> AssetHandler::Relocations; std::map<void*, void*> 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; AssetHandler::TypeCallbacks[type] = callback;
} }
void AssetHandler::OnLoad(RestrictCallback* callback) void AssetHandler::OnLoad(AssetHandler::RestrictCallback* callback)
{ {
AssetHandler::RestrictSignal.connect(callback); AssetHandler::RestrictSignal.connect(callback);
} }

View File

@ -13,14 +13,14 @@ namespace Components
virtual void Load(Game::XAssetHeader* header, std::string name, ZoneBuilder::Zone* builder) { /*ErrorTypeNotSupported(this);*/ }; 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); typedef void(RestrictCallback)(Game::XAssetType type, Game::XAssetHeader asset, std::string name, bool* restrict);
AssetHandler(); AssetHandler();
~AssetHandler(); ~AssetHandler();
const char* GetName() { return "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 OnLoad(RestrictCallback* callback);
static void Relocate(void* start, void* to, DWORD size = 4); static void Relocate(void* start, void* to, DWORD size = 4);
@ -49,7 +49,7 @@ namespace Components
static std::map<std::string, Game::XAssetHeader> TemporaryAssets[Game::XAssetType::ASSET_TYPE_COUNT]; static std::map<std::string, Game::XAssetHeader> TemporaryAssets[Game::XAssetType::ASSET_TYPE_COUNT];
static std::map<Game::XAssetType, IAsset*> AssetInterfaces; static std::map<Game::XAssetType, IAsset*> AssetInterfaces;
static std::map<Game::XAssetType, Callback> TypeCallbacks; static std::map<Game::XAssetType, wink::slot<Callback>> TypeCallbacks;
static wink::signal<wink::slot<RestrictCallback>> RestrictSignal; static wink::signal<wink::slot<RestrictCallback>> RestrictSignal;
static std::map<void*, void*> Relocations; static std::map<void*, void*> Relocations;

View File

@ -3,7 +3,7 @@
namespace Components namespace Components
{ {
std::vector<Game::cmd_function_t*> Command::Functions; std::vector<Game::cmd_function_t*> Command::Functions;
std::map<std::string, Command::Callback> Command::FunctionMap; std::map<std::string, wink::slot<Command::Callback>> Command::FunctionMap;
char* Command::Params::operator[](size_t index) char* Command::Params::operator[](size_t index)
{ {
@ -20,17 +20,7 @@ namespace Components
return Game::cmd_argc[this->CommandId]; return Game::cmd_argc[this->CommandId];
} }
Command::~Command() void Command::Add(const char* name, Command::Callback* callback)
{
for (auto command : Command::Functions)
{
delete command;
}
Command::Functions.clear();
}
void Command::Add(const char* name, Command::Callback callback)
{ {
Command::FunctionMap[Utils::StrToLower(name)] = callback; Command::FunctionMap[Utils::StrToLower(name)] = callback;
Game::Cmd_AddCommand(name, Command::MainCallback, Command::Allocate(), 0); Game::Cmd_AddCommand(name, Command::MainCallback, Command::Allocate(), 0);
@ -74,4 +64,14 @@ namespace Components
{ {
// TODO: Add commands here? // TODO: Add commands here?
} }
Command::~Command()
{
for (auto command : Command::Functions)
{
delete command;
}
Command::Functions.clear();
}
} }

View File

@ -17,20 +17,19 @@ namespace Components
DWORD CommandId; DWORD CommandId;
}; };
typedef void(*Callback)(Command::Params params); typedef void(Callback)(Command::Params params);
Command(); Command();
~Command(); ~Command();
const char* GetName() { return "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); static void Execute(std::string command, bool sync = true);
private: private:
static Game::cmd_function_t* Allocate(); static Game::cmd_function_t* Allocate();
static std::vector<Game::cmd_function_t*> Functions; static std::vector<Game::cmd_function_t*> Functions;
static std::map<std::string, Callback> FunctionMap; static std::map<std::string, wink::slot<Callback>> FunctionMap;
static void MainCallback(); static void MainCallback();
}; };
} }

View File

@ -3,7 +3,7 @@
namespace Components namespace Components
{ {
std::string Network::SelectedPacket; std::string Network::SelectedPacket;
std::map<std::string, Network::Callback> Network::PacketHandlers; std::map<std::string, wink::slot<Network::Callback>> Network::PacketHandlers;
Network::Address::Address(std::string addrString) Network::Address::Address(std::string addrString)
{ {
@ -16,7 +16,7 @@ namespace Components
void Network::Address::SetPort(unsigned short port) void Network::Address::SetPort(unsigned short port)
{ {
this->address.port = htons(port); this->address.port = htons(port);
}; }
unsigned short Network::Address::GetPort() unsigned short Network::Address::GetPort()
{ {
return ntohs(this->address.port); return ntohs(this->address.port);
@ -82,7 +82,7 @@ namespace Components
return false; 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; Network::PacketHandlers[Utils::StrToLower(packet)] = callback;
} }

View File

@ -34,13 +34,13 @@ namespace Components
Game::netadr_t address; Game::netadr_t address;
}; };
typedef void(*Callback)(Address address, std::string data); typedef void(Callback)(Address address, std::string data);
Network(); Network();
~Network(); ~Network();
const char* GetName() { return "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 // Send quake-styled binary data
static void Send(Address target, std::string data); static void Send(Address target, std::string data);
@ -61,7 +61,7 @@ namespace Components
private: private:
static SOCKET TcpSocket; static SOCKET TcpSocket;
static std::string SelectedPacket; static std::string SelectedPacket;
static std::map<std::string, Callback> PacketHandlers; static std::map<std::string, wink::slot<Callback>> PacketHandlers;
static int PacketInterceptionHandler(const char* packet); static int PacketInterceptionHandler(const char* packet);
static void DeployPacket(Game::netadr_t* from, Game::msg_t* msg); static void DeployPacket(Game::netadr_t* from, Game::msg_t* msg);
static void DeployPacketStub(); static void DeployPacketStub();

View File

@ -2,8 +2,8 @@
namespace Components namespace Components
{ {
std::map<std::string, UIScript::Callback> UIScript::UIScripts; std::map<std::string, wink::slot<UIScript::Callback>> UIScript::UIScripts;
std::map<int, UIScript::CallbackRaw> UIScript::UIOwnerDraws; std::map<int, wink::slot<UIScript::CallbackRaw>> UIScript::UIOwnerDraws;
template<> int UIScript::Token::Get() 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; 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<UIScript::Callback>(callback)); UIScript::Add(name, reinterpret_cast<UIScript::Callback*>(callback));
} }
void UIScript::AddOwnerDraw(int ownerdraw, UIScript::CallbackRaw callback) void UIScript::AddOwnerDraw(int ownerdraw, UIScript::CallbackRaw* callback)
{ {
UIScript::UIOwnerDraws[ownerdraw] = callback; UIScript::UIOwnerDraws[ownerdraw] = callback;
} }

View File

@ -23,20 +23,20 @@ namespace Components
void Parse(const char** args); void Parse(const char** args);
}; };
typedef void(*Callback)(Token token); typedef void(Callback)(Token token);
typedef void(*CallbackRaw)(); typedef void(CallbackRaw)();
static void Add(std::string name, Callback callback); static void Add(std::string name, Callback* callback);
static void Add(std::string name, CallbackRaw callback); static void Add(std::string name, CallbackRaw* callback);
static void AddOwnerDraw(int ownerdraw, CallbackRaw callback); static void AddOwnerDraw(int ownerdraw, CallbackRaw* callback);
private: private:
static void OwnerDrawHandleKeyStub(int ownerDraw, int flags, float *special, int key); static void OwnerDrawHandleKeyStub(int ownerDraw, int flags, float *special, int key);
static bool RunMenuScript(const char* name, const char** args); static bool RunMenuScript(const char* name, const char** args);
static void RunMenuScriptStub(); static void RunMenuScriptStub();
static std::map<std::string, Callback> UIScripts; static std::map<std::string, wink::slot<Callback>> UIScripts;
static std::map<int, CallbackRaw> UIOwnerDraws; static std::map<int, wink::slot<CallbackRaw>> UIOwnerDraws;
}; };
} }