Skip to content

Commit

Permalink
Make sure binary operators are const to fix C++20 (#33033)
Browse files Browse the repository at this point in the history
Fix errors of the form

../../src/system/tests/TestSystemPacketBuffer.cpp:1063:72: error: C++20 says that these are ambiguous, even though the second is reversed: [-Werror]
 1063 |                             NL_TEST_ASSERT(inSuite, config_1.handle == original_handle_1);
      |                                                                        ^~~~~~~~~~~~~~~~~
../../third_party/nlunit-test/repo/src/nlunit-test.h:366:15: note: in definition of macro ‘NL_TEST_ASSERT’
  366 |         if (!(inCondition))                             \
      |               ^~~~~~~~~~~
../../src/system/SystemPacketBuffer.h:690:10: note: candidate 1: ‘bool chip::System::PacketBufferHandle::operator==(const chip::System::PacketBufferHandle&)’
  690 |     bool operator==(const PacketBufferHandle & aOther) { return mBuffer == aOther.mBuffer; }
      |          ^~~~~~~~
../../src/system/SystemPacketBuffer.h:690:10: note: candidate 2: ‘bool chip::System::PacketBufferHandle::operator==(const chip::System::PacketBufferHandle&)’ (reversed)
../../src/system/SystemPacketBuffer.h:690:10: note: try making the operator a ‘const’ member function

when compiling with cpp_standard="gnu++20" by making sure comparators
are const members.
  • Loading branch information
mspang authored Apr 18, 2024
1 parent 4322a88 commit 84fd7fc
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class TargetVideoPlayerInfo
public:
TargetVideoPlayerInfo() {}

bool operator==(const TargetVideoPlayerInfo & other) { return this->mNodeId == other.mNodeId; }
bool operator==(const TargetVideoPlayerInfo & other) const { return this->mNodeId == other.mNodeId; }

bool IsInitialized() { return mInitialized; }
void Reset();
Expand Down
5 changes: 4 additions & 1 deletion src/app/ConcreteAttributePath.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ struct ConcreteDataAttributePath : public ConcreteAttributePath
ChipLogValueMEI(mClusterId), ChipLogValueMEI(mAttributeId));
}

bool MatchesConcreteAttributePath(const ConcreteAttributePath & aOther) { return ConcreteAttributePath::operator==(aOther); }
bool MatchesConcreteAttributePath(const ConcreteAttributePath & aOther) const
{
return ConcreteAttributePath::operator==(aOther);
}

bool operator==(const ConcreteDataAttributePath & aOther) const
{
Expand Down
4 changes: 2 additions & 2 deletions src/app/clusters/bindings/PendingNotificationMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ class PendingNotificationMap
return *this;
}

bool operator!=(const Iterator & rhs) { return mIndex != rhs.mIndex; }
bool operator!=(const Iterator & rhs) const { return mIndex != rhs.mIndex; }

bool operator==(const Iterator & rhs) { return mIndex == rhs.mIndex; }
bool operator==(const Iterator & rhs) const { return mIndex == rhs.mIndex; }

private:
PendingNotificationMap * mMap;
Expand Down
4 changes: 2 additions & 2 deletions src/app/clusters/scenes-server/SceneTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class SceneTable

bool IsValid() { return (mSceneId != kUndefinedSceneId); }

bool operator==(const SceneStorageId & other) { return (mGroupId == other.mGroupId && mSceneId == other.mSceneId); }
bool operator==(const SceneStorageId & other) const { return (mGroupId == other.mGroupId && mSceneId == other.mSceneId); }
};

/// @brief struct used to store data held in a scene
Expand Down Expand Up @@ -235,7 +235,7 @@ class SceneTable
SceneTableEntry(SceneStorageId id) : mStorageId(id) {}
SceneTableEntry(const SceneStorageId id, const SceneData data) : mStorageId(id), mStorageData(data) {}

bool operator==(const SceneTableEntry & other)
bool operator==(const SceneTableEntry & other) const
{
return (mStorageId == other.mStorageId && mStorageData == other.mStorageData);
}
Expand Down
4 changes: 2 additions & 2 deletions src/credentials/FabricTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ class ConstFabricIterator
return GetCurrent();
}

bool operator==(const ConstFabricIterator & other)
bool operator==(const ConstFabricIterator & other) const
{
if (IsAtEnd())
{
Expand All @@ -308,7 +308,7 @@ class ConstFabricIterator
// Pending entry does not participate in finding this.
return (mStart == other.mStart) && (mIndex == other.mIndex) && (mMaxSize == other.mMaxSize);
}
bool operator!=(const ConstFabricIterator & other) { return !(*this == other); }
bool operator!=(const ConstFabricIterator & other) const { return !(*this == other); }

bool IsAtEnd() const { return (mIndex == mMaxSize); }

Expand Down
4 changes: 2 additions & 2 deletions src/credentials/GroupDataProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class GroupDataProvider
Platform::CopyString(name, groupName);
}
}
bool operator==(const GroupInfo & other)
bool operator==(const GroupInfo & other) const
{
return (this->group_id == other.group_id) && !strncmp(this->name, other.name, kGroupNameMax);
}
Expand Down Expand Up @@ -151,7 +151,7 @@ class GroupDataProvider
// Number of keys present
uint8_t num_keys_used = 0;

bool operator==(const KeySet & other)
bool operator==(const KeySet & other) const
{
VerifyOrReturnError(this->policy == other.policy && this->num_keys_used == other.num_keys_used, false);
return !memcmp(this->epoch_keys, other.epoch_keys, this->num_keys_used * sizeof(EpochKey));
Expand Down
15 changes: 8 additions & 7 deletions src/setup_payload/SetupPayload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ bool PayloadContents::CheckPayloadCommonConstraints() const
return true;
}

bool PayloadContents::operator==(PayloadContents & input) const
bool PayloadContents::operator==(const PayloadContents & input) const
{
return (this->version == input.version && this->vendorID == input.vendorID && this->productID == input.productID &&
this->commissioningFlow == input.commissioningFlow && this->rendezvousInformation == input.rendezvousInformation &&
Expand Down Expand Up @@ -257,10 +257,11 @@ CHIP_ERROR SetupPayload::addOptionalExtensionData(const OptionalQRCodeInfoExtens
return CHIP_NO_ERROR;
}

CHIP_ERROR SetupPayload::getOptionalVendorData(uint8_t tag, OptionalQRCodeInfo & info)
CHIP_ERROR SetupPayload::getOptionalVendorData(uint8_t tag, OptionalQRCodeInfo & info) const
{
VerifyOrReturnError(optionalVendorData.find(tag) != optionalVendorData.end(), CHIP_ERROR_KEY_NOT_FOUND);
info = optionalVendorData[tag];
const auto it = optionalVendorData.find(tag);
VerifyOrReturnError(it != optionalVendorData.end(), CHIP_ERROR_KEY_NOT_FOUND);
info = it->second;

return CHIP_NO_ERROR;
}
Expand All @@ -273,7 +274,7 @@ CHIP_ERROR SetupPayload::getOptionalExtensionData(uint8_t tag, OptionalQRCodeInf
return CHIP_NO_ERROR;
}

optionalQRCodeInfoType SetupPayload::getNumericTypeFor(uint8_t tag)
optionalQRCodeInfoType SetupPayload::getNumericTypeFor(uint8_t tag) const
{
optionalQRCodeInfoType elemType = optionalQRCodeInfoTypeUnknown;

Expand All @@ -289,7 +290,7 @@ optionalQRCodeInfoType SetupPayload::getNumericTypeFor(uint8_t tag)
return elemType;
}

std::vector<OptionalQRCodeInfoExtension> SetupPayload::getAllOptionalExtensionData()
std::vector<OptionalQRCodeInfoExtension> SetupPayload::getAllOptionalExtensionData() const
{
std::vector<OptionalQRCodeInfoExtension> returnedOptionalInfo;
for (auto & entry : optionalExtensionData)
Expand All @@ -299,7 +300,7 @@ std::vector<OptionalQRCodeInfoExtension> SetupPayload::getAllOptionalExtensionDa
return returnedOptionalInfo;
}

bool SetupPayload::operator==(SetupPayload & input)
bool SetupPayload::operator==(const SetupPayload & input) const
{
std::vector<OptionalQRCodeInfo> inputOptionalVendorData;
std::vector<OptionalQRCodeInfoExtension> inputOptionalExtensionData;
Expand Down
10 changes: 5 additions & 5 deletions src/setup_payload/SetupPayload.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ struct PayloadContents

bool isValidQRCodePayload() const;
bool isValidManualCode() const;
bool operator==(PayloadContents & input) const;
bool operator==(const PayloadContents & input) const;

static bool IsValidSetupPIN(uint32_t setupPIN);

Expand Down Expand Up @@ -233,7 +233,7 @@ class SetupPayload : public PayloadContents
**/
CHIP_ERROR removeSerialNumber();

bool operator==(SetupPayload & input);
bool operator==(const SetupPayload & input) const;

private:
std::map<uint8_t, OptionalQRCodeInfo> optionalVendorData;
Expand Down Expand Up @@ -267,14 +267,14 @@ class SetupPayload : public PayloadContents
* @brief A function to retrieve the vector of CHIPQRCodeInfo infos
* @return Returns a vector of CHIPQRCodeInfos
**/
std::vector<OptionalQRCodeInfoExtension> getAllOptionalExtensionData();
std::vector<OptionalQRCodeInfoExtension> getAllOptionalExtensionData() const;

/** @brief A function to retrieve an optional QR Code info vendor object
* @param tag 7 bit [0-127] tag number
* @param info retrieved OptionalQRCodeInfo object
* @return Returns a CHIP_ERROR_KEY_NOT_FOUND on error, CHIP_NO_ERROR otherwise
**/
CHIP_ERROR getOptionalVendorData(uint8_t tag, OptionalQRCodeInfo & info);
CHIP_ERROR getOptionalVendorData(uint8_t tag, OptionalQRCodeInfo & info) const;

/** @brief A function to retrieve an optional QR Code info extended object
* @param tag 8 bit [128-255] tag number
Expand All @@ -287,7 +287,7 @@ class SetupPayload : public PayloadContents
* @param tag 8 bit [0-255] tag number
* @return Returns an optionalQRCodeInfoType value
**/
optionalQRCodeInfoType getNumericTypeFor(uint8_t tag);
optionalQRCodeInfoType getNumericTypeFor(uint8_t tag) const;
};

} // namespace chip
2 changes: 1 addition & 1 deletion src/system/SystemPacketBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ class DLL_EXPORT PacketBufferHandle

PacketBuffer * Get() const { return mBuffer; }

bool operator==(const PacketBufferHandle & aOther) { return mBuffer == aOther.mBuffer; }
bool operator==(const PacketBufferHandle & aOther) const { return mBuffer == aOther.mBuffer; }

#if CHIP_SYSTEM_PACKETBUFFER_HAS_RIGHTSIZE
void InternalRightSize();
Expand Down

0 comments on commit 84fd7fc

Please sign in to comment.