Skip to content

Commit

Permalink
Further improvement towards code cleanup
Browse files Browse the repository at this point in the history
The worker() lambda function previously used to pass to processCb() has
now been integrated into the flow of processCb() function itself.
  • Loading branch information
Vodopivec, Klemen committed Mar 20, 2021
1 parent 85205e3 commit 480b38a
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 104 deletions.
18 changes: 6 additions & 12 deletions src/pydev_ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,23 @@ static void processRecordCb(aiRecord* rec)
else if (keyval.first == "%PREC%") keyval.second = std::to_string(rec->prec);
}
std::string code = Util::replace(rec->inp.value.instio.string, fields);
auto worker = [code, rec]() {

try {
epicsFloat64 val;
bool ret = PyWrapper::exec(code, (rec->tpro == 1), &val);
if (ret == true) {
if (PyWrapper::exec(code, (rec->tpro == 1), &val) == true) {
val = (val * rec->aslo) + rec->aoff;
if (rec->smoo == 0.0 || rec->udf)
rec->val = val;
else
rec->val = (rec->val * rec->smoo) + (val * (1.0 - rec->smoo));
rec->udf = 0;
}
return ret;
};

try {
if (worker() == false) {
ctx->processCbStatus = 0;
} else {
if (rec->tpro == 1) {
printf("ERROR: Can't convert returned Python type to record type\n");
printf("ERROR: Can't convert returned Python type to double type\n");
}
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
ctx->processCbStatus = -1;
} else {
ctx->processCbStatus = 0;
}
} catch (...) {
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
Expand Down
11 changes: 3 additions & 8 deletions src/pydev_ao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,15 @@ static void processRecordCb(aoRecord* rec)
else if (keyval.first == "%PREC%") keyval.second = std::to_string(rec->prec);
}
std::string code = Util::replace(rec->out.value.instio.string, fields);
auto worker = [code, rec]() {

try {
epicsFloat64 val;
bool ret = PyWrapper::exec(code, (rec->tpro == 1), &val);
if (ret == true) {
if (PyWrapper::exec(code, (rec->tpro == 1), &val) == true) {
rec->val = val;
if (rec->aslo != 0.0) rec->val *= rec->aslo;
rec->val += rec->aoff;
rec->udf = 0;
}
return ret;
};

try {
worker();
ctx->processCbStatus = 0;
} catch (...) {
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
Expand Down
9 changes: 3 additions & 6 deletions src/pydev_bi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,16 @@ static void processRecordCb(biRecord* rec)
else if (keyval.first == "%ONAM%") keyval.second = rec->onam;
}
std::string code = Util::replace(rec->inp.value.instio.string, fields);
auto worker = [code, rec]() {
return PyWrapper::exec(code, (rec->tpro == 1), &rec->rval);
};

try {
if (worker() == false) {
if (PyWrapper::exec(code, (rec->tpro == 1), &rec->rval) == true) {
ctx->processCbStatus = 0;
} else {
if (rec->tpro == 1) {
printf("ERROR: Can't convert returned Python type to record type\n");
}
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
ctx->processCbStatus = -1;
} else {
ctx->processCbStatus = 0;
}
} catch (...) {
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
Expand Down
5 changes: 1 addition & 4 deletions src/pydev_bo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,9 @@ static void processRecordCb(boRecord* rec)
else if (keyval.first == "%ONAM%") keyval.second = rec->onam;
}
std::string code = Util::replace(rec->out.value.instio.string, fields);
auto worker = [code, rec]() {
return PyWrapper::exec(code, (rec->tpro == 1), &rec->rval);
};

try {
worker();
PyWrapper::exec(code, (rec->tpro == 1), &rec->rval);
ctx->processCbStatus = 0;
} catch (...) {
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
Expand Down
9 changes: 3 additions & 6 deletions src/pydev_longin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,16 @@ static void processRecordCb(longinRecord* rec)
else if (keyval.first == "%LOLO%") keyval.second = std::to_string(rec->lolo);
}
std::string code = Util::replace(rec->inp.value.instio.string, fields);
auto worker = [code, rec]() {
return PyWrapper::exec(code, (rec->tpro == 1), &rec->val);
};

try {
if (worker() == false) {
if (PyWrapper::exec(code, (rec->tpro == 1), &rec->val) == true) {
ctx->processCbStatus = 0;
} else {
if (rec->tpro == 1) {
printf("ERROR: Can't convert returned Python type to record type\n");
}
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
ctx->processCbStatus = -1;
} else {
ctx->processCbStatus = 0;
}
} catch (...) {
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
Expand Down
5 changes: 1 addition & 4 deletions src/pydev_longout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,9 @@ static void processRecordCb(longoutRecord* rec)
else if (keyval.first == "%LOLO%") keyval.second = std::to_string(rec->lolo);
}
std::string code = Util::replace(rec->out.value.instio.string, fields);
auto worker = [code, rec]() {
return PyWrapper::exec(code, (rec->tpro == 1), &rec->val);
};

try {
worker();
PyWrapper::exec(code, (rec->tpro == 1), &rec->val);
ctx->processCbStatus = 0;
} catch (...) {
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
Expand Down
20 changes: 7 additions & 13 deletions src/pydev_lsi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,20 @@ static void processRecordCb(lsiRecord* rec)
else if (keyval.first == "%LEN%") keyval.second = std::to_string(rec->len);
}
std::string code = Util::replace(rec->inp.value.instio.string, fields);
auto worker = [code, rec]() {
std::string val(rec->val);
if (PyWrapper::exec(code, (rec->tpro == 1), val) == false) {
return false;
}
strncpy(rec->val, val.c_str(), rec->sizv - 1);
rec->val[rec->sizv - 1] = 0;
rec->len = strlen(rec->val) + 1;
return true;
};

try {
if (worker() == false) {
std::string val(rec->val);
if (PyWrapper::exec(code, (rec->tpro == 1), val) == true) {
strncpy(rec->val, val.c_str(), rec->sizv - 1);
rec->val[rec->sizv - 1] = 0;
rec->len = strlen(rec->val) + 1;
ctx->processCbStatus = 0;
} else {
if (rec->tpro == 1) {
printf("ERROR: Can't convert returned Python type to record type\n");
}
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
ctx->processCbStatus = -1;
} else {
ctx->processCbStatus = 0;
}
} catch (...) {
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
Expand Down
17 changes: 6 additions & 11 deletions src/pydev_lso.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,14 @@ static void processRecordCb(lsoRecord* rec)
else if (keyval.first == "%LEN%") keyval.second = std::to_string(rec->len);
}
std::string code = Util::replace(rec->out.value.instio.string, fields);
auto worker = [code, rec]() {
std::string val(rec->val);
if (PyWrapper::exec(code, (rec->tpro == 1), val) == false) {
return false;
}
strncpy(rec->val, val.c_str(), rec->sizv - 1);
rec->val[rec->sizv - 1] = 0;
rec->len = strlen(rec->val) + 1;
return true;
};

try {
worker();
std::string val(rec->val);
if (PyWrapper::exec(code, (rec->tpro == 1), val) == true) {
strncpy(rec->val, val.c_str(), rec->sizv - 1);
rec->val[rec->sizv - 1] = 0;
rec->len = strlen(rec->val) + 1;
}
ctx->processCbStatus = 0;
} catch (...) {
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
Expand Down
9 changes: 3 additions & 6 deletions src/pydev_mbbi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,16 @@ static void processRecordCb(mbbiRecord* rec)
else if (keyval.first == "%FFST%") keyval.second = rec->ffst;
}
std::string code = Util::replace(rec->inp.value.instio.string, fields);
auto worker = [code, rec]() {
return PyWrapper::exec(code, (rec->tpro == 1), &rec->rval);
};

try {
if (worker() == false) {
if (PyWrapper::exec(code, (rec->tpro == 1), &rec->rval) == true) {
ctx->processCbStatus = 0;
} else {
if (rec->tpro == 1) {
printf("ERROR: Can't convert returned Python type to record type\n");
}
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
ctx->processCbStatus = -1;
} else {
ctx->processCbStatus = 0;
}
} catch (...) {
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
Expand Down
5 changes: 1 addition & 4 deletions src/pydev_mbbo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,9 @@ static void processRecordCb(mbboRecord* rec)
else if (keyval.first == "%FFST%") keyval.second = rec->ffst;
}
std::string code = Util::replace(rec->out.value.instio.string, fields);
auto worker = [code, rec]() {
return PyWrapper::exec(code, (rec->tpro == 1), &rec->rval);
};

try {
worker();
PyWrapper::exec(code, (rec->tpro == 1), &rec->rval);
ctx->processCbStatus = 0;
} catch (...) {
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
Expand Down
18 changes: 6 additions & 12 deletions src/pydev_stringin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,19 @@ static void processRecordCb(stringinRecord* rec)
else if (keyval.first == "%NAME%") keyval.second = rec->name;
}
std::string code = Util::replace(rec->inp.value.instio.string, fields);
auto worker = [code, rec]() {
std::string val(rec->val);
if (PyWrapper::exec(code, (rec->tpro == 1), val) == false) {
return false;
}
strncpy(rec->val, val.c_str(), sizeof(rec->val)-1);
rec->val[sizeof(rec->val)-1] = 0;
return true;
};

try {
if (worker() == false) {
std::string val(rec->val);
if (PyWrapper::exec(code, (rec->tpro == 1), val) == true) {
strncpy(rec->val, val.c_str(), sizeof(rec->val)-1);
rec->val[sizeof(rec->val)-1] = 0;
ctx->processCbStatus = 0;
} else {
if (rec->tpro == 1) {
printf("ERROR: Can't convert returned Python type to record type\n");
}
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
ctx->processCbStatus = -1;
} else {
ctx->processCbStatus = 0;
}
} catch (...) {
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
Expand Down
15 changes: 5 additions & 10 deletions src/pydev_stringout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,13 @@ static void processRecordCb(stringoutRecord* rec)
else if (keyval.first == "%NAME%") keyval.second = rec->name;
}
std::string code = Util::replace(rec->out.value.instio.string, fields);
auto worker = [code, rec]() {
std::string val(rec->val);
if (PyWrapper::exec(code, (rec->tpro == 1), val) == false) {
return false;
}
strncpy(rec->val, val.c_str(), sizeof(rec->val)-1);
rec->val[sizeof(rec->val)-1] = 0;
return true;
};

try {
worker();
std::string val(rec->val);
if (PyWrapper::exec(code, (rec->tpro == 1), val) == true) {
strncpy(rec->val, val.c_str(), sizeof(rec->val)-1);
rec->val[sizeof(rec->val)-1] = 0;
}
ctx->processCbStatus = 0;
} catch (...) {
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
Expand Down
16 changes: 8 additions & 8 deletions src/pydev_waveform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,25 +188,25 @@ static void processRecordCb(waveformRecord* rec)
}
}
std::string code = Util::replace(rec->inp.value.instio.string, fields);
auto worker = [code, rec]() {

try {
bool ret;
if (rec->ftvl == menuFtypeFLOAT || rec->ftvl == menuFtypeDOUBLE) {
std::vector<double> arr;
return (PyWrapper::exec(code, (rec->tpro == 1), arr) && toRecArrayVal(rec, arr));
ret = (PyWrapper::exec(code, (rec->tpro == 1), arr) && toRecArrayVal(rec, arr));
} else {
std::vector<long> arr;
return (PyWrapper::exec(code, (rec->tpro == 1), arr) && toRecArrayVal(rec, arr));
ret = (PyWrapper::exec(code, (rec->tpro == 1), arr) && toRecArrayVal(rec, arr));
}
};

try {
if (worker() == false) {
if (ret == true) {
ctx->processCbStatus = 0;
} else {
if (rec->tpro == 1) {
printf("ERROR: Can't convert returned Python type to record type\n");
}
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
ctx->processCbStatus = -1;
} else {
ctx->processCbStatus = 0;
}
} catch (...) {
recGblSetSevr(rec, epicsAlarmCalc, epicsSevInvalid);
Expand Down

0 comments on commit 480b38a

Please sign in to comment.