Skip to content

Commit 11fb517

Browse files
committed
Target: Do not allow duplicate entities
1 parent e3c232b commit 11fb517

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/scratch/target.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ const std::vector<std::shared_ptr<Variable>> &Target::variables() const
3636
/*! Adds a variable and returns its index. */
3737
int Target::addVariable(std::shared_ptr<Variable> variable)
3838
{
39+
auto it = std::find(impl->variables.begin(), impl->variables.end(), variable);
40+
41+
if (it != impl->variables.end())
42+
return it - impl->variables.begin();
43+
3944
impl->variables.push_back(variable);
4045
return impl->variables.size() - 1;
4146
}
@@ -79,6 +84,11 @@ const std::vector<std::shared_ptr<List>> &Target::lists() const
7984
/*! Adds a list and returns its index. */
8085
int Target::addList(std::shared_ptr<List> list)
8186
{
87+
auto it = std::find(impl->lists.begin(), impl->lists.end(), list);
88+
89+
if (it != impl->lists.end())
90+
return it - impl->lists.begin();
91+
8292
impl->lists.push_back(list);
8393
return impl->lists.size() - 1;
8494
}
@@ -122,6 +132,11 @@ const std::vector<std::shared_ptr<Block>> &Target::blocks() const
122132
/*! Adds a block and returns its index. */
123133
int Target::addBlock(std::shared_ptr<Block> block)
124134
{
135+
auto it = std::find(impl->blocks.begin(), impl->blocks.end(), block);
136+
137+
if (it != impl->blocks.end())
138+
return it - impl->blocks.begin();
139+
125140
impl->blocks.push_back(block);
126141
return impl->blocks.size() - 1;
127142
}
@@ -177,6 +192,21 @@ const std::vector<Costume> &Target::costumes() const
177192
/*! Adds a costume and returns its index. */
178193
int Target::addCostume(const Costume &costume)
179194
{
195+
// TODO: Use shared_ptr for assets
196+
/*auto it = std::find(impl->costumes.begin(), impl->costumes.end(), costume);
197+
198+
if (it != impl->blocks.end())
199+
return it - impl->blocks.begin();*/
200+
201+
int i = 0;
202+
203+
for (const auto &c : impl->costumes) {
204+
if (c.name() == costume.name())
205+
return i;
206+
207+
i++;
208+
}
209+
180210
impl->costumes.push_back(costume);
181211
return impl->costumes.size() - 1;
182212
}
@@ -208,6 +238,20 @@ const std::vector<Sound> &Target::sounds() const
208238
/*! Adds a sound and returns its index. */
209239
int Target::addSound(const Sound &sound)
210240
{
241+
// TODO: Use shared_ptr for assets
242+
/*auto it = std::find(impl->sounds.begin(), impl->sounds.end(), sound);
243+
244+
if (it != impl->sounds.end())
245+
return it - impl->sounds.begin();*/
246+
247+
int i = 0;
248+
249+
for (const auto &s : impl->sounds) {
250+
if (s.name() == sound.name())
251+
return i;
252+
253+
i++;
254+
}
211255
impl->sounds.push_back(sound);
212256
return impl->sounds.size() - 1;
213257
}

0 commit comments

Comments
 (0)