From 09a3cfa384f1e01528c556cb294b8c51f27657b5 Mon Sep 17 00:00:00 2001 From: Nathan Glenn Date: Sat, 26 Aug 2023 00:41:28 -0500 Subject: [PATCH] SQUASH LATER: remove pUserData in several places --- .vscode/launch.json | 2 +- Core/ClientSML/src/sml_ClientEvents.h | 27 ++++++++------- Core/ClientSML/src/sml_ClientKernel.cpp | 45 ++++++++++--------------- Core/ClientSML/src/sml_ClientKernel.h | 6 ++-- 4 files changed, 35 insertions(+), 45 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 882cd7bef3..53ba2399e7 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "program": "${workspaceFolder}/out/UnitTests", // "args": ["-e", "PRIMS_Sanity1", "-e", "PRIMS_Sanity2", "-f", "testSmemArithmetic", "-l", "-c", "FullTestsClientThread"], - "args": ["-e", "PRIMS_Sanity1","-t", "testRHSHandler"], + "args": ["-e", "PRIMS_Sanity1","-c", "MiscTests"], "stopAtEntry": false, "cwd": "${workspaceFolder}/out", "environment": [], diff --git a/Core/ClientSML/src/sml_ClientEvents.h b/Core/ClientSML/src/sml_ClientEvents.h index e335341d28..d16a735c0b 100644 --- a/Core/ClientSML/src/sml_ClientEvents.h +++ b/Core/ClientSML/src/sml_ClientEvents.h @@ -102,19 +102,19 @@ namespace sml typedef char const *(*RhsEventHandler)(smlRhsEventId id, void* pUserData, Agent* pAgent, char const* pFunctionName, char const* pArgument, int *buffSize, char *buff) ; - using RhsEventHandlerCPP = std::function; - -// Handler for a generic "client message". The content is determined by the client sending this data. -// The message is sent as a simple string and the response is also a string. The string can contain data that is intended to be parsed, -// such as a simple series of integers up to a complete XML message. -// SEE MAINTAINER NOTE ABOVE! - typedef char const *(*ClientMessageHandler)(smlRhsEventId id, void* pUserData, Agent* pAgent, - char const* pFunctionName, char const* pArgument, int *buffSize, char *buff) ; - -// We'll store a handler function together with a generic pointer to data of the user's choosing -// (which is then passed back into the handler when the event occurs). -// We also include a callback "id" which is a unique way to refer to this callback--used during unregistering. + using RhsEventHandlerCPP = std::function; + + // Handler for a generic "client message". The content is determined by the client sending this data. + // The message is sent as a simple string and the response is also a string. The string can contain data that is intended to be parsed, + // such as a simple series of integers up to a complete XML message. + // SEE MAINTAINER NOTE ABOVE! + // TODO: add alternative similar to RhsEventHandlerCPP above + typedef char const *(*ClientMessageHandler)(smlRhsEventId id, void *pUserData, Agent *pAgent, + char const *pFunctionName, char const *pArgument, int *buffSize, char *buff); + + // We'll store a handler function together with a generic pointer to data of the user's choosing + // (which is then passed back into the handler when the event occurs). + // We also include a callback "id" which is a unique way to refer to this callback--used during unregistering. class EventHandlerPlusData { public: @@ -142,6 +142,7 @@ namespace sml { return m_EventID ; } + // TODO: get rid of this in favor of storing in std::function captures void* getUserData() { return m_UserData ; diff --git a/Core/ClientSML/src/sml_ClientKernel.cpp b/Core/ClientSML/src/sml_ClientKernel.cpp index db7cb64d4b..ad704162f3 100644 --- a/Core/ClientSML/src/sml_ClientKernel.cpp +++ b/Core/ClientSML/src/sml_ClientKernel.cpp @@ -671,6 +671,8 @@ void Kernel::ReceivedRhsEvent(smlRhsEventId id, AnalyzeXML* pIncoming, ElementXM return ; } + // TODO: since we're only using the name, we could get rid of the handler+data wrapper class + // and iterate over name/handler key/vals here instead of looking up the name in the wrapper // Look up the handler(s) from the map RhsEventMap::ValueList* pHandlers = m_RhsEventMap.getList(pFunctionName) ; @@ -695,12 +697,10 @@ void Kernel::ReceivedRhsEvent(smlRhsEventId id, AnalyzeXML* pIncoming, ElementXM RhsEventHandlerPlusData handlerWithData = *iter ; - // RhsEventHandler handler = handlerWithData.m_Handler ; RhsEventHandlerCPP handler = handlerWithData.m_Handler ; - void* pUserData = handlerWithData.getUserData() ; // Call the handler - const std::string retVal = handler(id, pUserData, pAgent, pFunctionName, pArgument); + const std::string retVal = handler(id, pAgent, pFunctionName, pArgument); GetConnection()->AddSimpleResultToSMLResponse(pResponse, retVal.c_str()) ; } @@ -1855,16 +1855,12 @@ class Kernel::TestRhsCallbackFull : public RhsEventMap::ValueTest private: int m_EventID ; std::string m_FunctionName ; - RhsEventHandlerCPP m_Handler ; - void* m_UserData ; public: - TestRhsCallbackFull(int eventID, char const* functionName, RhsEventHandlerCPP handler, void* pUserData) + TestRhsCallbackFull(int eventID, char const* functionName) { m_EventID = eventID ; m_FunctionName = functionName ; - m_Handler = handler ; - m_UserData = pUserData ; } virtual ~TestRhsCallbackFull() { } ; @@ -1872,10 +1868,7 @@ class Kernel::TestRhsCallbackFull : public RhsEventMap::ValueTest bool isEqual(RhsEventHandlerPlusData handlerPlus) { return handlerPlus.m_FunctionName.compare(m_FunctionName) == 0 && - handlerPlus.m_EventID == m_EventID && - // TODO: did we really need this? Not possible with std::function - // handlerPlus.m_Handler == m_Handler && - handlerPlus.m_UserData == m_UserData ; + handlerPlus.m_EventID == m_EventID; } } ; @@ -2076,18 +2069,15 @@ int Kernel::RegisterForAgentEvent(smlAgentEventId id, AgentEventHandler handler, /*** *** RHS functions and message event handlers use the same internal logic, although they look rather different to the user ***/ -int Kernel::InternalAddRhsFunction(smlRhsEventId id, char const* pRhsFunctionName, RhsEventHandlerCPP handler, void* pUserData, bool addToBack) +int Kernel::InternalAddRhsFunction(smlRhsEventId id, char const* pRhsFunctionName, RhsEventHandlerCPP handler, bool addToBack) { - // Start by checking if this functionName, handler, pUSerData combination has already been registered - TestRhsCallbackFull test(id, pRhsFunctionName, handler, pUserData) ; - - // See if this handler is already registered - RhsEventHandlerPlusData plus(0, 0, 0, 0, 0) ; - bool found = m_RhsEventMap.findFirstValueByTest(&test, &plus) ; - - if (found && plus.m_Handler != 0) + // Start by checking if this event type and function namecombination has already been registered + TestRhsCallbackFull test(id, pRhsFunctionName) ; + RhsEventHandlerPlusData optionalFoundHandler(0, 0, 0, 0) ; + bool found = m_RhsEventMap.findFirstValueByTest(&test, &optionalFoundHandler) ; + if (found && optionalFoundHandler.m_Handler != 0) { - return plus.getCallbackID() ; + return optionalFoundHandler.getCallbackID() ; } // If we have no handlers registered with the kernel, then we need @@ -2105,7 +2095,7 @@ int Kernel::InternalAddRhsFunction(smlRhsEventId id, char const* pRhsFunctionNam // Record the handler m_CallbackIDCounter++ ; - RhsEventHandlerPlusData handlerPlus(id, pRhsFunctionName, handler, pUserData, m_CallbackIDCounter) ; + RhsEventHandlerPlusData handlerPlus(id, pRhsFunctionName, handler, m_CallbackIDCounter) ; m_RhsEventMap.add(pRhsFunctionName, handlerPlus, addToBack) ; // Return the ID. We use this later to unregister the callback @@ -2146,7 +2136,6 @@ bool Kernel::InternalRemoveRhsFunction(smlRhsEventId id, int callbackID) RhsEventHandlerCPP c2cppHandler(RhsEventHandler handler, void* pUserData) { return [handler, pUserData]( smlRhsEventId id, - void* pUserDataIgnored, Agent* pAgent, char const* pFunctionName, char const* pArgument) -> std::string { @@ -2203,14 +2192,14 @@ int Kernel::AddRhsFunction(char const* pRhsFunctionName, RhsEventHandler handler // TODO: remove separate pUserData everywhere. RhsEventHandlerCPP newHandler = c2cppHandler(handler, pUserData); - return InternalAddRhsFunction(id, pRhsFunctionName, newHandler, pUserData, addToBack) ; + return InternalAddRhsFunction(id, pRhsFunctionName, newHandler, addToBack) ; } -int Kernel::AddRhsFunctionCPP(char const* pRhsFunctionName, RhsEventHandlerCPP handler, void* pUserData, bool addToBack) +int Kernel::AddRhsFunctionCPP(char const* pRhsFunctionName, RhsEventHandlerCPP handler, bool addToBack) { smlRhsEventId id = smlEVENT_RHS_USER_FUNCTION ; // TODO: get rid of pUserData - return InternalAddRhsFunction(id, pRhsFunctionName, handler, pUserData, addToBack) ; + return InternalAddRhsFunction(id, pRhsFunctionName, handler, addToBack) ; } /************************************************************* @@ -2255,7 +2244,7 @@ int Kernel::RegisterForClientMessageEvent(char const* pClientName, ClientMessage // We actually use the RHS function code internally to process this message (since it's almost exactly like calling a RHS function that's // processed on a client). - return InternalAddRhsFunction(id, pClientName, newHandler, pUserData, addToBack) ; + return InternalAddRhsFunction(id, pClientName, newHandler, addToBack) ; } /************************************************************* diff --git a/Core/ClientSML/src/sml_ClientKernel.h b/Core/ClientSML/src/sml_ClientKernel.h index 0869613a83..1c4ce87dd4 100644 --- a/Core/ClientSML/src/sml_ClientKernel.h +++ b/Core/ClientSML/src/sml_ClientKernel.h @@ -107,7 +107,7 @@ namespace sml RhsEventHandlerPlusData() {} - RhsEventHandlerPlusData(int eventID, char const* pFunctionName, RhsEventHandlerCPP handler, void* userData, int callbackID) : EventHandlerPlusData(eventID, userData, callbackID) + RhsEventHandlerPlusData(int eventID, char const* pFunctionName, RhsEventHandlerCPP handler, int callbackID) : EventHandlerPlusData(eventID, nullptr, callbackID) { m_Handler = handler ; @@ -281,7 +281,7 @@ namespace sml /*** *** RHS functions and message event handlers use the same internal logic, although they look rather different to the user ***/ - int InternalAddRhsFunction(smlRhsEventId id, char const* pRhsFunctionName, RhsEventHandlerCPP handler, void* pUserData, bool addToBack) ; + int InternalAddRhsFunction(smlRhsEventId id, char const* pRhsFunctionName, RhsEventHandlerCPP handler, bool addToBack) ; bool InternalRemoveRhsFunction(smlRhsEventId id, int callbackID) ; /************************************************************* @@ -782,7 +782,7 @@ namespace sml *************************************************************/ int AddRhsFunction(char const* pRhsFunctionName, RhsEventHandler handler, void* pUserData, bool addToBack = true) ; // TODO: document - int AddRhsFunctionCPP(char const* pRhsFunctionName, RhsEventHandlerCPP handler, void* pUserData, bool addToBack = true) ; + int AddRhsFunctionCPP(char const* pRhsFunctionName, RhsEventHandlerCPP handler, bool addToBack = true) ; /************************************************************* * @brief Unregister for a particular rhs function callback