我将从一个例子开始:
class ThreadManger{
public:
...
private:
// First predicate
class MyThreadDereferencedEqual
{
public:
MyThreadDereferencedEqual(const MyThread* toFind) : toFind_(toFind) { }
bool operator() (const MyThread* other) const { return *toFind_ == *other; }
private:
const MyThread* toFind_;
};
// Second predicate
struct MyThreadIsCurrent
{
bool operator() (MyThread* theThread) const { return theThread->isCurrentThread(); }
};
std::vector<MyThread*>::iterator findThreadHandle(MyThread* threadHandle)
{
assert(threadHandle);
return std::find_if(vThreadHandles_.begin(), vThreadHandles_.end(), MyThreadDereferencedEqual(threadHandle));
}
std::vector<MyThread*>::iterator findCurrentThread()
{
return std::find_if(vThreadHandles_.begin(), vThreadHandles_.end(), MyThreadIsCurrent());
}
...
}
我应该把这些谓词放在哪里?
对于数字4)如果我想将这些谓词概括为重用并使它们模板化,我会改变:
第一个谓词:
template<typename T>
class DereferencedEqual
{
public:
DereferencedEqual(const T* toFind) : toFind_(toFind) { }
bool operator() (const T* other) const { return *toFind_ == *other; }
private:
const T* toFind_;
};
第二个谓词:
// Have no idea how to make this generic ...