Skip to content

Commit

Permalink
stock/BasicStock: use std::predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Oct 16, 2023
1 parent 2b737aa commit 9ae5021
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
11 changes: 5 additions & 6 deletions src/stock/BasicStock.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "util/DeleteDisposer.hxx"
#include "util/IntrusiveList.hxx"

#include <concepts> // for std::predicate
#include <cstddef>
#include <cstdint>
#include <string>
Expand Down Expand Up @@ -128,13 +129,12 @@ public:
* Destroy all matching idle items and don't reuse any of the
* matching busy items.
*/
template<typename P>
void FadeIf(P &&predicate) noexcept {
void FadeIf(std::predicate<const StockItem &> auto predicate) noexcept {
for (auto &i : busy)
if (predicate(i))
i.Fade();

ClearIdleIf(std::forward<P>(predicate));
ClearIdleIf(predicate);

ScheduleCheckEmpty();
// TODO: restart the "num_create" list?
Expand Down Expand Up @@ -174,9 +174,8 @@ private:

void ClearIdle() noexcept;

template<typename P>
void ClearIdleIf(P &&predicate) noexcept {
idle.remove_and_dispose_if(std::forward<P>(predicate),
void ClearIdleIf(std::predicate<const StockItem &> auto predicate) noexcept {
idle.remove_and_dispose_if(predicate,
DeleteDisposer());

if (idle.size() <= max_idle)
Expand Down
4 changes: 2 additions & 2 deletions src/stock/MapStock.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "util/StringAPI.hxx"

#include <cassert>
#include <concepts> // for std::predicate
#include <cstddef>

/**
Expand Down Expand Up @@ -106,8 +107,7 @@ public:
/**
* @see Stock::FadeIf()
*/
template<typename P>
void FadeIf(P &&predicate) noexcept {
void FadeIf(std::predicate<const StockItem &> auto predicate) noexcept {
map.for_each([&predicate](auto &i){
i.stock.FadeIf(predicate);
});
Expand Down
10 changes: 4 additions & 6 deletions src/stock/MultiStock.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "util/IntrusiveHashSet.hxx"
#include "util/IntrusiveList.hxx"

#include <concepts> // for std::predicate
#include <string>

class CancellablePointer;
Expand Down Expand Up @@ -106,8 +107,7 @@ class MultiStock {

void Fade() noexcept;

template<typename P>
void FadeIf(P &&predicate) noexcept {
void FadeIf(std::predicate<const StockItem &> auto predicate) noexcept {
if (predicate(shared_item))
Fade();
}
Expand Down Expand Up @@ -246,8 +246,7 @@ class MultiStock {
i.Fade();
}

template<typename P>
void FadeIf(P &&predicate) noexcept {
void FadeIf(std::predicate<const StockItem &> auto predicate) noexcept {
for (auto &i : items)
i.FadeIf(predicate);
}
Expand Down Expand Up @@ -388,8 +387,7 @@ public:
/**
* @see Stock::FadeIf()
*/
template<typename P>
void FadeIf(P &&predicate) noexcept {
void FadeIf(std::predicate<const StockItem &> auto predicate) noexcept {
map.for_each([&predicate](auto &i){
i.FadeIf(predicate);
});
Expand Down

0 comments on commit 9ae5021

Please sign in to comment.