Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/scratchcpp/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class LIBSCRATCHCPP_EXPORT List

std::string toString() const;

std::shared_ptr<List> clone();

private:
spimpl::unique_impl_ptr<ListPrivate> impl;
};
Expand Down
2 changes: 2 additions & 0 deletions include/scratchcpp/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class LIBSCRATCHCPP_EXPORT Variable : public Entity
bool isCloudVariable() const;
void setIsCloudVariable(bool isCloudVariable);

std::shared_ptr<Variable> clone();

private:
spimpl::unique_impl_ptr<VariablePrivate> impl;
};
Expand Down
11 changes: 11 additions & 0 deletions src/scratch/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,14 @@ std::string List::toString() const

return ret;
}

/*! Creates a copy of the list. */
std::shared_ptr<List> List::clone()
{
auto copy = std::make_shared<List>(id(), impl->name);

for (const Value &item : *this)
copy->push_back(item);

return copy;
}
11 changes: 3 additions & 8 deletions src/scratch/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,12 @@ std::shared_ptr<Sprite> Sprite::clone()
const auto &vars = variables();

for (auto var : vars)
clone->addVariable(std::make_shared<Variable>(var->id(), var->name(), var->value()));
clone->addVariable(var->clone());

const auto &l = lists();

for (auto list : l) {
auto newList = std::make_shared<List>(list->id(), list->name());
clone->addList(newList);

for (const Value &item : *list)
newList->push_back(item);
}
for (auto list : l)
clone->addList(list->clone());

clone->setCurrentCostume(currentCostume());
clone->setLayerOrder(layerOrder());
Expand Down
6 changes: 6 additions & 0 deletions src/scratch/variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ void Variable::setIsCloudVariable(bool isCloudVariable)
{
impl->isCloudVariable = isCloudVariable;
}

/*! Creates a copy of the variable. */
std::shared_ptr<Variable> Variable::clone()
{
return std::make_shared<Variable>(id(), impl->name, impl->value, impl->isCloudVariable);
}
19 changes: 19 additions & 0 deletions test/scratch_classes/list_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,22 @@ TEST(ListTest, ToString)
list.push_back(8);
ASSERT_EQ(list.toString(), "098");
}

TEST(ListTest, Clone)
{
List list("abc", "test list");
list.push_back("Lorem");
list.push_back("ipsum");
list.push_back("dolor");
list.push_back("sit");
list.push_back("amet");

std::shared_ptr<List> clone = list.clone();
ASSERT_TRUE(clone);
ASSERT_EQ(clone->id(), list.id());
ASSERT_EQ(clone->name(), list.name());
ASSERT_EQ(clone->size(), list.size());

for (std::size_t i = 0; i < list.size(); i++)
ASSERT_EQ(list[i], (*clone)[i]);
}
20 changes: 20 additions & 0 deletions test/scratch_classes/variable_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,23 @@ TEST(VariableTest, IsCloudVariable)
var.setIsCloudVariable(true);
ASSERT_TRUE(var.isCloudVariable());
}

TEST(VariableTest, Clone)
{
std::shared_ptr<Variable> clone;
std::vector<std::shared_ptr<Variable>> vars;

vars.push_back(std::make_shared<Variable>("abc", "var1"));
vars.push_back(std::make_shared<Variable>("abc", "var2", "test"));
vars.push_back(std::make_shared<Variable>("abc", "var3", "test", true));
vars.push_back(std::make_shared<Variable>("abc", "var4", "test", false));

for (auto var : vars) {
clone = var->clone();
ASSERT_TRUE(clone);
ASSERT_EQ(clone->id(), var->id());
ASSERT_EQ(clone->name(), var->name());
ASSERT_EQ(clone->value(), var->value());
ASSERT_EQ(clone->isCloudVariable(), var->isCloudVariable());
}
}