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
ScopedStartandProcessData().In()[i].AsInt16()).
Optional; leaving it out does not affect the standard entry. ——
Connected()/Status()/ErrorCode(),ReadInt16/WriteInt16,ProcessData().Read/ProcessData().WriteUse them as usual. Syntax sugar is just another way of writing code.
Header Files —
#include "pns.hpp". ForScopedStart, additionally add#include "pns_sugar.hpp". Types are indarra::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 Style | Call |
|---|---|
| Standards API | device.Start(); … device.Stop(); |
| Syntax Sugar | ScopedStart 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 Style | Call |
|---|---|
| Standards API | int16_t v = device.ReadInt16("IW2"); |
| Syntax Sugar | int16_t v = device.ProcessData().In()[2].AsInt16(); |
| Standards API | device.WriteInt16("QW2", 123); |
| Syntax Sugar | device.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
| Grouping | Content |
|---|---|
| Scope Start and Stop | ScopedStart: Enter Start(), Exit Stop(). release() / active() |
| Automatic Conversion of Process Data | Content / AsInt8 / AsUInt8 / AsInt16 / AsUInt16 / AsInt32 / AsUInt32 / AsInt64 / AsUInt64 / AsFloat / AsDouble / AsBool / GetBit / SetBit |