Skip to content

Commit

Permalink
Non-functioning solution
Browse files Browse the repository at this point in the history
This would have been so convenient, but alas C++ doesn't allow us to convert
from lambdas to function pointers.

Error we get:
Core/ClientSML/src/sml_ClientKernel.cpp:2225:45: error: no viable conversion from '(lambda at Core/ClientSML/src/sml_ClientKernel.cpp:2194:23)' to 'sml::RhsEventHandler' (aka 'const char *(*)(sml::smlRhsEventId, void *, sml::Agent *, const char *, const char *, int *, char *)')
    return AddRhsFunction(pRhsFunctionName, newHandler, pUserData, addToBack);
                                            ^~~~~~~~~~
Core/ClientSML/src/sml_ClientKernel.cpp:2185:74: note: passing argument to parameter 'handler' here
int Kernel::AddRhsFunction(char const* pRhsFunctionName, RhsEventHandler handler, void* pUserData, bool addToBack)
                                                                         ^
1 error generated.

See
https://stackoverflow.com/questions/28746744/passing-capturing-lambda-as-function-pointer.
  • Loading branch information
garfieldnate committed Jul 26, 2023
1 parent 5b73b5b commit b068747
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Core/ClientSML/src/sml_ClientEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,16 @@ namespace sml
typedef char const *(*RhsEventHandler)(smlRhsEventId id, void* pUserData, Agent* pAgent,
char const* pFunctionName, char const* pArgument, int *buffSize, char *buff) ;

typedef std::string(*RhsEventHandlerManaged)(smlRhsEventId id, void* pUserData, Agent* pAgent,
char const* pFunctionName, char const* pArgument) ;
// 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) ;

typedef std::string(*ClientMessageHandlerManaged)(smlRhsEventId id, void* pUserData, Agent* pAgent,
char const* pClientName, char const* pMessage) ;
// 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.
Expand Down
36 changes: 36 additions & 0 deletions Core/ClientSML/src/sml_ClientKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2189,6 +2189,42 @@ int Kernel::AddRhsFunction(char const* pRhsFunctionName, RhsEventHandler handler
return InternalAddRhsFunction(id, pRhsFunctionName, handler, pUserData, addToBack) ;
}

int Kernel::AddRhsFunctionManaged(char const* pRhsFunctionName, RhsEventHandlerManaged handler, void* pUserData, bool addToBack)
{
auto newHandler = [&handler](
smlRhsEventId id,
void* pUserData,
Agent* pAgent,
char const* pFunctionName,
char const* pArgument,
int *bufSize,
char *buf) -> char * {
static std::string prevResult;
// Previous call could not return the full result and cached it in prevResult instead.
// Return the cached result and clear the cache.
if ( !prevResult.empty() )
{
strncpy( buf, prevResult.c_str(), *bufSize );
prevResult = "";
return buf;
}

std::string resultStr = handler(id, pUserData, pAgent, pFunctionName, pArgument);
// Result won't fit in the buffer. Return NULL and set buffSize to the required size.
// Soar will then re-call this handler and get the cached result.
if ( resultStr.length() + 1 > *bufSize )
{
*bufSize = resultStr.length() + 1;
prevResult = resultStr;
return NULL;
}

strcpy( buf, resultStr.c_str() );
return buf;
};
return AddRhsFunction(pRhsFunctionName, newHandler, pUserData, addToBack);
}

/*************************************************************
* @brief Unregister for a particular RHS function
*************************************************************/
Expand Down
1 change: 1 addition & 0 deletions Core/ClientSML/src/sml_ClientKernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,7 @@ namespace sml
* @returns Unique ID for this callback. Required when unregistering this callback.
*************************************************************/
int AddRhsFunction(char const* pRhsFunctionName, RhsEventHandler handler, void* pUserData, bool addToBack = true) ;
int AddRhsFunctionManaged(char const* pRhsFunctionName, RhsEventHandlerManaged handler, void* pUserData, bool addToBack = true) ;

/*************************************************************
* @brief Unregister for a particular rhs function callback
Expand Down

0 comments on commit b068747

Please sign in to comment.