From 7c9fc12b63dd6cefd803b20b5ea097f8857a6288 Mon Sep 17 00:00:00 2001 From: Jordan Cristiano Date: Tue, 8 Mar 2016 23:14:13 -0500 Subject: [PATCH] Reduced code duplication --- demboyz/netmessages/nethandlers.cpp | 32 ++++++++++++----------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/demboyz/netmessages/nethandlers.cpp b/demboyz/netmessages/nethandlers.cpp index 155056d..8cd5d79 100644 --- a/demboyz/netmessages/nethandlers.cpp +++ b/demboyz/netmessages/nethandlers.cpp @@ -155,44 +155,38 @@ typedef bool (*NetMsgJsonReadFn)(NetHandlers::JsonRead& jsonbuf, SourceGameConte typedef bool (*NetMsgJsonWriteFn)(NetHandlers::JsonWrite& jsonbuf, const SourceGameContext& context, void* data); typedef void (*NetMsgToStringFn)(std::ostringstream& out, void* data); -bool NetHandlers::NetMsg_BitRead(uint32_t type, BitRead& bitbuf, SourceGameContext& context, void* data) +template +bool NetMsgFuncRunner(const FnType (&netHandlers)[NumHandlers], uint32_t type, BufType& buf, ContextType& context, void* data) { - static const NetMsgBitReadFn netHandlers[] = DECLARE_NET_HANDLER_ARRAY(BitRead); - if (type >= (sizeof(netHandlers) / sizeof(NetMsgBitReadFn))) + if (type >= NumHandlers) { return false; } - return netHandlers[type](bitbuf, context, data); + return netHandlers[type](buf, context, data); +} + +bool NetHandlers::NetMsg_BitRead(uint32_t type, BitRead& bitbuf, SourceGameContext& context, void* data) +{ + static const NetMsgBitReadFn netHandlers[] = DECLARE_NET_HANDLER_ARRAY(BitRead); + return NetMsgFuncRunner(netHandlers, type, bitbuf, context, data); } bool NetHandlers::NetMsg_BitWrite(uint32_t type, BitWrite& bitbuf, const SourceGameContext& context, void* data) { static const NetMsgBitWriteFn netHandlers[] = DECLARE_NET_HANDLER_ARRAY(BitWrite); - if (type >= (sizeof(netHandlers) / sizeof(NetMsgBitWriteFn))) - { - return false; - } - return netHandlers[type](bitbuf, context, data); + return NetMsgFuncRunner(netHandlers, type, bitbuf, context, data); } bool NetHandlers::NetMsg_JsonRead(uint32_t type, JsonRead& jsonbuf, SourceGameContext& context, void* data) { static const NetMsgJsonReadFn netHandlers[] = DECLARE_NET_HANDLER_ARRAY(JsonRead); - if (type >= (sizeof(netHandlers) / sizeof(NetMsgJsonReadFn))) - { - return false; - } - return netHandlers[type](jsonbuf, context, data); + return NetMsgFuncRunner(netHandlers, type, jsonbuf, context, data); } bool NetHandlers::NetMsg_JsonWrite(uint32_t type, JsonWrite& jsonbuf, const SourceGameContext& context, void* data) { static const NetMsgJsonWriteFn netHandlers[] = DECLARE_NET_HANDLER_ARRAY(JsonWrite); - if (type >= (sizeof(netHandlers) / sizeof(NetMsgJsonWriteFn))) - { - return false; - } - return netHandlers[type](jsonbuf, context, data); + return NetMsgFuncRunner(netHandlers, type, jsonbuf, context, data); } void NetHandlers::NetMsg_ToString(uint32_t type, std::ostringstream& out, void* data)