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()) {
// copy to handlers array and clear table for target
TypeHandlerTable &typeHandlers = index->second;
for (auto index2 = typeHandlers.begin(); index2 != typeHandlers.end(); ++index2) {
handlers.push_back(index2->second);
for (const auto &[key, value] : typeHandlers) {
handlers.push_back(value);
}
typeHandlers.clear();
}
}
// delete handlers
for (auto index = handlers.begin(); index != handlers.end(); ++index) {
delete *index;
for (auto index : handlers) {
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
// destinations and "*" means all destinations.
std::string screens;
for (auto i = destinations.begin(); i != destinations.end(); ++i) {
if (*i == "*") {
for (const auto &i : destinations) {
if (i == "*") {
screens = "*";
break;
} else {
if (screens.empty()) {
screens = ":";
}
screens += *i;
screens += i;
screens += ":";
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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