From 5911ae9db18c3eea8f3a21af676b18a57f619520 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 24 Mar 2021 11:11:50 -0700 Subject: [PATCH] [equatable] 'Unequatable' class adding overload of operator `!=` (#6340) This commit adds `Unequatable` class which adds an overload of operator `!=` using an existing `==` overload provided by the `Type` class. The `Unequatable` should follow CRTP-style inheritance, i.e., the `Type` class should publicly inherit from `Unequatable`. This provides a solution to avoid repeating the simple implementation of `operator!=()` overload in different classes. --- src/core/common/equatable.hpp | 39 ++++++++++++++++++++++---------- src/core/common/settings.hpp | 13 +---------- src/core/common/time.hpp | 15 +++--------- src/core/mac/channel_mask.hpp | 13 ++--------- src/core/meshcop/meshcop.hpp | 27 +++------------------- src/core/net/ip6_address.hpp | 13 +---------- src/core/net/socket.hpp | 14 ++---------- src/core/thread/network_data.hpp | 26 ++------------------- src/core/thread/topology.hpp | 16 ++----------- 9 files changed, 43 insertions(+), 133 deletions(-) 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