in reply to Passing integer pointer in XS?
Provides the specified interface:
#define _MAX_PROTOCOL_LEN 25 #define _MAX_MODEL_LEN 25 int tdSensor(SV* protocol_sv, SV* model_sv, SV* id_sv, SV* dataTypes_sv) CODE: { char protocol[_MAX_PROTOCOL_LEN + 1]; char model[_MAX_MODEL_LEN + 1]; int id; int dataTypes; RETVAL = tdSensor( protocol, sizeof(protocol), model, sizeof(model), &id, &dataTypes ); if (RETVAL == TELLSTICK_SUCCESS) { SV* sv; sv = sv_2mortal(newSVpv(protocol, 0))); SvSetMagicSV(protoco +l_sv, sv); sv = sv_2mortal(newSVpv(model, 0)); SvSetMagicSV(model_s +v, sv); sv = sv_2mortal(newIV(id)); SvSetMagicSV(id_sv, + sv); sv = sv_2mortal(newIV(dataTypes)); SvSetMagicSV(dataTyp +es_sv, sv); } else { SvSetMagicSV(protocol_sv, &PL_sv_undef); SvSetMagicSV(model_sv, &PL_sv_undef); SvSetMagicSV(id_sv, &PL_sv_undef); SvSetMagicSV(dataTypes_sv, &PL_sv_undef); } } OUTPUT: RETVAL
Untested.
Provides a more Perlish interface:
#define _MAX_PROTOCOL_LEN 25 #define _MAX_MODEL_LEN 25 SV* tdSensor() CODE: { char protocol[_MAX_PROTOCOL_LEN + 1]; char model[_MAX_MODEL_LEN + 1]; int id; int dataTypes; int rv = tdSensor( protocol, sizeof(protocol), model, sizeof(model), &id, &dataTypes ); if (rv == TELLSTICK_SUCCESS) { HV* hv = newHV(); SV* sv; /* In theory, hv_stores can fail. */ /* However, I suspect it can't happen for this new hash. */ /* It it were to happen here, this code would leak. */ sv = newSVpv(protocol, 0); hv_stores(hv, "protocol", sv); sv = newSVpv(model, 0); hv_stores(hv, "model", sv); sv = newIV(id); hv_stores(hv, "id", sv); sv = newIV(dataTypes); hv_stores(hv, "dataTypes", sv); RETVAL = newRV_noinc(MUTABLE_SV(hv)); } else { RETVAL = &PL_sv_undef; } } OUTPUT: RETVAL
Usage:
use feature qw( say ); while (my $rec = tdSensor()) { say join ', ', "protocol: $rec->{protocol}", "model: $rec->{model}", "sensorId: $rec->{id}", "dataTypes: $rec->{dataTypes}"; }
Untested.
Update: Fixed missing sv declaration and missing typecast for newRV_noinc's argument.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Passing integer pointer in XS?
by martin67 (Novice) on Jul 18, 2016 at 20:56 UTC | |
by tye (Sage) on Jul 18, 2016 at 21:10 UTC | |
by ikegami (Patriarch) on Jul 18, 2016 at 21:31 UTC | |
by tye (Sage) on Jul 18, 2016 at 22:14 UTC | |
by ikegami (Patriarch) on Jul 19, 2016 at 07:02 UTC | |
| |
by ikegami (Patriarch) on Jul 18, 2016 at 21:18 UTC |