diff --git a/src/core/common/equatable.hpp b/src/core/common/equatable.hpp index 83ab05b9e..06fdda993 100644 --- a/src/core/common/equatable.hpp +++ b/src/core/common/equatable.hpp @@ -40,6 +40,32 @@ namespace ot { +/** + * This template class defines an overload of operator `!=`. + * + * The `!=` implementation uses an existing `==` overload provided by the `Type` class. + * + * Users of this class should follow CRTP-style inheritance, i.e., the `Type` class itself should publicly inherit + * from `Unequatable`. + * + */ +template class Unequatable +{ +public: + /** + * This method overloads operator `!=` to evaluate whether or not two instances of `Type` are equal. + * + * This is implemented in terms of an existing `==` overload provided by `Type` class itself. + * + * @param[in] aOther The other `Type` instance to compare with. + * + * @retval TRUE If the two `Type` instances are not equal. + * @retval FALSE If the two `Type` instances are equal. + * + */ + bool operator!=(const Type &aOther) const { return !(*static_cast(this) == aOther); } +}; + /** * This template class defines overloads of operators `==` and `!=`. * @@ -49,7 +75,7 @@ namespace ot { * from `Equatable`. * */ -template class Equatable +template class Equatable : public Unequatable { public: /** @@ -62,17 +88,6 @@ public: * */ bool operator==(const Type &aOther) const { return memcmp(this, &aOther, sizeof(Type)) == 0; } - - /** - * This method overloads operator `!=` to evaluate whether or not two instances of `Type` are equal. - * - * @param[in] aOther The other `Type` instance to compare with. - * - * @retval TRUE If the two `Type` instances are not equal. - * @retval FALSE If the two `Type` instances are equal. - * - */ - bool operator!=(const Type &aOther) const { return !(*this == aOther); } }; } // namespace ot diff --git a/src/core/common/settings.hpp b/src/core/common/settings.hpp index 97265fd55..929fef453 100644 --- a/src/core/common/settings.hpp +++ b/src/core/common/settings.hpp @@ -870,7 +870,7 @@ public: * This class defines an iterator to access all Child Info entries in the settings. * */ - class ChildInfoIterator : public SettingsBase + class ChildInfoIterator : public SettingsBase, public Unequatable { friend class ChildInfoIteratorBuilder; @@ -955,17 +955,6 @@ public: return (mIsDone && aOther.mIsDone) || (!mIsDone && !aOther.mIsDone && (mIndex == aOther.mIndex)); } - /** - * This method overloads operator `!=` to evaluate whether or not two iterator instances are unequal. - * - * @param[in] aOther The other iterator to compare with. - * - * @retval TRUE If the two iterator objects are unequal. - * @retval FALSE If the two iterator objects are not unequal. - * - */ - bool operator!=(const ChildInfoIterator &aOther) const { return !(*this == aOther); } - private: enum IteratorType { diff --git a/src/core/common/time.hpp b/src/core/common/time.hpp index 2f63e618a..98c299bd6 100644 --- a/src/core/common/time.hpp +++ b/src/core/common/time.hpp @@ -39,6 +39,8 @@ #include #include +#include "common/equatable.hpp" + namespace ot { /** @@ -55,7 +57,7 @@ namespace ot { * This class represents a time instance. * */ -class Time +class Time : public Unequatable