From fe08e1f98914ddcbc57d1ebc449fcad0f34d82ae Mon Sep 17 00:00:00 2001 From: Marco Satti Date: Thu, 19 Mar 2026 11:25:36 +0000 Subject: [PATCH 1/3] Add in post callback function to simulation --- include/fschuetz04/simcpp20/simulation.hpp | 12 +++++ tests/tests.cpp | 54 ++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/include/fschuetz04/simcpp20/simulation.hpp b/include/fschuetz04/simcpp20/simulation.hpp index ee98947..a745241 100644 --- a/include/fschuetz04/simcpp20/simulation.hpp +++ b/include/fschuetz04/simcpp20/simulation.hpp @@ -63,6 +63,18 @@ template class simulation { schedule(ev, delay); return ev; } + + /** + * Posts a callback to be executed as soon as possible. + * @param callback Callback to invoke, which has the following signature: `void callback()`. + * @return New pending event. + */ + event_type post(std::function callback) { + auto ev = event(); + ev.add_callback(callback); + schedule(ev); + return ev; + } /** * @tparam Value Value type of the event. diff --git a/tests/tests.cpp b/tests/tests.cpp index 68e33c7..cc5d672 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -167,3 +167,57 @@ TEST_CASE("all_of") { REQUIRE(finished); } } + +TEST_CASE("post event to simulation") { + simcpp20::simulation<> sim; + + bool ran = false; + auto ev1 = sim.post([&ran]() { ran = true; }); + + sim.step(); + REQUIRE(ran); + REQUIRE(ev1.processed()); +} + +TEST_CASE("post event ordering") { + simcpp20::simulation<> sim; + + SECTION("between other pending events") + { + bool ran = false; + + auto ev1 = sim.timeout(1); + auto ev2 = sim.post([&ran]() { ran = true; }); + + sim.step(); + REQUIRE(ran); + REQUIRE(ev1.pending()); + REQUIRE(ev2.processed()); + } + + SECTION("between multiple posted events") + { + bool ran1 = false; + bool ran2 = false; + bool ran3 = false; + + auto ev1 = sim.post([&ran1]() { ran1 = true; }); + auto ev2 = sim.post([&ran2]() { ran2 = true; }); + auto ev3 = sim.post([&ran3]() { ran3 = true; }); + + sim.step(); + REQUIRE(ran1); + REQUIRE_FALSE(ran2); + REQUIRE_FALSE(ran3); + + sim.step(); + REQUIRE(ran1); + REQUIRE(ran2); + REQUIRE_FALSE(ran3); + + sim.step(); + REQUIRE(ran1); + REQUIRE(ran2); + REQUIRE(ran3); + } +} \ No newline at end of file From ab9a0a9f8967af8526e46dfd5fce8661171dbea2 Mon Sep 17 00:00:00 2001 From: Marco Satti Date: Thu, 19 Mar 2026 11:50:42 +0000 Subject: [PATCH 2/3] Modify post function to accept delay parameter --- include/fschuetz04/simcpp20/simulation.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/fschuetz04/simcpp20/simulation.hpp b/include/fschuetz04/simcpp20/simulation.hpp index a745241..fbf39c5 100644 --- a/include/fschuetz04/simcpp20/simulation.hpp +++ b/include/fschuetz04/simcpp20/simulation.hpp @@ -65,14 +65,15 @@ template class simulation { } /** - * Posts a callback to be executed as soon as possible. + * Posts a callback to be executed after the delay. * @param callback Callback to invoke, which has the following signature: `void callback()`. + * @param delay Delay before executing the callback. If default constructed, execute as soon as possible. * @return New pending event. */ - event_type post(std::function callback) { + event_type post(std::function callback, Time delay = Time{0}) { auto ev = event(); ev.add_callback(callback); - schedule(ev); + schedule(ev, delay); return ev; } From 3523e07e770129cd9ae154eaf1cfafafb88ad3ce Mon Sep 17 00:00:00 2001 From: Marco Satti Date: Thu, 19 Mar 2026 11:52:34 +0000 Subject: [PATCH 3/3] Add test in for post with delay --- tests/tests.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/tests.cpp b/tests/tests.cpp index cc5d672..ce34e49 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -220,4 +220,22 @@ TEST_CASE("post event ordering") { REQUIRE(ran2); REQUIRE(ran3); } +} + +TEST_CASE("post event with delay to simulation") { + simcpp20::simulation<> sim; + + bool ran1 = false; + bool ran2 = false; + + auto ev1 = sim.post([&ran1]() { ran1 = true; }, 2.0); + auto ev2 = sim.post([&ran2]() { ran2 = true; }, 1.0); + + sim.step(); + REQUIRE_FALSE(ran1); + REQUIRE(ran2); + + sim.step(); + REQUIRE(ran1); + REQUIRE(ran2); } \ No newline at end of file