From 474c84967c9b64c93146e0caa7658412c3bd9042 Mon Sep 17 00:00:00 2001 From: Erez Geva Date: Mon, 20 Jan 2025 12:47:56 +0100 Subject: [PATCH] clkmgr: replace iterator type with automatic. As the iterator type must be derived from the object it serve. It is easier to use automatic type. Use range-based loop. Use iterator with normal-for loop when a non-smooth range loop is required. Signed-off-by: Erez Geva --- clkmgr/client/notification_msg.cpp | 4 +--- clkmgr/client/subscribe_msg.cpp | 29 +++++++++++------------------ clkmgr/common/message.cpp | 14 ++++++-------- clkmgr/common/transport.cpp | 14 ++++++-------- clkmgr/proxy/client.cpp | 2 +- 5 files changed, 25 insertions(+), 38 deletions(-) diff --git a/clkmgr/client/notification_msg.cpp b/clkmgr/client/notification_msg.cpp index 9722525d..49b64397 100644 --- a/clkmgr/client/notification_msg.cpp +++ b/clkmgr/client/notification_msg.cpp @@ -103,11 +103,9 @@ PROCESS_MESSAGE_TYPE(ClientNotificationMessage::processMessage) PrintDebug("[ClientNotificationMessage]::processMessage "); bool old_composite_event; /* Need to walk thru the whole vector */ - std::vector ::iterator it ; - for(it = ClientStateArray.begin(); it < ClientStateArray.end(); it++) { + for(const auto ¤tClientState : ClientStateArray) { uint32_t eventSub; uint32_t composite_eventSub; - ClientState *currentClientState = *it; timespec last_notification_time = {}; if(clock_gettime(CLOCK_REALTIME, &last_notification_time) == -1) PrintDebug("ClientNotificationMessage::processMessage \ diff --git a/clkmgr/client/subscribe_msg.cpp b/clkmgr/client/subscribe_msg.cpp index e59f50d0..52df2f67 100644 --- a/clkmgr/client/subscribe_msg.cpp +++ b/clkmgr/client/subscribe_msg.cpp @@ -82,9 +82,8 @@ PARSE_RXBUFFER_TYPE(ClientSubscribeMessage::parseBuffer) 2. to move some/all processing inside the processMessage instead of here. */ sessionId_t currentSessionID = currentClientState->get_sessionId(); - std::map >::iterator it; client_ptp_event *client_data, *composite_client_data; - it = client_ptp_event_map.find(currentSessionID); + const auto &it = client_ptp_event_map.find(currentSessionID); if(it == client_ptp_event_map.end()) { /* Creation of a new map item for this new sessionID */ client_data = new client_ptp_event(); @@ -187,9 +186,8 @@ PROCESS_MESSAGE_TYPE(ClientSubscribeMessage::processMessage) /* delete the client ptp event based on session ID given */ void ClientSubscribeMessage::deleteClientPtpEventStruct(sessionId_t sID) { - std::map >::iterator it; client_ptp_event *client_data, *composite_data; - it = client_ptp_event_map.find(sID); + const auto &it = client_ptp_event_map.find(sID); if(it != client_ptp_event_map.end()) { client_data = it->second[0]; composite_data = it->second[1]; @@ -201,36 +199,31 @@ void ClientSubscribeMessage::deleteClientPtpEventStruct(sessionId_t sID) } /* get the corresponding ClientPtpEvent */ -client_ptp_event -*ClientSubscribeMessage::getClientPtpEventStruct(sessionId_t sID) +client_ptp_event *ClientSubscribeMessage::getClientPtpEventStruct( + sessionId_t sID) { - std::map >::iterator it; - client_ptp_event *client_data = nullptr; - it = client_ptp_event_map.find(sID); + const auto &it = client_ptp_event_map.find(sID); if(it != client_ptp_event_map.end()) - client_data = it->second[0]; - return client_data; + return it->second[0]; + return nullptr; } /* get the corresponding ClientPtpEvent for composite */ client_ptp_event *ClientSubscribeMessage::getClientPtpEventCompositeStruct( sessionId_t sID) { - std::map >::iterator it; - client_ptp_event *client_data = nullptr; - it = client_ptp_event_map.find(sID); + const auto &it = client_ptp_event_map.find(sID); if(it != client_ptp_event_map.end()) - client_data = it->second[1]; - return client_data; + return it->second[1]; + return nullptr; } /* reduce the corresponding eventCount */ void ClientSubscribeMessage::resetClientPtpEventStruct(sessionId_t sID, Event_count &eventCount) { - std::map >::iterator it; client_ptp_event *client_ptp_data = nullptr; - it = client_ptp_event_map.find(sID); + const auto &it = client_ptp_event_map.find(sID); if(it != client_ptp_event_map.end()) client_ptp_data = it->second[0]; else { diff --git a/clkmgr/common/message.cpp b/clkmgr/common/message.cpp index e917507e..e52ff9e0 100644 --- a/clkmgr/common/message.cpp +++ b/clkmgr/common/message.cpp @@ -31,18 +31,17 @@ Message::Message(msgId_t msgId) string Message::ExtractClassName(string prettyFunction, string function) { - auto fpos = prettyFunction.find(function); - auto spos = fpos; + const auto &fpos = prettyFunction.find(function); if(fpos == string::npos) return prettyFunction; - spos = prettyFunction.rfind(" ", fpos); + auto spos = prettyFunction.rfind(" ", fpos); ++spos; if(spos == string::npos || spos >= fpos) return prettyFunction; - auto ret = prettyFunction.substr(spos, fpos - spos); - while(ret.length() > 0 && ret.back() == ':') + string ret = prettyFunction.substr(spos, fpos - spos); + while(!ret.empty() && ret.back() == ':') ret.pop_back(); - return ret.length() != 0 ? ret : prettyFunction; + return ret.empty() ? prettyFunction : ret; } string Message::toString() @@ -100,10 +99,9 @@ PARSE_RXBUFFER_TYPE(Message::parseBuffer) MAKE_RXBUFFER_TYPE(Message::buildMessage) { msgId_t msgId; - std::map::iterator it; if(!PARSE_RX(FIELD, msgId, LxContext)) return false; - it = parseMsgMap.find(msgId); + const auto &it = parseMsgMap.find(msgId); if(it == parseMsgMap.cend()) { PrintError("Unknown message type " + to_string(msgId)); return false; diff --git a/clkmgr/common/transport.cpp b/clkmgr/common/transport.cpp index 75934231..57ff48a3 100644 --- a/clkmgr/common/transport.cpp +++ b/clkmgr/common/transport.cpp @@ -97,9 +97,8 @@ bool Transport::init() bool Transport::stop() { /* Send stop signal to all of the threads */ - for(std::vector::iterator it = workerList.begin(); - it != workerList.end(); ++it) - it->exitVal->store(true); + for(const auto &it : workerList) + it.exitVal->store(true); /* Do any transport specific stop */ if(!_stopTransport()) return false; @@ -109,15 +108,14 @@ bool Transport::stop() bool Transport::finalize() { bool retVal = false; - for(auto it = workerList.begin(); - it != workerList.end(); ++it) { - if(it->retVal.wait_for(chrono::milliseconds(EXIT_TIMEOUT)) != + for(auto &it : workerList) { + if(it.retVal.wait_for(chrono::milliseconds(EXIT_TIMEOUT)) != future_status::ready) { PrintError("Thread Join Timeout"); goto done; } - it->thread.get()->join(); - retVal &= it->retVal.get(); + it.thread.get()->join(); + retVal = retVal && it.retVal.get(); } if(!_finalizeTransport()) goto done; diff --git a/clkmgr/proxy/client.cpp b/clkmgr/proxy/client.cpp index 51bd5a3e..a8a64742 100644 --- a/clkmgr/proxy/client.cpp +++ b/clkmgr/proxy/client.cpp @@ -40,7 +40,7 @@ sessionId_t Client::GetSessionIdAt(size_t index) ClientX Client::GetClientSession(sessionId_t sessionId) { - auto iter = SessionMap.find(sessionId); + const auto &iter = SessionMap.find(sessionId); if(iter == SessionMap.cend()) { PrintError("Session ID " + to_string(sessionId) + " not found"); return ClientX(nullptr);