Skip to content

Commit f782936

Browse files
committed
ExecutionContext: Add random number generator
1 parent 5736a0f commit f782936

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

include/scratchcpp/dev/executioncontext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Thread;
1212
class IEngine;
1313
class Promise;
1414
class IStackTimer;
15+
class IRandomGenerator;
1516
class ExecutionContextPrivate;
1617

1718
/*! \brief The ExecutionContext represents the execution context of a target (can be a clone) with variables, lists, etc. */
@@ -31,6 +32,9 @@ class LIBSCRATCHCPP_EXPORT ExecutionContext
3132
IStackTimer *stackTimer() const;
3233
void setStackTimer(IStackTimer *newStackTimer);
3334

35+
IRandomGenerator *rng() const;
36+
void setRng(IRandomGenerator *newRng);
37+
3438
private:
3539
spimpl::unique_impl_ptr<ExecutionContextPrivate> impl;
3640
};

src/dev/engine/executioncontext.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,15 @@ void ExecutionContext::setStackTimer(IStackTimer *newStackTimer)
4848
{
4949
impl->stackTimer = newStackTimer;
5050
}
51+
52+
/*! Returns the random number generator of this context. */
53+
IRandomGenerator *ExecutionContext::rng() const
54+
{
55+
return impl->rng;
56+
}
57+
58+
/*! Sets a custom random number generator. */
59+
void ExecutionContext::setRng(IRandomGenerator *newRng)
60+
{
61+
impl->rng = newRng;
62+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// SPDX-License-Identifier: Apache-2.0
22

33
#include "executioncontext_p.h"
4+
#include "../../engine/internal/randomgenerator.h"
45

56
using namespace libscratchcpp;
67

78
ExecutionContextPrivate::ExecutionContextPrivate(Thread *thread) :
89
thread(thread),
910
defaultStackTimer(std::make_unique<StackTimer>()),
10-
stackTimer(defaultStackTimer.get())
11+
stackTimer(defaultStackTimer.get()),
12+
rng(RandomGenerator::instance().get())
1113
{
1214
}

src/dev/engine/executioncontext_p.h

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

1212
class Thread;
1313
class Promise;
14+
class IRandomGenerator;
1415

1516
struct ExecutionContextPrivate
1617
{
@@ -20,6 +21,7 @@ struct ExecutionContextPrivate
2021
std::shared_ptr<Promise> promise;
2122
std::unique_ptr<IStackTimer> defaultStackTimer;
2223
IStackTimer *stackTimer = nullptr;
24+
IRandomGenerator *rng = nullptr;
2325
};
2426

2527
} // namespace libscratchcpp

test/dev/executioncontext/executioncontext_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <enginemock.h>
66
#include <stacktimermock.h>
7+
#include <randomgeneratormock.h>
78

89
#include "../../common.h"
910

@@ -40,3 +41,13 @@ TEST(ExecutionContextTest, StackTimer)
4041
ctx.setStackTimer(&timer);
4142
ASSERT_EQ(ctx.stackTimer(), &timer);
4243
}
44+
45+
TEST(ExecutionContextTest, Rng)
46+
{
47+
ExecutionContext ctx(nullptr);
48+
ASSERT_TRUE(ctx.rng());
49+
50+
RandomGeneratorMock rng;
51+
ctx.setRng(&rng);
52+
ASSERT_EQ(ctx.rng(), &rng);
53+
}

0 commit comments

Comments
 (0)