Skip to content

Commit 284c097

Browse files
authored
Merge pull request #295 from scratchcpp/variable_list_target
Add target property to Variable and List
2 parents b2ed432 + 8078799 commit 284c097

File tree

18 files changed

+84
-106
lines changed

18 files changed

+84
-106
lines changed

include/scratchcpp/iengine.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,6 @@ class LIBSCRATCHCPP_EXPORT IEngine
247247
/*! Returns the Stage. */
248248
virtual Stage *stage() const = 0;
249249

250-
/*! Returns the target which owns the given variable. If it is the stage, the variable is global. */
251-
virtual Target *variableOwner(Variable *variable) const = 0;
252-
253-
/*! Returns the target which owns the given list. If it is the stage, the list is global. */
254-
virtual Target *listOwner(List *list) const = 0;
255-
256250
/*! Returns the list of extension names. */
257251
virtual const std::vector<std::string> &extensions() const = 0;
258252

include/scratchcpp/list.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace libscratchcpp
1212
{
1313

14+
class Target;
1415
class ListPrivate;
1516

1617
/*! \brief The List class represents a Scratch list. */
@@ -25,6 +26,9 @@ class LIBSCRATCHCPP_EXPORT List
2526
const std::string &name();
2627
void setName(const std::string &name);
2728

29+
Target *target() const;
30+
void setTarget(Target *target);
31+
2832
long indexOf(const Value &value) const;
2933
bool contains(const Value &value) const;
3034

include/scratchcpp/variable.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace libscratchcpp
1010
{
1111

12+
class Target;
1213
class VariablePrivate;
1314

1415
/*! \brief The Variable class represents a Scratch variable. */
@@ -29,6 +30,9 @@ class LIBSCRATCHCPP_EXPORT Variable : public Entity
2930
bool isCloudVariable() const;
3031
void setIsCloudVariable(bool isCloudVariable);
3132

33+
Target *target() const;
34+
void setTarget(Target *target);
35+
3236
std::shared_ptr<Variable> clone();
3337

3438
private:

src/engine/internal/engine.cpp

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,6 @@ const std::vector<std::shared_ptr<Target>> &Engine::targets() const
689689
void Engine::setTargets(const std::vector<std::shared_ptr<Target>> &newTargets)
690690
{
691691
m_targets = newTargets;
692-
m_variableOwners.clear();
693-
m_listOwners.clear();
694692

695693
for (auto target : m_targets) {
696694
// Set engine in the target
@@ -702,18 +700,6 @@ void Engine::setTargets(const std::vector<std::shared_ptr<Target>> &newTargets)
702700
block->setEngine(this);
703701
block->setTarget(target.get());
704702
}
705-
706-
// Add variables to owner map
707-
const auto &variables = target->variables();
708-
709-
for (auto variable : variables)
710-
m_variableOwners[variable.get()] = target.get();
711-
712-
// Add lists to owner map
713-
const auto &lists = target->lists();
714-
715-
for (auto list : lists)
716-
m_listOwners[list.get()] = target.get();
717703
}
718704
}
719705

@@ -746,26 +732,6 @@ Stage *Engine::stage() const
746732
return dynamic_cast<Stage *>((*it).get());
747733
}
748734

749-
Target *Engine::variableOwner(Variable *variable) const
750-
{
751-
auto it = m_variableOwners.find(variable);
752-
753-
if (it == m_variableOwners.cend())
754-
return nullptr;
755-
756-
return it->second;
757-
}
758-
759-
Target *Engine::listOwner(List *list) const
760-
{
761-
auto it = m_listOwners.find(list);
762-
763-
if (it == m_listOwners.cend())
764-
return nullptr;
765-
766-
return it->second;
767-
}
768-
769735
const std::vector<std::string> &Engine::extensions() const
770736
{
771737
return m_extensions;

src/engine/internal/engine.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ class Engine : public IEngine
100100

101101
Stage *stage() const override;
102102

103-
Target *variableOwner(Variable *variable) const override;
104-
Target *listOwner(List *list) const override;
105-
106103
const std::vector<std::string> &extensions() const override;
107104
void setExtensions(const std::vector<std::string> &newExtensions) override;
108105

@@ -133,8 +130,6 @@ class Engine : public IEngine
133130
std::vector<VirtualMachine *> m_scriptsToRemove;
134131
std::unordered_map<std::shared_ptr<Block>, std::shared_ptr<Script>> m_scripts;
135132
std::vector<BlockFunc> m_functions;
136-
std::unordered_map<Variable *, Target *> m_variableOwners;
137-
std::unordered_map<List *, Target *> m_listOwners;
138133

139134
std::unique_ptr<ITimer> m_defaultTimer;
140135
ITimer *m_timer = nullptr;

src/engine/script.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ std::shared_ptr<VirtualMachine> Script::start(Target *target)
7777
std::vector<List *> lists;
7878

7979
for (const auto &var : impl->variables) {
80-
Target *owner = impl->engine->variableOwner(var);
80+
Target *owner = var->target();
8181

8282
if (owner && (owner == root)) {
8383
auto cloneVar = sprite->variableAt(sprite->findVariableById(var->id()));
@@ -90,7 +90,7 @@ std::shared_ptr<VirtualMachine> Script::start(Target *target)
9090
}
9191

9292
for (const auto &list : impl->lists) {
93-
Target *owner = impl->engine->listOwner(list);
93+
Target *owner = list->target();
9494

9595
if (owner && (owner == root)) {
9696
auto cloneList = sprite->listAt(sprite->findListById(list->id()));

src/scratch/list.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ void List::setName(const std::string &name)
2626
impl->name = name;
2727
}
2828

29+
/*! Returns the sprite or stage this list belongs to. */
30+
Target *List::target() const
31+
{
32+
return impl->target;
33+
}
34+
35+
/*! Sets the sprite or stage this list belongs to. */
36+
void List::setTarget(Target *target)
37+
{
38+
impl->target = target;
39+
}
40+
2941
/*! Returns the index of the given item. */
3042
long List::indexOf(const Value &value) const
3143
{

src/scratch/list_p.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
namespace libscratchcpp
88
{
99

10+
class Target;
11+
1012
struct ListPrivate
1113
{
1214
ListPrivate(const std::string &name);
1315

1416
std::string name;
17+
Target *target = nullptr;
1518
};
1619

1720
} // namespace libscratchcpp

src/scratch/target.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ int Target::addVariable(std::shared_ptr<Variable> variable)
4242
return it - impl->variables.begin();
4343

4444
impl->variables.push_back(variable);
45+
variable->setTarget(this);
46+
4547
return impl->variables.size() - 1;
4648
}
4749

@@ -93,6 +95,8 @@ int Target::addList(std::shared_ptr<List> list)
9395
return it - impl->lists.begin();
9496

9597
impl->lists.push_back(list);
98+
list->setTarget(this);
99+
96100
return impl->lists.size() - 1;
97101
}
98102

src/scratch/variable.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ void Variable::setIsCloudVariable(bool isCloudVariable)
4949
impl->isCloudVariable = isCloudVariable;
5050
}
5151

52+
/*! Returns the sprite or stage this variable belongs to. */
53+
Target *Variable::target() const
54+
{
55+
return impl->target;
56+
}
57+
58+
/*! Sets the sprite or stage this variable belongs to. */
59+
void Variable::setTarget(Target *target)
60+
{
61+
impl->target = target;
62+
}
63+
5264
/*! Creates a copy of the variable. */
5365
std::shared_ptr<Variable> Variable::clone()
5466
{

0 commit comments

Comments
 (0)