You can abstract out the thing that changes, the comparison expression, into a predicate for a template function.
class PriorityMatch
{
int priority_;
public:
explicit PriorityMatch(int priority)
: priority_(priority) {}
bool operator()(const Job& job)
{
return job->priority == priority_;
}
};
class DescriptionMatch
{
std::string substring_;
public:
explicit PriorityMatchDescriptionMatch(intconst std::string& substring)
: substring_(substring) {}
bool operator()(const Job& job)
{
return job->description.find(substring_) != std::string::npos;
}
};
template<class Predicate>
int Queue::remove(Predicate predicate)
{
// Loop through the queue list
if (predicate(current_job)) {
// Remove job.
}
// Return the amount of removed jobs.
}
int Queue::remove(const string& substring)
{
return remove(DescriptionMatch(substring));
}
int Queue::remove(int priority)
{
return remove(PriorityMatch(substring));
}
Or in C++11:
template<class Predicate>
int Queue::remove(Predicate predicate)
{
// Loop through the queue list
if (predicate(current_job)) {
// Remove job.
}
// Return the amount of removed jobs.
}
int Queue::remove(const string& substring)
{
return remove([](const Job& job){
return job->description.find(substring) != std::string::npos;
});
}
int Queue::remove(int priority)
{
return remove([](const Job& job){
return job->priority == priority;
});
}