Skip to content

Commit c0031eb

Browse files
committed
Find inputs and fields by ID manually if not found in the map
1 parent 01d5644 commit c0031eb

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/scratch/block.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ Input *Block::findInputById(int id) const
199199
{
200200
if (impl->inputMap.count(id) == 1)
201201
return impl->inputMap.at(id);
202+
else {
203+
auto it = std::find_if(impl->inputs.begin(), impl->inputs.end(), [id](std::shared_ptr<Input> input) { return input->inputId() == id; });
204+
205+
if (it != impl->inputs.end())
206+
return it->get();
207+
}
202208
return nullptr;
203209
}
204210

@@ -255,6 +261,12 @@ Field *Block::findFieldById(int id) const
255261
{
256262
if (impl->fieldMap.count(id) == 1)
257263
return impl->fieldMap.at(id);
264+
else {
265+
auto it = std::find_if(impl->fields.begin(), impl->fields.end(), [id](std::shared_ptr<Field> field) { return field->fieldId() == id; });
266+
267+
if (it != impl->fields.end())
268+
return it->get();
269+
}
258270
return nullptr;
259271
}
260272

test/scratch_classes/block_test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ TEST_F(BlockTest, Inputs)
118118
ASSERT_EQ(block.findInputById(11), i1.get());
119119
ASSERT_EQ(block.findInputById(12), i2.get());
120120
ASSERT_EQ(block.findInputById(15), i3.get());
121+
122+
auto i4 = std::make_shared<Input>("VALUE4", Input::Type::Shadow);
123+
block.addInput(i4);
124+
i4->setInputId(20);
125+
ASSERT_EQ(block.findInputById(20), i4.get());
121126
}
122127

123128
TEST_F(BlockTest, Fields)
@@ -153,6 +158,11 @@ TEST_F(BlockTest, Fields)
153158
ASSERT_EQ(block.findFieldById(11), f1.get());
154159
ASSERT_EQ(block.findFieldById(12), f2.get());
155160
ASSERT_EQ(block.findFieldById(15), f3.get());
161+
162+
auto f4 = std::make_shared<Field>("VARIABLE4", Value());
163+
block.addField(f4);
164+
f4->setFieldId(20);
165+
ASSERT_EQ(block.findFieldById(20), f4.get());
156166
}
157167

158168
TEST_F(BlockTest, Shadow)

0 commit comments

Comments
 (0)