Skip to main content

C-Specific Syntax Sugar

This page only covers C SDK A Unique and Convenient Way to Write. They Not a new feature. ..it simply repackages existing entries into shorter macros.

Optional; leaving it out does not affect the standard entry. —— PnSServiceStart / PnSServiceStop, PnSServiceReadInt16 / PnSServiceWriteInt16 Use them as usual. Syntax sugar is just another way of writing code.

Header Files —— #include "pns.h" That's it; there's no need to include it separately pns_sugar.h.


PNS_TRY / PNS_START_OR_RETURN​

Write short macros for checking return codes and starting/stopping. These must be used within the function pns_result_t that returns the value.

Writing StyleCall
Standards APIif (rc != PNS_OK) return rc;
Syntax SugarPNS_TRY(rc);
Standards APIrc = PnSServiceStart(session); if (rc != PNS_OK) return rc;
Syntax SugarPNS_START_OR_RETURN(session);
#include "pns.h"

pns_result_t run(void)
{
pns_service_handle_t session = NULL;
pns_result_t rc;

rc = PnSServiceConnectRealtime(&session);
PNS_TRY(rc);

PNS_START_OR_RETURN(session);

/* Write "Q", read "I" */

PnSServiceStop(session);
PnSServiceClose(session);
return PNS_OK;
}

PNS_START_OR_RETURN Failure means return. Do not close the session; call PnSServiceStop / yourself when it ends PnSServiceClose.

When to Use​

  • Short functions—return an error if there's a mistake: Use PNS_TRY / PNS_START_OR_RETURN.
  • Running for a long time: Call PnSServiceStart / directly PnSServiceStop; do not use a macro.

List of Skills​

GroupingContent
Return CodePNS_TRY(rc): Failure means return this error code
Start-StopPNS_START_OR_RETURN(session): PnSServiceStart Failure means return. Do not close the session.