diff --git a/src/core/common/clearable.hpp b/src/core/common/clearable.hpp index 679f35c60..69ee467be 100644 --- a/src/core/common/clearable.hpp +++ b/src/core/common/clearable.hpp @@ -66,8 +66,13 @@ template void ClearAllBytes(ObjectType &aObject) */ template class Clearable { + friend Type; + public: void Clear(void) { ClearAllBytes(*static_cast(this)); } + +private: + Clearable(void) = default; }; } // namespace ot diff --git a/src/core/common/equatable.hpp b/src/core/common/equatable.hpp index f5cdc5f75..9f25f4333 100644 --- a/src/core/common/equatable.hpp +++ b/src/core/common/equatable.hpp @@ -50,6 +50,8 @@ namespace ot { */ template class Unequatable { + friend Type; + public: /** * Overloads operator `!=` to evaluate whether or not two instances of `Type` are equal. @@ -62,6 +64,9 @@ public: * @retval FALSE If the two `Type` instances are equal. */ bool operator!=(const Type &aOther) const { return !(*static_cast(this) == aOther); } + +private: + Unequatable(void) = default; }; /** @@ -72,8 +77,10 @@ public: * Users of this class should follow CRTP-style inheritance, i.e., the `Type` class itself should publicly inherit * from `Equatable`. */ -template class Equatable : public Unequatable +template class Equatable { + friend Type; + public: /** * Overloads operator `==` to evaluate whether or not two instances of `Type` are equal. @@ -87,6 +94,19 @@ public: { return memcmp(static_cast(this), &aOther, sizeof(Type)) == 0; } + + /** + * 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 !(*static_cast(this) == aOther); } + +private: + Equatable(void) = default; }; } // namespace ot