Skip to content

Commit 8297cd5

Browse files
committed
Add target property to Variable
1 parent cd78df4 commit 8297cd5

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

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/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
{

src/scratch/variable_p.h

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

10+
class Target;
11+
1012
struct VariablePrivate
1113
{
1214
VariablePrivate(const std::string &name, const Value &value = Value(), bool isCloudVariable = false);
@@ -15,6 +17,7 @@ struct VariablePrivate
1517
std::string name;
1618
Value value;
1719
bool isCloudVariable;
20+
Target *target = nullptr;
1821
};
1922

2023
} // namespace libscratchcpp

test/scratch_classes/variable_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <scratchcpp/variable.h>
2+
#include <scratchcpp/target.h>
23

34
#include "../common.h"
45

@@ -57,6 +58,16 @@ TEST(VariableTest, IsCloudVariable)
5758
ASSERT_TRUE(var.isCloudVariable());
5859
}
5960

61+
TEST(VariableTest, Target)
62+
{
63+
Variable var("", "");
64+
ASSERT_EQ(var.target(), nullptr);
65+
66+
Target target;
67+
var.setTarget(&target);
68+
ASSERT_EQ(var.target(), &target);
69+
}
70+
6071
TEST(VariableTest, Clone)
6172
{
6273
std::shared_ptr<Variable> clone;

0 commit comments

Comments
 (0)