Skip to content

Commit

Permalink
Add jngl::removeJob(Job*)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Feb 8, 2024
1 parent 30d6e3b commit f38a747
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/jngl/job.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2023 Jan Niklas Hasse <[email protected]>
// Copyright 2012-2024 Jan Niklas Hasse <[email protected]>
// For conditions of distribution and use, see copyright notice in LICENSE.txt

#include "job.hpp"
Expand All @@ -13,6 +13,10 @@ void addJob(std::shared_ptr<Job> job) {
return pWindow->addJob(std::move(job));
}

void removeJob(Job* job) {
pWindow->removeJob(job);
}

std::shared_ptr<Job> getJob(const std::function<bool(Job&)>& predicate) {
return pWindow->getJob(predicate);
}
Expand Down
5 changes: 5 additions & 0 deletions src/jngl/job.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ void addJob(Args&&... args) {
addJob(std::make_shared<T>(std::forward<Args>(args)...));
}

/// Removes the passed Job after all Jobs have been stepped
///
/// If the Job isn't found, nothing happens.
void removeJob(Job*);

/// Returns the first Job for which \a predicate returned true
std::shared_ptr<Job> getJob(const std::function<bool(Job&)>& predicate);

Expand Down
18 changes: 15 additions & 3 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,20 @@ void Window::stepIfNeeded() {
#ifdef JNGL_PERFORMANCE_OVERLAY
auto start = std::chrono::steady_clock::now();
#endif
if (currentWork_) {
currentWork_->step();
}
for (auto& job : jobs) {
job->step();
}
for (auto job : jobsToRemove) {
const auto it = std::find_if(jobs.begin(), jobs.end(),
[job](const auto& p) { return p.get() == job; });
if (it != jobs.end()) {
jobs.erase(it);
}
}
jobsToRemove.clear();
if (currentWork_) {
currentWork_->step();
}
#ifdef JNGL_PERFORMANCE_OVERLAY
lastStepDuration = static_cast<double>(
std::chrono::duration_cast<std::chrono::microseconds>(
Expand Down Expand Up @@ -447,6 +455,10 @@ void Window::addJob(std::shared_ptr<Job> job) {
jobs.emplace_back(std::move(job));
}

void Window::removeJob(Job* job) {
jobsToRemove.emplace_back(job);
}

std::shared_ptr<Job> Window::getJob(const std::function<bool(Job&)>& predicate) const {
for (const auto& job : jobs) {
if (predicate(*job)) {
Expand Down
2 changes: 2 additions & 0 deletions src/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class Window {
void draw() const;
std::shared_ptr<Work> getWork();
void addJob(std::shared_ptr<Job>);
void removeJob(Job*);
std::shared_ptr<Job> getJob(const std::function<bool(Job&)>& predicate) const;
void resetFrameLimiter();
unsigned int getStepsPerSecond() const;
Expand Down Expand Up @@ -164,6 +165,7 @@ class Window {
bool changeWork = false;
std::shared_ptr<Work> newWork_;
std::vector<std::shared_ptr<Job>> jobs;
std::vector<Job*> jobsToRemove;
unsigned int stepsPerFrame;
double sleepPerFrame = 0; // in seconds
double sleepCorrectionFactor;
Expand Down

0 comments on commit f38a747

Please sign in to comment.