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;
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;
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;
}
void AssetHandler::OnLoad(RestrictCallback* callback)
void AssetHandler::OnLoad(AssetHandler::RestrictCallback* 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);*/ };
};
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<std::string, Game::XAssetHeader> TemporaryAssets[Game::XAssetType::ASSET_TYPE_COUNT];
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 std::map<void*, void*> Relocations;

View File

@ -3,7 +3,7 @@
namespace Components
{
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)
{
@ -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();
}
}

View File

@ -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<Game::cmd_function_t*> Functions;
static std::map<std::string, Callback> FunctionMap;
static std::map<std::string, wink::slot<Callback>> FunctionMap;
static void MainCallback();
};
}

View File

@ -3,7 +3,7 @@
namespace Components
{
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)
{
@ -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;
}

View File

@ -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<std::string, Callback> PacketHandlers;
static std::map<std::string, wink::slot<Callback>> PacketHandlers;
static int PacketInterceptionHandler(const char* packet);
static void DeployPacket(Game::netadr_t* from, Game::msg_t* msg);
static void DeployPacketStub();

View File

@ -2,8 +2,8 @@
namespace Components
{
std::map<std::string, UIScript::Callback> UIScript::UIScripts;
std::map<int, UIScript::CallbackRaw> UIScript::UIOwnerDraws;
std::map<std::string, wink::slot<UIScript::Callback>> UIScript::UIScripts;
std::map<int, wink::slot<UIScript::CallbackRaw>> 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<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;
}

View File

@ -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<std::string, Callback> UIScripts;
static std::map<int, CallbackRaw> UIOwnerDraws;
static std::map<std::string, wink::slot<Callback>> UIScripts;
static std::map<int, wink::slot<CallbackRaw>> UIOwnerDraws;
};
}