Skip to content

Commit 4b83845

Browse files
authored
Merge pull request #293 from scratchcpp/remove_updateinputfieldmap
Make input and field map update methods private
2 parents cd78df4 + c0031eb commit 4b83845

File tree

13 files changed

+31
-44
lines changed

13 files changed

+31
-44
lines changed

include/scratchcpp/block.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class BlockPrivate;
1919
class LIBSCRATCHCPP_EXPORT Block : public Entity
2020
{
2121
public:
22+
friend class Engine;
23+
2224
Block(const std::string &id, const std::string &opcode);
2325
Block(const Block &) = delete;
2426

@@ -41,14 +43,12 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
4143
std::shared_ptr<Input> inputAt(int index) const;
4244
int findInput(const std::string &inputName) const;
4345
Input *findInputById(int id) const;
44-
void updateInputMap();
4546

4647
std::vector<std::shared_ptr<Field>> fields() const;
4748
int addField(std::shared_ptr<Field> field);
4849
std::shared_ptr<Field> fieldAt(int index) const;
4950
int findField(const std::string &fieldName) const;
5051
Field *findFieldById(int id) const;
51-
void updateFieldMap();
5252

5353
bool shadow() const;
5454
void setShadow(bool newShadow);
@@ -75,6 +75,9 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
7575
InputValue *topLevelReporterInfo();
7676

7777
private:
78+
void updateInputMap();
79+
void updateFieldMap();
80+
7881
spimpl::unique_impl_ptr<BlockPrivate> impl;
7982
};
8083

src/scratch/block.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ int Block::addInput(std::shared_ptr<Input> input)
168168
return it - impl->inputs.begin();
169169

170170
impl->inputs.push_back(input);
171+
impl->inputMap[input->inputId()] = input.get();
172+
171173
return impl->inputs.size() - 1;
172174
}
173175

@@ -197,10 +199,15 @@ Input *Block::findInputById(int id) const
197199
{
198200
if (impl->inputMap.count(id) == 1)
199201
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+
}
200208
return nullptr;
201209
}
202210

203-
/*! Updates the map that assigns input IDs to input indexes. Used internally by Engine. */
204211
void Block::updateInputMap()
205212
{
206213
impl->inputMap.clear();
@@ -223,6 +230,8 @@ int Block::addField(std::shared_ptr<Field> field)
223230
return it - impl->fields.begin();
224231

225232
impl->fields.push_back(field);
233+
impl->fieldMap[field->fieldId()] = field.get();
234+
226235
return impl->fields.size() - 1;
227236
}
228237

@@ -252,10 +261,15 @@ Field *Block::findFieldById(int id) const
252261
{
253262
if (impl->fieldMap.count(id) == 1)
254263
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+
}
255270
return nullptr;
256271
}
257272

258-
/*! Updates the map that assigns input IDs to input indexes. Used internally by Engine. */
259273
void Block::updateFieldMap()
260274
{
261275
impl->fieldMap.clear();

test/blocks/control_blocks_test.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class ControlBlocksTest : public testing::Test
6363
input->setValueBlock(valueBlock);
6464
input->setInputId(id);
6565
block->addInput(input);
66-
block->updateInputMap();
6766
}
6867

6968
void addObscuredInput(std::shared_ptr<Block> block, const std::string &name, ControlBlocks::Inputs id, std::shared_ptr<Block> valueBlock) const
@@ -72,7 +71,6 @@ class ControlBlocksTest : public testing::Test
7271
input->setValueBlock(valueBlock);
7372
input->setInputId(id);
7473
block->addInput(input);
75-
block->updateInputMap();
7674
}
7775

7876
void addValueInput(std::shared_ptr<Block> block, const std::string &name, ControlBlocks::Inputs id, const Value &value) const
@@ -81,15 +79,13 @@ class ControlBlocksTest : public testing::Test
8179
input->setPrimaryValue(value);
8280
input->setInputId(id);
8381
block->addInput(input);
84-
block->updateInputMap();
8582
}
8683

8784
std::shared_ptr<Input> addNullInput(std::shared_ptr<Block> block, const std::string &name, ControlBlocks::Inputs id) const
8885
{
8986
auto input = std::make_shared<Input>(name, Input::Type::Shadow);
9087
input->setInputId(id);
9188
block->addInput(input);
92-
block->updateInputMap();
9389

9490
return input;
9591
}
@@ -100,15 +96,13 @@ class ControlBlocksTest : public testing::Test
10096
field->setFieldId(id);
10197
field->setSpecialValueId(valueId);
10298
block->addField(field);
103-
block->updateFieldMap();
10499
}
105100

106101
void addVariableField(std::shared_ptr<Block> block, std::shared_ptr<Variable> variable)
107102
{
108103
auto variableField = std::make_shared<Field>("VARIABLE", Value(), variable);
109104
variableField->setFieldId(ControlBlocks::VARIABLE);
110105
block->addField(variableField);
111-
block->updateFieldMap();
112106
}
113107

114108
std::shared_ptr<Block> createSubstack(const std::string &id)

test/blocks/custom_blocks_test.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class CustomBlocksTest : public testing::Test
2727
input->setValueBlock(prototypeBlock);
2828
input->setInputId(CustomBlocks::CUSTOM_BLOCK);
2929
definitionBlock->addInput(input);
30-
definitionBlock->updateInputMap();
3130
}
3231

3332
void addArgumentInput(std::shared_ptr<Block> block, const std::string &argId, const Value &value) const
@@ -94,7 +93,6 @@ TEST_F(CustomBlocksTest, CustomBlocks)
9493
auto valueField = std::make_shared<Field>("VALUE", "boolean");
9594
valueField->setFieldId(CustomBlocks::VALUE);
9695
argBlock->addField(valueField);
97-
argBlock->updateFieldMap();
9896
input->setValueBlock(argBlock);
9997
input->setInputId(-100);
10098
testBlock->addInput(input);
@@ -105,12 +103,10 @@ TEST_F(CustomBlocksTest, CustomBlocks)
105103
valueField = std::make_shared<Field>("VALUE", "invalid");
106104
valueField->setFieldId(CustomBlocks::VALUE);
107105
argBlock->addField(valueField);
108-
argBlock->updateFieldMap();
109106
input->setValueBlock(argBlock);
110107
input->setInputId(-101);
111108
testBlock->addInput(input);
112109

113-
testBlock->updateInputMap();
114110
testBlock->setCompileFunction([](Compiler *compiler) {
115111
compiler->addInput(-100);
116112
compiler->addInstruction(vm::OP_PRINT);

test/blocks/event_blocks_test.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class EventBlocksTest : public testing::Test
4040
input->primaryValue()->setType(InputValue::Type::Broadcast);
4141
input->setInputId(id);
4242
block->addInput(input);
43-
block->updateInputMap();
4443
}
4544

4645
void addObscuredInput(std::shared_ptr<Block> block, const std::string &name, EventBlocks::Inputs id, std::shared_ptr<Block> valueBlock) const
@@ -49,23 +48,20 @@ class EventBlocksTest : public testing::Test
4948
input->setValueBlock(valueBlock);
5049
input->setInputId(id);
5150
block->addInput(input);
52-
block->updateInputMap();
5351
}
5452

5553
void addValueField(std::shared_ptr<Block> block, const std::string &name, EventBlocks::Fields id, const std::string &value) const
5654
{
5755
auto field = std::make_shared<Field>(name, value);
5856
field->setFieldId(id);
5957
block->addField(field);
60-
block->updateFieldMap();
6158
}
6259

6360
void addBroadcastField(std::shared_ptr<Block> block, const std::string &name, EventBlocks::Fields id, std::shared_ptr<Broadcast> broadcast) const
6461
{
6562
auto field = std::make_shared<Field>(name, Value(), broadcast);
6663
field->setFieldId(id);
6764
block->addField(field);
68-
block->updateFieldMap();
6965
}
7066

7167
std::unique_ptr<IBlockSection> m_section;

test/blocks/list_blocks_test.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class ListBlocksTest : public testing::Test
2828
auto listField = std::make_shared<Field>("LIST", Value(), list);
2929
listField->setFieldId(ListBlocks::LIST);
3030
block->addField(listField);
31-
block->updateFieldMap();
3231

3332
return block;
3433
}
@@ -39,7 +38,6 @@ class ListBlocksTest : public testing::Test
3938
input->setPrimaryValue(item);
4039
input->setInputId(ListBlocks::ITEM);
4140
block->addInput(input);
42-
block->updateInputMap();
4341
}
4442

4543
void addIndexInput(std::shared_ptr<Block> block, const Value &index) const
@@ -48,7 +46,6 @@ class ListBlocksTest : public testing::Test
4846
input->setPrimaryValue(index);
4947
input->setInputId(ListBlocks::INDEX);
5048
block->addInput(input);
51-
block->updateInputMap();
5249
}
5350

5451
// For add item, item index and list contains item

test/blocks/looks_blocks_test.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class LooksBlocksTest : public testing::Test
4545
input->setPrimaryValue(value);
4646
input->setInputId(id);
4747
block->addInput(input);
48-
block->updateInputMap();
4948
}
5049

5150
void addObscuredInput(std::shared_ptr<Block> block, const std::string &name, LooksBlocks::Inputs id, std::shared_ptr<Block> valueBlock) const
@@ -54,15 +53,13 @@ class LooksBlocksTest : public testing::Test
5453
input->setValueBlock(valueBlock);
5554
input->setInputId(id);
5655
block->addInput(input);
57-
block->updateInputMap();
5856
}
5957

6058
std::shared_ptr<Input> addNullInput(std::shared_ptr<Block> block, const std::string &name, LooksBlocks::Inputs id) const
6159
{
6260
auto input = std::make_shared<Input>(name, Input::Type::Shadow);
6361
input->setInputId(id);
6462
block->addInput(input);
65-
block->updateInputMap();
6663

6764
return input;
6865
}
@@ -85,7 +82,6 @@ class LooksBlocksTest : public testing::Test
8582
field->setFieldId(id);
8683
field->setSpecialValueId(valueId);
8784
block->addField(field);
88-
block->updateFieldMap();
8985
}
9086

9187
std::unique_ptr<IBlockSection> m_section;

test/blocks/motion_blocks_test.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class MotionBlocksTest : public testing::Test
4545
input->setPrimaryValue(value);
4646
input->setInputId(id);
4747
block->addInput(input);
48-
block->updateInputMap();
4948
}
5049

5150
void addObscuredInput(std::shared_ptr<Block> block, const std::string &name, MotionBlocks::Inputs id, std::shared_ptr<Block> valueBlock) const
@@ -54,15 +53,13 @@ class MotionBlocksTest : public testing::Test
5453
input->setValueBlock(valueBlock);
5554
input->setInputId(id);
5655
block->addInput(input);
57-
block->updateInputMap();
5856
}
5957

6058
std::shared_ptr<Input> addNullInput(std::shared_ptr<Block> block, const std::string &name, MotionBlocks::Inputs id) const
6159
{
6260
auto input = std::make_shared<Input>(name, Input::Type::Shadow);
6361
input->setInputId(id);
6462
block->addInput(input);
65-
block->updateInputMap();
6663

6764
return input;
6865
}
@@ -85,7 +82,6 @@ class MotionBlocksTest : public testing::Test
8582
field->setFieldId(id);
8683
field->setSpecialValueId(valueId);
8784
block->addField(field);
88-
block->updateFieldMap();
8985
}
9086

9187
std::unique_ptr<IBlockSection> m_section;

test/blocks/operator_blocks_test.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class OperatorBlocksTest : public testing::Test
3030
input->setPrimaryValue(value);
3131
input->setInputId(id);
3232
block->addInput(input);
33-
block->updateInputMap();
3433
}
3534

3635
void addObscuredInput(std::shared_ptr<Block> block, const std::string &name, OperatorBlocks::Inputs id, std::shared_ptr<Block> valueBlock) const
@@ -39,15 +38,13 @@ class OperatorBlocksTest : public testing::Test
3938
input->setValueBlock(valueBlock);
4039
input->setInputId(id);
4140
block->addInput(input);
42-
block->updateInputMap();
4341
}
4442

4543
void addNullInput(std::shared_ptr<Block> block, const std::string &name, OperatorBlocks::Inputs id) const
4644
{
4745
auto input = std::make_shared<Input>(name, Input::Type::Shadow);
4846
input->setInputId(id);
4947
block->addInput(input);
50-
block->updateInputMap();
5148
}
5249

5350
// For the mathop block
@@ -57,7 +54,6 @@ class OperatorBlocksTest : public testing::Test
5754
field->setFieldId(OperatorBlocks::OPERATOR);
5855
field->setSpecialValueId(operatorId);
5956
block->addField(field);
60-
block->updateFieldMap();
6157
}
6258

6359
std::unique_ptr<IBlockSection> m_section;

test/blocks/sensing_blocks_test.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class SensingBlocksTest : public testing::Test
3434
optionField->setFieldId(SensingBlocks::CURRENTMENU);
3535
optionField->setSpecialValueId(option);
3636
block->addField(optionField);
37-
block->updateFieldMap();
3837

3938
return block;
4039
}
@@ -54,15 +53,13 @@ class SensingBlocksTest : public testing::Test
5453
input->setValueBlock(valueBlock);
5554
input->setInputId(id);
5655
block->addInput(input);
57-
block->updateInputMap();
5856
}
5957

6058
std::shared_ptr<Input> addNullInput(std::shared_ptr<Block> block, const std::string &name, SensingBlocks::Inputs id) const
6159
{
6260
auto input = std::make_shared<Input>(name, Input::Type::Shadow);
6361
input->setInputId(id);
6462
block->addInput(input);
65-
block->updateInputMap();
6663

6764
return input;
6865
}
@@ -85,7 +82,6 @@ class SensingBlocksTest : public testing::Test
8582
field->setFieldId(id);
8683
field->setSpecialValueId(valueId);
8784
block->addField(field);
88-
block->updateFieldMap();
8985
}
9086

9187
std::unique_ptr<IBlockSection> m_section;

0 commit comments

Comments
 (0)