Skip to content

Commit

Permalink
clkmgr: replace iterator type with automatic.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
erezgeva authored and yoongsiang2 committed Feb 3, 2025
1 parent 1e9ca18 commit 474c849
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 38 deletions.
4 changes: 1 addition & 3 deletions clkmgr/client/notification_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ClientState *>::iterator it ;
for(it = ClientStateArray.begin(); it < ClientStateArray.end(); it++) {
for(const auto &currentClientState : 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 \
Expand Down
29 changes: 11 additions & 18 deletions clkmgr/client/subscribe_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sessionId_t, std::array<client_ptp_event *, 2>>::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();
Expand Down Expand Up @@ -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 <sessionId_t, std::array<client_ptp_event *, 2>>::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];
Expand All @@ -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 <sessionId_t, std::array<client_ptp_event *, 2>>::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 <sessionId_t, std::array<client_ptp_event *, 2>>::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 <sessionId_t, std::array<client_ptp_event *, 2>>::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 {
Expand Down
14 changes: 6 additions & 8 deletions clkmgr/common/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -100,10 +99,9 @@ PARSE_RXBUFFER_TYPE(Message::parseBuffer)
MAKE_RXBUFFER_TYPE(Message::buildMessage)
{
msgId_t msgId;
std::map<msgId_t, BuildMessage_t>::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;
Expand Down
14 changes: 6 additions & 8 deletions clkmgr/common/transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ bool Transport::init()
bool Transport::stop()
{
/* Send stop signal to all of the threads */
for(std::vector<TransportWorkerState>::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<NullTransport, MessageQueue>())
return false;
Expand All @@ -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<NullTransport, MessageQueue>())
goto done;
Expand Down
2 changes: 1 addition & 1 deletion clkmgr/proxy/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 474c849

Please sign in to comment.