Skip to content

Commit d977806

Browse files
committed
fix(node-opal-orchestra): Use std::optional instead of negative value for absent indices
Signed-off-by: Steffen Vogel <[email protected]>
1 parent 9c19687 commit d977806

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

lib/nodes/opal_orchestra.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class OpalOrchestraMapping {
4545
// Cached signal indices
4646
// We keep a vector of indices to map the signal index in the signal list.
4747
SignalList::Ptr signalList; // Signal list for which the indices are valid.
48-
std::vector<int> indices;
48+
std::vector<std::optional<unsigned>> indices;
4949

5050
// Run-time members which will be retrieved from Orchestra in prepare().
5151
unsigned short key;
@@ -56,22 +56,22 @@ class OpalOrchestraMapping {
5656
OpalOrchestraMapping(DataItem *item, const std::string &path)
5757
: item(item), path(path), signals(), signalList(), indices() {}
5858

59-
void addSignal(Signal::Ptr signal, int orchestraIdx) {
60-
if (orchestraIdx < 0) {
59+
void addSignal(Signal::Ptr signal, std::optional<unsigned> orchestraIdx) {
60+
if (!orchestraIdx) {
6161
orchestraIdx = signals.size();
6262
}
6363

64-
if (orchestraIdx < static_cast<int>(signals.size())) {
65-
if (signals[orchestraIdx]) {
64+
if (*orchestraIdx < signals.size()) {
65+
if (signals[*orchestraIdx]) {
6666
throw RuntimeError("Index {} of Orchestra signal already mapped",
67-
orchestraIdx);
67+
*orchestraIdx);
6868
}
6969
} else {
70-
signals.resize(orchestraIdx + 1, nullptr);
70+
signals.resize(*orchestraIdx + 1, nullptr);
7171
item->length = signals.size();
7272
}
7373

74-
signals[orchestraIdx] = signal;
74+
signals[*orchestraIdx] = signal;
7575
}
7676

7777
void check() {
@@ -152,17 +152,17 @@ class OpalOrchestraMapping {
152152

153153
auto *orchestraDataPtr = buffer;
154154
for (auto &index : indices) {
155-
if (index < 0 || index >= static_cast<int>(smp->length)) {
155+
if (!index || *index >= smp->length) {
156156
orchestraDataPtr += typeSize;
157157
continue; // Unused index or index out of range.
158158
}
159159

160-
auto signal = smp->signals->getByIndex(index);
160+
auto signal = smp->signals->getByIndex(*index);
161161
if (!signal) {
162162
throw RuntimeError("Signal {} not found", index);
163163
}
164164

165-
toOrchestraSignalData(orchestraDataPtr, item->type, smp->data[index],
165+
toOrchestraSignalData(orchestraDataPtr, item->type, smp->data[*index],
166166
signal->type);
167167

168168
orchestraDataPtr += typeSize;
@@ -174,27 +174,27 @@ class OpalOrchestraMapping {
174174

175175
auto *orchestraDataPtr = buffer;
176176
for (auto &index : indices) {
177-
if (index < 0 || index >= static_cast<int>(smp->capacity)) {
177+
if (!index || *index >= smp->capacity) {
178178
continue; // Unused index or index out of range.
179179
}
180180

181-
for (int i = static_cast<int>(smp->length); i < index; i++) {
181+
for (unsigned i = smp->length; i < *index; i++) {
182182
smp->data[i].i = 0;
183183
}
184184

185-
auto signal = smp->signals->getByIndex(index);
185+
auto signal = smp->signals->getByIndex(*index);
186186
if (!signal) {
187-
throw RuntimeError("Signal {} not found", index);
187+
throw RuntimeError("Signal {} not found", *index);
188188
}
189189

190190
node::SignalType villasType;
191191
SignalData villasData =
192192
toNodeSignalData(orchestraDataPtr, item->type, villasType);
193193

194-
smp->data[index] = villasData.cast(villasType, signal->type);
194+
smp->data[*index] = villasData.cast(villasType, signal->type);
195195

196196
if (index >= static_cast<int>(smp->length)) {
197-
smp->length = index + 1;
197+
smp->length = *index + 1;
198198
}
199199

200200
orchestraDataPtr += typeSize;
@@ -218,7 +218,7 @@ class OpalOrchestraMapping {
218218

219219
indices.push_back(idx);
220220
} else {
221-
indices.push_back(-1); // Unused index
221+
indices.emplace_back(); // Unused index
222222
}
223223
}
224224

@@ -348,16 +348,22 @@ class OpalOrchestraNode : public Node {
348348

349349
const char *nme = nullptr;
350350
const char *typ = nullptr;
351-
int orchestraIdx = -1;
351+
int oi = -1;
352352

353353
auto ret = json_unpack_ex(json_signal, &err, 0, "{ s?: s, s?: s, s?: i }",
354354
"orchestra_name", &nme, "orchestra_type", &typ,
355-
"orchestra_index", &orchestraIdx);
355+
"orchestra_index", &oi);
356356
if (ret) {
357357
throw ConfigError(json_signal, err,
358358
"node-config-node-opal-orchestra-signals");
359359
}
360360

361+
std::optional<unsigned> orchestraIdx;
362+
363+
if (oi >= 0) {
364+
orchestraIdx = oi;
365+
}
366+
361367
auto defaultValue =
362368
signal->init.cast(signal->type, node::SignalType::FLOAT);
363369

0 commit comments

Comments
 (0)