Skip to content

Commit 5744b3d

Browse files
committed
ExecutionContext: Add stack timer
1 parent 21df2fe commit 5744b3d

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

include/scratchcpp/dev/executioncontext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace libscratchcpp
1111
class Thread;
1212
class IEngine;
1313
class Promise;
14+
class IStackTimer;
1415
class ExecutionContextPrivate;
1516

1617
/*! \brief The ExecutionContext represents the execution context of a target (can be a clone) with variables, lists, etc. */
@@ -27,6 +28,9 @@ class LIBSCRATCHCPP_EXPORT ExecutionContext
2728
std::shared_ptr<Promise> promise() const;
2829
void setPromise(std::shared_ptr<Promise> promise);
2930

31+
IStackTimer *stackTimer() const;
32+
void setStackTimer(IStackTimer *newStackTimer);
33+
3034
private:
3135
spimpl::unique_impl_ptr<ExecutionContextPrivate> impl;
3236
};

src/dev/engine/executioncontext.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,15 @@ void ExecutionContext::setPromise(std::shared_ptr<Promise> promise)
3636
{
3737
impl->promise = promise;
3838
}
39+
40+
/*! Returns the stack timer of this context. Can be used for wait blocks. */
41+
IStackTimer *ExecutionContext::stackTimer() const
42+
{
43+
return impl->stackTimer;
44+
}
45+
46+
/*! Sets a custom stack timer. */
47+
void ExecutionContext::setStackTimer(IStackTimer *newStackTimer)
48+
{
49+
impl->stackTimer = newStackTimer;
50+
}

src/dev/engine/executioncontext_p.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using namespace libscratchcpp;
66

77
ExecutionContextPrivate::ExecutionContextPrivate(Thread *thread) :
8-
thread(thread)
8+
thread(thread),
9+
defaultStackTimer(std::make_unique<StackTimer>()),
10+
stackTimer(defaultStackTimer.get())
911
{
1012
}

src/dev/engine/executioncontext_p.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
#include <memory>
66

7+
#include "../../engine/internal/stacktimer.h"
8+
79
namespace libscratchcpp
810
{
911

1012
class Thread;
11-
class Target;
1213
class Promise;
1314

1415
struct ExecutionContextPrivate
@@ -17,6 +18,8 @@ struct ExecutionContextPrivate
1718

1819
Thread *thread = nullptr;
1920
std::shared_ptr<Promise> promise;
21+
std::unique_ptr<IStackTimer> defaultStackTimer;
22+
IStackTimer *stackTimer = nullptr;
2023
};
2124

2225
} // namespace libscratchcpp

test/dev/executioncontext/executioncontext_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <scratchcpp/dev/promise.h>
44

55
#include <enginemock.h>
6+
#include <stacktimermock.h>
67

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

@@ -29,3 +30,13 @@ TEST(ExecutionContextTest, Promise)
2930
ctx.setPromise(nullptr);
3031
ASSERT_EQ(ctx.promise(), nullptr);
3132
}
33+
34+
TEST(ExecutionContextTest, StackTimer)
35+
{
36+
ExecutionContext ctx(nullptr);
37+
ASSERT_TRUE(ctx.stackTimer());
38+
39+
StackTimerMock timer;
40+
ctx.setStackTimer(&timer);
41+
ASSERT_EQ(ctx.stackTimer(), &timer);
42+
}

0 commit comments

Comments
 (0)