MXS-1503: Add EqualPointees<T> template

The templated provides an unary predicate for (shared) pointers that
allows containers containing pointers to objects to be searched, not by
pointer comparisons but by comparisons of the values that they point
to. This allows std::find_if to be used with shared pointers in a similar
manner that std::find is used with non-pointer objects.
This commit is contained in:
Markus Mäkelä
2018-03-29 17:24:52 +03:00
parent 14e399dd68
commit 98fb2cf5ed
2 changed files with 27 additions and 16 deletions

View File

@ -89,22 +89,6 @@ public:
*/ */
bool eq(const SessionCommand& rhs) const; bool eq(const SessionCommand& rhs) const;
class Equals: public std::unary_function<const SSessionCommand&, bool>
{
public:
Equals(const SSessionCommand& base):
m_base(base)
{}
bool operator ()(const SSessionCommand& rhs)
{
return m_base->eq(*rhs);
}
private:
const SSessionCommand& m_base;
};
private: private:
mxs::Buffer m_buffer; /**< The buffer containing the command */ mxs::Buffer m_buffer; /**< The buffer containing the command */
uint8_t m_command; /**< The command being executed */ uint8_t m_command; /**< The command being executed */

View File

@ -268,4 +268,31 @@ private:
ContainerType m_registry; ContainerType m_registry;
}; };
// binary compare of pointed-to objects
template<typename Ptr>
bool equal_pointees(const Ptr& lhs, const Ptr& rhs)
{
return *lhs == *rhs;
}
// Unary predicate for equality of pointed-to objects
template<typename T>
class EqualPointees
{
public:
EqualPointees(const T& lhs) : m_ppLhs(&lhs) {}
bool operator()(const T& pRhs)
{
return **m_ppLhs == *pRhs;
}
private:
const T* m_ppLhs;
};
template<typename T>
EqualPointees<T> equal_pointees(const T& t)
{
return EqualPointees<T>(t);
}
} }