Skip to main content

C++-Specific Syntax Sugar

This page only covers convenient syntax unique to the PnSService C++ SDK. They are not new features — they simply repackage existing entry points in a more convenient C++ style (indexes such as ScopedStart and ProcessData().In()[i].AsInt16()).

Optional; leaving it out does not affect the standard entry. —— Connected() / Status() / ErrorCode(), ReadInt16 / WriteInt16, ProcessData().Read / ProcessData().Write Use them as usual. Syntax sugar is just another way of writing code.

Header Files — #include "pns.hpp". For ScopedStart, additionally add #include "pns_sugar.hpp". Types are in darra::pns.


I. ScopedStart​

Enter scope Start(), exit scope Stop(). No Close. Not destroyed, not discarded. Not copyable, not movable.

Constructor does not throw: Start() On failure, ok() is false, active() is false, and the destructorNo Stop(). To determine if it has started, check first ok(); look at the fault code errorCode()(return value PnSErrorCode).

Writing StyleCall
Standards APIdevice.Start(); … device.Stop();
Syntax SugarScopedStart guard(device);
#include "pns.hpp"
#include "pns_sugar.hpp"
using namespace darra::pns;

int main()
{
PnSService device;
{
ScopedStart guard(device);
if (!guard.ok())
return 1;
device.WriteBool("Q0.0", true);
} // Stop when leaving
return 0;
}

When you leave, don't stop; adjust it guard.release().

Use ScopedStart to open a temporary IO section so nothing is missed before Stop(). If it runs continuously, just use Start().


II. Automatic Conversion of Process Data Indexes​

ProcessData().In()[i] / ProcessData().Out()[i] read and write by type starting at the current offset. Use In to read I and Out to write Q.

Writing StyleCall
Standards APIint16_t v = device.ReadInt16("IW2");
Syntax Sugarint16_t v = device.ProcessData().In()[2].AsInt16();
Standards APIdevice.WriteInt16("QW2", 123);
Syntax Sugardevice.ProcessData().Out()[2].AsInt16(123);
uint8_t i0 = device.ProcessData().In()[0].Content();
int16_t iw = device.ProcessData().In()[2].AsInt16();
float f = device.ProcessData().In()[8].AsFloat();

device.ProcessData().Out()[0].Content(0x11);
device.ProcessData().Out()[2].AsInt16(123);
device.ProcessData().Out()[8].AsFloat(1.5f);

Write In()[i] Discard PnSException(I read-only).

When to Use​

  • Just a few fields: I don't want to create a struct just for a few words.
  • Many fields: Or ProcessData().Read / ProcessData().Write.

List of Skills​

GroupingContent
Scope Start and StopScopedStart: Enter Start(), Exit Stop(). release() / active()
Automatic Conversion of Process DataContent / AsInt8 / AsUInt8 / AsInt16 / AsUInt16 / AsInt32 / AsUInt32 / AsInt64 / AsUInt64 / AsFloat / AsDouble / AsBool / GetBit / SetBit