Преглед изворни кода

Use ranged for loops in ExchangeManager. (#6982)

The resulting code is clearer and makes some other changes simpler.
Boris Zbarsky пре 4 година
родитељ
комит
d87cdb75fa
1 измењених фајлова са 14 додато и 20 уклоњено
  1. 14 20
      src/messaging/ExchangeMgr.cpp

+ 14 - 20
src/messaging/ExchangeMgr.cpp

@@ -146,19 +146,18 @@ void ExchangeManager::OnReceiveError(CHIP_ERROR error, const Transport::PeerAddr
 
 CHIP_ERROR ExchangeManager::RegisterUMH(Protocols::Id protocolId, int16_t msgType, ExchangeDelegateBase * delegate)
 {
-    UnsolicitedMessageHandler * umh      = UMHandlerPool;
     UnsolicitedMessageHandler * selected = nullptr;
 
-    for (int i = 0; i < CHIP_CONFIG_MAX_UNSOLICITED_MESSAGE_HANDLERS; i++, umh++)
+    for (auto & umh : UMHandlerPool)
     {
-        if (!umh->IsInUse())
+        if (!umh.IsInUse())
         {
             if (selected == nullptr)
-                selected = umh;
+                selected = &umh;
         }
-        else if (umh->Matches(protocolId, msgType))
+        else if (umh.Matches(protocolId, msgType))
         {
-            umh->Delegate = delegate;
+            umh.Delegate = delegate;
             return CHIP_NO_ERROR;
         }
     }
@@ -177,13 +176,11 @@ CHIP_ERROR ExchangeManager::RegisterUMH(Protocols::Id protocolId, int16_t msgTyp
 
 CHIP_ERROR ExchangeManager::UnregisterUMH(Protocols::Id protocolId, int16_t msgType)
 {
-    UnsolicitedMessageHandler * umh = UMHandlerPool;
-
-    for (int i = 0; i < CHIP_CONFIG_MAX_UNSOLICITED_MESSAGE_HANDLERS; i++, umh++)
+    for (auto & umh : UMHandlerPool)
     {
-        if (umh->IsInUse() && umh->Matches(protocolId, msgType))
+        if (umh.IsInUse() && umh.Matches(protocolId, msgType))
         {
-            umh->Reset();
+            umh.Reset();
             SYSTEM_STATS_DECREMENT(chip::System::Stats::kExchangeMgr_NumUMHandlers);
             return CHIP_NO_ERROR;
         }
@@ -197,7 +194,6 @@ void ExchangeManager::OnMessageReceived(const PacketHeader & packetHeader, const
                                         System::PacketBufferHandle && msgBuf, SecureSessionMgr * msgLayer)
 {
     CHIP_ERROR err                          = CHIP_NO_ERROR;
-    UnsolicitedMessageHandler * umh         = nullptr;
     UnsolicitedMessageHandler * matchingUMH = nullptr;
     bool sendAckAndCloseExchange            = false;
 
@@ -236,22 +232,20 @@ void ExchangeManager::OnMessageReceived(const PacketHeader & packetHeader, const
     {
         // Search for an unsolicited message handler that can handle the message. Prefer handlers that can explicitly
         // handle the message type over handlers that handle all messages for a profile.
-        umh = (UnsolicitedMessageHandler *) UMHandlerPool;
-
         matchingUMH = nullptr;
 
-        for (int i = 0; i < CHIP_CONFIG_MAX_UNSOLICITED_MESSAGE_HANDLERS; i++, umh++)
+        for (auto & umh : UMHandlerPool)
         {
-            if (umh->IsInUse() && payloadHeader.HasProtocol(umh->ProtocolId))
+            if (umh.IsInUse() && payloadHeader.HasProtocol(umh.ProtocolId))
             {
-                if (umh->MessageType == payloadHeader.GetMessageType())
+                if (umh.MessageType == payloadHeader.GetMessageType())
                 {
-                    matchingUMH = umh;
+                    matchingUMH = &umh;
                     break;
                 }
 
-                if (umh->MessageType == kAnyMessageType)
-                    matchingUMH = umh;
+                if (umh.MessageType == kAnyMessageType)
+                    matchingUMH = &umh;
             }
         }
     }