chore: use more ranged for loops

This commit is contained in:
sithlord48 2025-05-21 21:55:49 -04:00 committed by Nick Bolton
parent 456afaa13e
commit a33574e1bd
13 changed files with 99 additions and 102 deletions

View file

@ -298,16 +298,16 @@ void EventQueue::removeHandlers(void *target)
if (index != m_handlers.end()) { if (index != m_handlers.end()) {
// copy to handlers array and clear table for target // copy to handlers array and clear table for target
TypeHandlerTable &typeHandlers = index->second; TypeHandlerTable &typeHandlers = index->second;
for (auto index2 = typeHandlers.begin(); index2 != typeHandlers.end(); ++index2) { for (const auto &[key, value] : typeHandlers) {
handlers.push_back(index2->second); handlers.push_back(value);
} }
typeHandlers.clear(); typeHandlers.clear();
} }
} }
// delete handlers // delete handlers
for (auto index = handlers.begin(); index != handlers.end(); ++index) { for (auto index : handlers) {
delete *index; delete index;
} }
} }

View file

@ -105,15 +105,15 @@ std::string IKeyState::KeyInfo::join(const std::set<std::string> &destinations)
// which makes searching easy. the string is empty if there are no // which makes searching easy. the string is empty if there are no
// destinations and "*" means all destinations. // destinations and "*" means all destinations.
std::string screens; std::string screens;
for (auto i = destinations.begin(); i != destinations.end(); ++i) { for (const auto &i : destinations) {
if (*i == "*") { if (i == "*") {
screens = "*"; screens = "*";
break; break;
} else { } else {
if (screens.empty()) { if (screens.empty()) {
screens = ":"; screens = ":";
} }
screens += *i; screens += i;
screens += ":"; screens += ":";
} }
} }

View file

@ -79,8 +79,8 @@ void KeyMap::addKeyEntry(const KeyItem &item)
// see if we already have this item; just return if so // see if we already have this item; just return if so
KeyEntryList &entries = groupTable[item.m_group]; KeyEntryList &entries = groupTable[item.m_group];
for (size_t i = 0, n = entries.size(); i < n; ++i) { for (const auto &entry : entries) {
if (entries[i].size() == 1 && newItem == entries[i][0]) { if (entry.size() == 1 && newItem == entry.at(0)) {
return; return;
} }
} }
@ -157,10 +157,10 @@ bool KeyMap::addKeyCombinationEntry(KeyID id, int32_t group, const KeyID *keys,
for (int32_t gd = 0; gd < n && !found; ++gd) { for (int32_t gd = 0; gd < n && !found; ++gd) {
const auto eg = (group + gd) % getNumGroups(); const auto eg = (group + gd) % getNumGroups();
const KeyEntryList &entries = groupTable[eg]; const KeyEntryList &entries = groupTable[eg];
for (size_t j = 0; j < entries.size(); ++j) { for (const auto &entry : entries) {
if (entries[j].size() == 1) { if (entry.size() == 1) {
found = true; found = true;
items.push_back(entries[j][0]); items.push_back(entry.at(0));
break; break;
} }
} }
@ -211,14 +211,14 @@ void KeyMap::finish()
void KeyMap::foreachKey(ForeachKeyCallback cb, void *userData) void KeyMap::foreachKey(ForeachKeyCallback cb, void *userData)
{ {
for (auto i = m_keyIDMap.begin(); i != m_keyIDMap.end(); ++i) { for (const auto &[keyId, keyGroup] : m_keyIDMap) {
KeyGroupTable &groupTable = i->second; const KeyGroupTable &groupTable = keyGroup;
for (size_t group = 0; group < groupTable.size(); ++group) { for (size_t group = 0; group < groupTable.size(); ++group) {
KeyEntryList &entryList = groupTable[group]; const KeyEntryList &entryList = groupTable.at(group);
for (size_t j = 0; j < entryList.size(); ++j) { for (auto &entry : entryList) {
KeyItemList &itemList = entryList[j]; const KeyItemList &itemList = entry;
for (size_t k = 0; k < itemList.size(); ++k) { for (auto item : itemList) {
(*cb)(i->first, static_cast<int32_t>(group), itemList[k], userData); (*cb)(keyId, static_cast<int32_t>(group), item, userData);
} }
} }
} }
@ -334,10 +334,10 @@ KeyMap::findCompatibleKey(KeyID id, int32_t group, KeyModifierMask required, Key
} }
const KeyEntryList &entries = i->second[group]; const KeyEntryList &entries = i->second[group];
for (size_t j = 0; j < entries.size(); ++j) { for (const auto &entry : entries) {
if ((entries[j].back().m_sensitive & sensitive) == 0 || if ((entry.back().m_sensitive & sensitive) == 0 ||
(entries[j].back().m_required & sensitive) == (required & sensitive)) { (entry.back().m_required & sensitive) == (required & sensitive)) {
return &entries[j]; return &entry;
} }
} }
@ -365,8 +365,8 @@ KeyModifierMask KeyMap::getCommandModifiers() const
void KeyMap::collectButtons(const ModifierToKeys &mods, ButtonToKeyMap &keys) void KeyMap::collectButtons(const ModifierToKeys &mods, ButtonToKeyMap &keys)
{ {
keys.clear(); keys.clear();
for (auto i = mods.begin(); i != mods.end(); ++i) { for (const auto &[modifierMask, keyItem] : mods) {
keys.insert(std::make_pair(i->second.m_button, &i->second)); keys.insert(std::make_pair(keyItem.m_button, &keyItem));
} }
} }
@ -440,18 +440,20 @@ void KeyMap::setModifierKeys()
{ {
m_modifierKeys.clear(); m_modifierKeys.clear();
m_modifierKeys.resize(kKeyModifierNumBits * getNumGroups()); m_modifierKeys.resize(kKeyModifierNumBits * getNumGroups());
for (KeyIDMap::const_iterator i = m_keyIDMap.begin(); i != m_keyIDMap.end(); ++i) { for (const auto &[keyId, keyGroup] : m_keyIDMap) {
const KeyGroupTable &groupTable = i->second; const KeyGroupTable &groupTable = keyGroup;
for (size_t g = 0; g < groupTable.size(); ++g) { int32_t g = -1;
const KeyEntryList &entries = groupTable[g]; for (const auto &group : groupTable) {
for (size_t j = 0; j < entries.size(); ++j) { g++;
const KeyEntryList &entries = group;
for (const auto &entry : entries) {
// skip multi-key sequences // skip multi-key sequences
if (entries[j].size() != 1) { if (entry.size() != 1) {
continue; continue;
} }
// skip keys that don't generate a modifier // skip keys that don't generate a modifier
const KeyItem &item = entries[j].back(); const KeyItem &item = entry.back();
if (item.m_generates == 0) { if (item.m_generates == 0) {
continue; continue;
} }
@ -460,7 +462,7 @@ void KeyMap::setModifierKeys()
for (int32_t b = 0; b < kKeyModifierNumBits; ++b) { for (int32_t b = 0; b < kKeyModifierNumBits; ++b) {
// skip if item doesn't generate bit b // skip if item doesn't generate bit b
if (((1u << b) & item.m_generates) != 0) { if (((1u << b) & item.m_generates) != 0) {
int32_t mIndex = (int32_t)g * kKeyModifierNumBits + b; int32_t mIndex = g * kKeyModifierNumBits + b;
m_modifierKeys[mIndex].push_back(&item); m_modifierKeys[mIndex].push_back(&item);
} }
} }
@ -602,8 +604,8 @@ const KeyMap::KeyItem *KeyMap::mapCharacterKey(
int32_t newGroup = group; int32_t newGroup = group;
// add each key // add each key
for (size_t j = 0; j < itemList->size(); ++j) { for (auto &item : *itemList) {
if (!keysForKeyItem(itemList->at(j), newGroup, newModifiers, newState, desiredMask, 0, isAutoRepeat, keys, lang)) { if (!keysForKeyItem(item, newGroup, newModifiers, newState, desiredMask, 0, isAutoRepeat, keys, lang)) {
LOG((CLOG_DEBUG1 "can't map key")); LOG((CLOG_DEBUG1 "can't map key"));
keys.clear(); keys.clear();
return nullptr; return nullptr;
@ -680,9 +682,9 @@ const KeyMap::KeyItem *KeyMap::keyForModifier(KeyButton button, int32_t group, i
// this is important when a shift button is modified by shift; we // this is important when a shift button is modified by shift; we
// must use the other shift button to do the shifting. // must use the other shift button to do the shifting.
const ModifierKeyItemList &items = m_modifierKeys[group * kKeyModifierNumBits + modifierBit]; const ModifierKeyItemList &items = m_modifierKeys[group * kKeyModifierNumBits + modifierBit];
for (auto i = items.begin(); i != items.end(); ++i) { for (const auto &item : items) {
if ((*i)->m_button != button) { if (item->m_button != button) {
return (*i); return item;
} }
} }
return nullptr; return nullptr;

View file

@ -756,8 +756,8 @@ void KeyState::updateKeyState()
// get the current keyboard state // get the current keyboard state
KeyButtonSet keysDown; KeyButtonSet keysDown;
pollPressedKeys(keysDown); pollPressedKeys(keysDown);
for (auto i = keysDown.begin(); i != keysDown.end(); ++i) { for (const auto &key : keysDown) {
m_keys[*i] = 1; m_keys[key] = 1;
} }
// get the current modifier state // get the current modifier state
@ -872,9 +872,9 @@ bool KeyState::fakeKeyRepeat(KeyID id, KeyModifierMask mask, int32_t count, KeyB
if (localID != oldLocalID) { if (localID != oldLocalID) {
// replace key up with previous KeyButton but leave key down // replace key up with previous KeyButton but leave key down
// alone so it uses the new KeyButton. // alone so it uses the new KeyButton.
for (auto index = keys.begin(); index != keys.end(); ++index) { for (auto &key : keys) {
if (index->m_type == Keystroke::kButton && index->m_data.m_button.m_button == localID) { if (key.m_type == Keystroke::kButton && key.m_data.m_button.m_button == localID) {
index->m_data.m_button.m_button = oldLocalID; key.m_data.m_button.m_button = oldLocalID;
break; break;
} }
} }
@ -1099,11 +1099,11 @@ void KeyState::updateModifierKeyState(
// get the pressed modifier buttons before and after // get the pressed modifier buttons before and after
deskflow::KeyMap::ButtonToKeyMap oldKeys; deskflow::KeyMap::ButtonToKeyMap oldKeys;
deskflow::KeyMap::ButtonToKeyMap newKeys; deskflow::KeyMap::ButtonToKeyMap newKeys;
for (auto i = oldModifiers.begin(); i != oldModifiers.end(); ++i) { for (const auto &[modifier, keyItem] : oldModifiers) {
oldKeys.insert(std::make_pair(i->second.m_button, &i->second)); oldKeys.insert(std::make_pair(keyItem.m_button, &keyItem));
} }
for (auto i = newModifiers.begin(); i != newModifiers.end(); ++i) { for (const auto &[modifier, keyItem] : newModifiers) {
newKeys.insert(std::make_pair(i->second.m_button, &i->second)); newKeys.insert(std::make_pair(keyItem.m_button, &keyItem));
} }
// get the modifier buttons that were pressed or released // get the modifier buttons that were pressed or released

View file

@ -209,13 +209,12 @@ QString generateFingerprintArt(const QByteArray &rawDigest)
int y = rows / 2; int y = rows / 2;
/* process raw key */ /* process raw key */
for (size_t i = 0; i < rawDigest.size(); i++) { for (int byte : rawDigest) {
/* each byte conveys four 2-bit move commands */ /* each byte conveys four 2-bit move commands */
int input = rawDigest[i];
for (uint32_t b = 0; b < 4; b++) { for (uint32_t b = 0; b < 4; b++) {
/* evaluate 2 bit, rest is shifted later */ /* evaluate 2 bit, rest is shifted later */
x += (input & 0x1) ? 1 : -1; x += (byte & 0x1) ? 1 : -1;
y += (input & 0x2) ? 1 : -1; y += (byte & 0x2) ? 1 : -1;
/* assure we are still in bounds */ /* assure we are still in bounds */
x = std::clamp(x, 0, columns - 1); x = std::clamp(x, 0, columns - 1);
@ -224,7 +223,7 @@ QString generateFingerprintArt(const QByteArray &rawDigest)
/* augment the field */ /* augment the field */
if (field[x][y] < len - 2) if (field[x][y] < len - 2)
field[x][y]++; field[x][y]++;
input = input >> 2; byte = byte >> 2;
} }
} }

View file

@ -1063,8 +1063,8 @@ void XWindowsClipboard::clearReplies()
void XWindowsClipboard::clearReplies(ReplyList &replies) void XWindowsClipboard::clearReplies(ReplyList &replies)
{ {
for (auto index = replies.begin(); index != replies.end(); ++index) { for (auto reply : replies) {
delete *index; delete reply;
} }
replies.clear(); replies.clear();
} }

View file

@ -210,8 +210,8 @@ void XWindowsEventQueueBuffer::flush()
// note -- m_mutex must be locked on entry // note -- m_mutex must be locked on entry
// flush the posted event list to the X server // flush the posted event list to the X server
for (size_t i = 0; i < m_postedEvents.size(); ++i) { for (auto &event : m_postedEvents) {
XSendEvent(m_display, m_window, False, 0, &m_postedEvents[i]); XSendEvent(m_display, m_window, False, 0, &event);
} }
XFlush(m_display); XFlush(m_display);
m_postedEvents.clear(); m_postedEvents.clear();

View file

@ -200,8 +200,8 @@ XWindowsScreen::~XWindowsScreen()
m_events->adoptBuffer(nullptr); m_events->adoptBuffer(nullptr);
m_events->removeHandler(EventTypes::System, m_events->getSystemTarget()); m_events->removeHandler(EventTypes::System, m_events->getSystemTarget());
for (ClipboardID id = 0; id < kClipboardEnd; ++id) { for (auto clipboard : m_clipboard) {
delete m_clipboard[id]; delete clipboard;
} }
delete m_keyState; delete m_keyState;
delete m_screensaver; delete m_screensaver;
@ -700,9 +700,9 @@ uint32_t XWindowsScreen::registerHotKey(KeyID key, KeyModifierMask mask)
if (err) { if (err) {
// if any failed then unregister any we did get // if any failed then unregister any we did get
for (auto j = hotKeys.begin(); j != hotKeys.end(); ++j) { for (const auto &[keyCode, keyMask] : hotKeys) {
XUngrabKey(m_display, j->first, j->second, m_root); XUngrabKey(m_display, keyCode, keyMask, m_root);
m_hotKeyToIDMap.erase(HotKeyItem(j->first, j->second)); m_hotKeyToIDMap.erase(HotKeyItem(keyCode, keyMask));
} }
m_oldHotKeyIDs.push_back(id); m_oldHotKeyIDs.push_back(id);
@ -733,10 +733,10 @@ void XWindowsScreen::unregisterHotKey(uint32_t id)
bool err = false; bool err = false;
{ {
XWindowsUtil::ErrorLock lock(m_display, &err); XWindowsUtil::ErrorLock lock(m_display, &err);
HotKeyList &hotKeys = i->second; const HotKeyList &hotKeys = i->second;
for (auto j = hotKeys.begin(); j != hotKeys.end(); ++j) { for (const auto &[keyCode, keyMask] : hotKeys) {
XUngrabKey(m_display, j->first, j->second, m_root); XUngrabKey(m_display, keyCode, keyMask, m_root);
m_hotKeyToIDMap.erase(HotKeyItem(j->first, j->second)); m_hotKeyToIDMap.erase(HotKeyItem(keyCode, keyMask));
} }
} }
if (err) { if (err) {
@ -1644,8 +1644,8 @@ ClipboardID XWindowsScreen::getClipboardID(Atom selection) const
void XWindowsScreen::processClipboardRequest(Window requestor, Time time, Atom property) const void XWindowsScreen::processClipboardRequest(Window requestor, Time time, Atom property) const
{ {
// check every clipboard until one returns success // check every clipboard until one returns success
for (ClipboardID id = 0; id < kClipboardEnd; ++id) { for (const auto &clipboard : m_clipboard) {
if (m_clipboard[id] != nullptr && m_clipboard[id]->processRequest(requestor, time, property)) { if (clipboard != nullptr && clipboard->processRequest(requestor, time, property)) {
break; break;
} }
} }
@ -1654,8 +1654,8 @@ void XWindowsScreen::processClipboardRequest(Window requestor, Time time, Atom p
void XWindowsScreen::destroyClipboardRequest(Window requestor) const void XWindowsScreen::destroyClipboardRequest(Window requestor) const
{ {
// check every clipboard until one returns success // check every clipboard until one returns success
for (ClipboardID id = 0; id < kClipboardEnd; ++id) { for (const auto &clipboard : m_clipboard) {
if (m_clipboard[id] != nullptr && m_clipboard[id]->destroyRequest(requestor)) { if (clipboard != nullptr && clipboard->destroyRequest(requestor)) {
break; break;
} }
} }

View file

@ -1599,13 +1599,13 @@ std::ostream &operator<<(std::ostream &s, const Config &config)
{ {
// screens section // screens section
s << "section: screens" << std::endl; s << "section: screens" << std::endl;
for (auto screen = config.begin(); screen != config.end(); ++screen) { for (const auto &screen : config) {
s << "\t" << screen->c_str() << ":" << std::endl; s << "\t" << screen.c_str() << ":" << std::endl;
const Config::ScreenOptions *options = config.getOptions(*screen); const auto options = config.getOptions(screen);
if (options != nullptr && options->size() > 0) { if (options != nullptr && options->size() > 0) {
for (auto option = options->begin(); option != options->end(); ++option) { for (auto [optionId, optionValue] : *options) {
const char *name = Config::getOptionName(option->first); const char *name = Config::getOptionName(optionId);
std::string value = Config::getOptionValue(option->first, option->second); std::string value = Config::getOptionValue(optionId, optionValue);
if (name != nullptr && !value.empty()) { if (name != nullptr && !value.empty()) {
s << "\t\t" << name << " = " << value << std::endl; s << "\t\t" << name << " = " << value << std::endl;
} }
@ -1617,10 +1617,10 @@ std::ostream &operator<<(std::ostream &s, const Config &config)
// links section // links section
std::string neighbor; std::string neighbor;
s << "section: links" << std::endl; s << "section: links" << std::endl;
for (auto screen = config.begin(); screen != config.end(); ++screen) { for (const auto &screen : config) {
s << "\t" << screen->c_str() << ":" << std::endl; s << "\t" << screen.c_str() << ":" << std::endl;
for (Config::link_const_iterator link = config.beginNeighbor(*screen), nend = config.endNeighbor(*screen); for (Config::link_const_iterator link = config.beginNeighbor(screen), nend = config.endNeighbor(screen);
link != nend; ++link) { link != nend; ++link) {
s << "\t\t" << Config::dirName(link->first.getSide()) << Config::formatInterval(link->first.getInterval()) s << "\t\t" << Config::dirName(link->first.getSide()) << Config::formatInterval(link->first.getInterval())
<< " = " << link->second.getName().c_str() << Config::formatInterval(link->second.getInterval()) << std::endl; << " = " << link->second.getName().c_str() << Config::formatInterval(link->second.getInterval()) << std::endl;
@ -1655,9 +1655,9 @@ std::ostream &operator<<(std::ostream &s, const Config &config)
// options section // options section
s << "section: options" << std::endl; s << "section: options" << std::endl;
if (const Config::ScreenOptions *options = config.getOptions(""); options && options->size() > 0) { if (const Config::ScreenOptions *options = config.getOptions(""); options && options->size() > 0) {
for (auto option = options->begin(); option != options->end(); ++option) { for (auto [optionId, optionValue] : *options) {
const char *name = Config::getOptionName(option->first); const char *name = Config::getOptionName(optionId);
std::string value = Config::getOptionValue(option->first, option->second); std::string value = Config::getOptionValue(optionId, optionValue);
if (name != nullptr && !value.empty()) { if (name != nullptr && !value.empty()) {
s << "\t" << name << " = " << value << std::endl; s << "\t" << name << " = " << value << std::endl;
} }

View file

@ -662,9 +662,9 @@ bool InputFilter::Rule::handleEvent(const Event &event)
} }
// perform actions // perform actions
for (auto i = actions->begin(); i != actions->end(); ++i) { for (auto action : *actions) {
LOG((CLOG_DEBUG1 "hotkey: %s", (*i)->format().c_str())); LOG((CLOG_DEBUG1 "hotkey: %s", action->format().c_str()));
(*i)->perform(event); action->perform(event);
} }
return true; return true;

View file

@ -20,10 +20,7 @@ PrimaryClient::PrimaryClient(const std::string &name, deskflow::Screen *screen)
m_screen(screen), m_screen(screen),
m_fakeInputCount(0) m_fakeInputCount(0)
{ {
// all clipboards are clean // do nothing
for (uint32_t i = 0; i < kClipboardEnd; ++i) {
m_clipboardDirty[i] = false;
}
} }
void PrimaryClient::reconfigure(uint32_t activeSides) void PrimaryClient::reconfigure(uint32_t activeSides)

View file

@ -142,6 +142,6 @@ public:
private: private:
deskflow::Screen *m_screen; deskflow::Screen *m_screen;
bool m_clipboardDirty[kClipboardEnd]; bool m_clipboardDirty[kClipboardEnd] = {false, false};
int32_t m_fakeInputCount; int32_t m_fakeInputCount;
}; };

View file

@ -83,8 +83,7 @@ Server::Server(
std::string primaryName = getName(primaryClient); std::string primaryName = getName(primaryClient);
// clear clipboards // clear clipboards
for (ClipboardID id = 0; id < kClipboardEnd; ++id) { for (auto &clipboard : m_clipboards) {
ClipboardInfo &clipboard = m_clipboards[id];
clipboard.m_clipboardOwner = primaryName; clipboard.m_clipboardOwner = primaryName;
clipboard.m_clipboardSeqNum = m_seqNum; clipboard.m_clipboardSeqNum = m_seqNum;
if (clipboard.m_clipboard.open(0)) { if (clipboard.m_clipboard.open(0)) {
@ -1062,9 +1061,9 @@ void Server::sendOptions(BaseClientProxy *client) const
if (options != nullptr) { if (options != nullptr) {
// convert options to a more convenient form for sending // convert options to a more convenient form for sending
optionsList.reserve(2 * options->size()); optionsList.reserve(2 * options->size());
for (auto index = options->begin(); index != options->end(); ++index) { for (auto [optionId, optionValue] : *options) {
optionsList.push_back(index->first); optionsList.push_back(optionId);
optionsList.push_back(static_cast<uint32_t>(index->second)); optionsList.push_back(static_cast<uint32_t>(optionValue));
} }
} }
@ -1073,9 +1072,9 @@ void Server::sendOptions(BaseClientProxy *client) const
if (options != nullptr) { if (options != nullptr) {
// convert options to a more convenient form for sending // convert options to a more convenient form for sending
optionsList.reserve(optionsList.size() + 2 * options->size()); optionsList.reserve(optionsList.size() + 2 * options->size());
for (auto index = options->begin(); index != options->end(); ++index) { for (auto [optionId, optionValue] : *options) {
optionsList.push_back(index->first); optionsList.push_back(optionId);
optionsList.push_back(static_cast<uint32_t>(index->second)); optionsList.push_back(static_cast<uint32_t>(optionValue));
} }
} }
@ -1096,9 +1095,9 @@ void Server::processOptions()
m_switchNeedsAlt = false; // doesnt' work correct. m_switchNeedsAlt = false; // doesnt' work correct.
bool newRelativeMoves = m_relativeMoves; bool newRelativeMoves = m_relativeMoves;
for (auto index = options->begin(); index != options->end(); ++index) { for (auto [optionId, optionValue] : *options) {
const OptionID id = index->first; const OptionID id = optionId;
const OptionValue value = index->second; const OptionValue value = optionValue;
if (id == kOptionProtocol) { if (id == kOptionProtocol) {
using enum ENetworkProtocol; using enum ENetworkProtocol;
const auto enumValue = static_cast<ENetworkProtocol>(value); const auto enumValue = static_cast<ENetworkProtocol>(value);
@ -2010,8 +2009,8 @@ void Server::closeClients(const ServerConfig &config)
// now close them. we collect the list then close in two steps // now close them. we collect the list then close in two steps
// because closeClient() modifies the collection we iterate over. // because closeClient() modifies the collection we iterate over.
for (auto index = removed.begin(); index != removed.end(); ++index) { for (auto &client : removed) {
closeClient(*index, kMsgCClose); closeClient(client, kMsgCClose);
} }
} }