写得用于比较的函数对象模板

#include "bdc_share_ptr.hpp"

template <typename IDTYPE, typename ITEMTYPE, typename GETIDFUN>
class ItemCompareById
{
    typedef boost::shared_ptr<ITEMTYPE> p_ITEMTYPE_T;
  public:
    explicit ItemCompareById(IDTYPE id, GETIDFUN dun);
    ItemCompareById(const ItemCompareById &compare);
    bool operator()(p_ITEMTYPE_T &p_obj) ;
    bool operator()(ITEMTYPE *p_obj) ;
    bool operator()(ITEMTYPE &p_obj) ;
  private:
    ItemCompareById &operator = (const ItemCompareById &compare);
    IDTYPE id_;
    GETIDFUN fun_;
};

template <typename IDTYPE, typename ITEMTYPE, typename GETIDFUN>
ItemCompareById<IDTYPE, ITEMTYPE, GETIDFUN>::ItemCompareById(IDTYPE id, GETIDFUN fun)
  : id_(id), fun_(fun)
{
}

template<typename IDTYPE, typename ITEMTYPE, typename GETIDFUN>
ItemCompareById<IDTYPE, ITEMTYPE, GETIDFUN>::ItemCompareById(const ItemCompareById &compare)
  : id_(compare.id_), fun_(compare.fun_)
{
}

template <typename IDTYPE, typename ITEMTYPE, typename GETIDFUN>
bool ItemCompareById<IDTYPE, ITEMTYPE, GETIDFUN>::operator()(ITEMTYPE &obj)
{
  return (obj.*fun_)() == id_;
}

template <typename IDTYPE, typename ITEMTYPE, typename GETIDFUN>
bool ItemCompareById<IDTYPE, ITEMTYPE, GETIDFUN>::operator()(p_ITEMTYPE_T &p_obj)
{
  return (p_obj != 0) && ((p_obj.get()->*fun_)() == id_);
}

template <typename IDTYPE, typename ITEMTYPE, typename GETIDFUN>
bool ItemCompareById<IDTYPE, ITEMTYPE, GETIDFUN>::operator()(ITEMTYPE *p_obj)
{
  return (p_obj != 0) && ((p_obj->*fun_)() == id_);
}

你可能感兴趣的:(include,fun)