From a3241065c55fd2a69e8793b89a5d0b1a957b3fd0 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Tue, 10 Oct 2023 21:14:50 +0100 Subject: Start migrating to range-based for loops. --- modules/database/db_flatfile.cpp | 59 +++++++++++++++++----------------------- modules/database/db_old.cpp | 26 +++++++++--------- modules/database/db_redis.cpp | 53 +++++++++++------------------------- modules/database/db_sql.cpp | 22 ++++++--------- modules/database/db_sql_live.cpp | 12 ++++---- 5 files changed, 68 insertions(+), 104 deletions(-) (limited to 'modules/database') diff --git a/modules/database/db_flatfile.cpp b/modules/database/db_flatfile.cpp index e4ad7329a..7841a57cf 100644 --- a/modules/database/db_flatfile.cpp +++ b/modules/database/db_flatfile.cpp @@ -77,17 +77,17 @@ class LoadData : public Serialize::Data std::set KeySet() const override { std::set keys; - for (std::map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it) - keys.insert(it->first); + for (const auto &[key, _]: this->data) + keys.insert(key); return keys; } size_t Hash() const override { size_t hash = 0; - for (std::map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it) - if (!it->second.empty()) - hash ^= Anope::hash_cs()(it->second); + for (const auto &[_, value] : this->data) + if (!value.empty()) + hash ^= Anope::hash_cs()(value); return hash; } @@ -117,51 +117,49 @@ class DBFlatFile : public Module, public Pipe { last_day = tm->tm_mday; - const std::vector &type_order = Serialize::Type::GetTypeOrder(); - std::set dbs; dbs.insert(Config->GetModule(this)->Get("database", "anope.db")); - for (unsigned i = 0; i < type_order.size(); ++i) + for (const auto &type_order : Serialize::Type::GetTypeOrder()) { - Serialize::Type *stype = Serialize::Type::Find(type_order[i]); + Serialize::Type *stype = Serialize::Type::Find(type_order); if (stype && stype->GetOwner()) dbs.insert("module_" + stype->GetOwner()->name + ".db"); } - for (std::set::const_iterator it = dbs.begin(), it_end = dbs.end(); it != it_end; ++it) + for (const auto &db : dbs) { - const Anope::string &oldname = Anope::DataDir + "/" + *it; - Anope::string newname = Anope::DataDir + "/backups/" + *it + "-" + stringify(tm->tm_year + 1900) + Anope::printf("-%02i-", tm->tm_mon + 1) + Anope::printf("%02i", tm->tm_mday); + const Anope::string &oldname = Anope::DataDir + "/" + db; + Anope::string newname = Anope::DataDir + "/backups/" + db + "-" + stringify(tm->tm_year + 1900) + Anope::printf("-%02i-", tm->tm_mon + 1) + Anope::printf("%02i", tm->tm_mday); /* Backup already exists or no database to backup */ if (Anope::IsFile(newname) || !Anope::IsFile(oldname)) continue; - Log(LOG_DEBUG) << "db_flatfile: Attempting to rename " << *it << " to " << newname; + Log(LOG_DEBUG) << "db_flatfile: Attempting to rename " << db << " to " << newname; if (rename(oldname.c_str(), newname.c_str())) { Anope::string err = Anope::LastError(); - Log(this) << "Unable to back up database " << *it << " (" << err << ")!"; + Log(this) << "Unable to back up database " << db << " (" << err << ")!"; if (!Config->GetModule(this)->Get("nobackupokay")) { Anope::Quitting = true; - Anope::QuitReason = "Unable to back up database " + *it + " (" + err + ")"; + Anope::QuitReason = "Unable to back up database " + db + " (" + err + ")"; } continue; } - backups[*it].push_back(newname); + backups[db].push_back(newname); unsigned keepbackups = Config->GetModule(this)->Get("keepbackups"); - if (keepbackups > 0 && backups[*it].size() > keepbackups) + if (keepbackups > 0 && backups[db].size() > keepbackups) { - unlink(backups[*it].front().c_str()); - backups[*it].pop_front(); + unlink(backups[db].front().c_str()); + backups[db].pop_front(); } } } @@ -217,7 +215,6 @@ class DBFlatFile : public Module, public Pipe EventReturn OnLoadDatabase() override { - const std::vector &type_order = Serialize::Type::GetTypeOrder(); std::set tried_dbs; const Anope::string &db_name = Anope::DataDir + "/" + Config->GetModule(this)->Get("database", "anope.db"); @@ -238,18 +235,16 @@ class DBFlatFile : public Module, public Pipe LoadData ld; ld.fs = &fd; - for (unsigned i = 0; i < type_order.size(); ++i) + for (const auto &type_order : Serialize::Type::GetTypeOrder()) { - Serialize::Type *stype = Serialize::Type::Find(type_order[i]); + Serialize::Type *stype = Serialize::Type::Find(type_order); if (!stype || stype->GetOwner()) continue; - std::vector &pos = positions[stype->GetName()]; - - for (unsigned j = 0; j < pos.size(); ++j) + for (const auto &position : positions[stype->GetName()]) { fd.clear(); - fd.seekg(pos[j]); + fd.seekg(position); Serializable *obj = stype->Unserialize(NULL, ld); if (obj != NULL) @@ -295,10 +290,8 @@ class DBFlatFile : public Module, public Pipe std::map databases; /* First open the databases of all of the registered types. This way, if we have a type with 0 objects, that database will be properly cleared */ - for (std::map::const_iterator it = Serialize::Type::GetTypes().begin(), it_end = Serialize::Type::GetTypes().end(); it != it_end; ++it) + for (const auto &[_, s_type] : Serialize::Type::GetTypes()) { - Serialize::Type *s_type = it->second; - if (databases[s_type->GetOwner()]) continue; @@ -316,9 +309,8 @@ class DBFlatFile : public Module, public Pipe SaveData data; const std::list &items = Serializable::GetItems(); - for (std::list::const_iterator it = items.begin(), it_end = items.end(); it != it_end; ++it) + for (auto *base : items) { - Serializable *base = *it; Serialize::Type *s_type = base->GetSerializableType(); data.fs = databases[s_type->GetOwner()]; @@ -332,10 +324,9 @@ class DBFlatFile : public Module, public Pipe *data.fs << "\nEND\n"; } - for (std::map::iterator it = databases.begin(), it_end = databases.end(); it != it_end; ++it) + for (auto &[mod, f] : databases) { - std::fstream *f = it->second; - const Anope::string &db_name = Anope::DataDir + "/" + (it->first ? (it->first->name + ".db") : Config->GetModule(this)->Get("database", "anope.db")); + const Anope::string &db_name = Anope::DataDir + "/" + (mod ? (mod->name + ".db") : Config->GetModule(this)->Get("database", "anope.db")); if (!f->is_open() || !f->good()) { diff --git a/modules/database/db_old.cpp b/modules/database/db_old.cpp index 710815068..f8663b395 100644 --- a/modules/database/db_old.cpp +++ b/modules/database/db_old.cpp @@ -146,20 +146,22 @@ enum static void process_mlock(ChannelInfo *ci, uint32_t lock, bool status, uint32_t *limit, Anope::string *key) { ModeLocks *ml = ci->Require("modelocks"); - for (unsigned i = 0; i < (sizeof(mlock_infos) / sizeof(mlock_info)); ++i) - if (lock & mlock_infos[i].m) + for (auto &mlock_info : mlock_infos) + { + if (lock & mlock_info.m) { - ChannelMode *cm = ModeManager::FindChannelModeByChar(mlock_infos[i].c); + ChannelMode *cm = ModeManager::FindChannelModeByChar(mlock_info.c); if (cm && ml) { - if (limit && mlock_infos[i].c == 'l') + if (limit && mlock_info.c == 'l') ml->SetMLock(cm, status, stringify(*limit)); - else if (key && mlock_infos[i].c == 'k') + else if (key && mlock_info.c == 'k') ml->SetMLock(cm, status, *key); else ml->SetMLock(cm, status); } } + } } static const char Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -449,8 +451,8 @@ static void LoadNicks() const Anope::string settings[] = { "killprotect", "kill_quick", "ns_secure", "ns_private", "hide_email", "hide_mask", "hide_quit", "memo_signon", "memo_receive", "autoop", "msg", "ns_keepmodes" }; - for (unsigned j = 0; j < sizeof(settings) / sizeof(Anope::string); ++j) - nc->Shrink(settings[j].upper()); + for (const auto &setting : settings) + nc->Shrink(setting.upper()); char pwbuf[32]; READ(read_buffer(pwbuf, f)); @@ -752,8 +754,8 @@ static void LoadChannels() const Anope::string settings[] = { "keeptopic", "peace", "cs_private", "restricted", "cs_secure", "secureops", "securefounder", "signkick", "signkick_level", "topiclock", "persist", "noautoop", "cs_keepmodes" }; - for (unsigned j = 0; j < sizeof(settings) / sizeof(Anope::string); ++j) - ci->Shrink(settings[j].upper()); + for (const auto &setting : settings) + ci->Shrink(setting.upper()); READ(read_string(buffer, f)); ci->SetFounder(NickCore::Find(buffer)); @@ -1104,9 +1106,8 @@ static void LoadOper() XLineManager *akill, *sqline, *snline, *szline; akill = sqline = snline = szline = NULL; - for (std::list::iterator it = XLineManager::XLineManagers.begin(), it_end = XLineManager::XLineManagers.end(); it != it_end; ++it) + for (auto *xl : XLineManager::XLineManagers) { - XLineManager *xl = *it; if (xl->Type() == 'G') akill = xl; else if (xl->Type() == 'Q') @@ -1328,9 +1329,8 @@ class DBOld : public Module void OnUplinkSync(Server *s) override { - for (registered_channel_map::iterator it = RegisteredChannelList->begin(), it_end = RegisteredChannelList->end(); it != it_end; ++it) + for (auto &[_, ci] : *RegisteredChannelList) { - ChannelInfo *ci = it->second; uint32_t *limit = mlock_limit.Get(ci); Anope::string *key = mlock_key.Get(ci); diff --git a/modules/database/db_redis.cpp b/modules/database/db_redis.cpp index 974e9ea3f..4c90d179d 100644 --- a/modules/database/db_redis.cpp +++ b/modules/database/db_redis.cpp @@ -21,8 +21,8 @@ class Data : public Serialize::Data ~Data() override { - for (std::map::iterator it = data.begin(), it_end = data.end(); it != it_end; ++it) - delete it->second; + for (auto &[_, stream] : data) + delete stream; } std::iostream& operator[](const Anope::string &key) override @@ -36,17 +36,17 @@ class Data : public Serialize::Data std::set KeySet() const override { std::set keys; - for (std::map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it) - keys.insert(it->first); + for (const auto &[key, _] : this->data) + keys.insert(key); return keys; } size_t Hash() const override { size_t hash = 0; - for (std::map::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it) - if (!it->second->str().empty()) - hash ^= Anope::hash_cs()(it->second->str()); + for (const auto &[_, value] : this->data) + if (!value->str().empty()) + hash ^= Anope::hash_cs()(value->str()); return hash; } }; @@ -161,11 +161,9 @@ class DatabaseRedis : public Module, public Pipe void OnNotify() override { - for (std::set::iterator it = this->updated_items.begin(), it_end = this->updated_items.end(); it != it_end; ++it) + for (auto *obj : this->updated_items) { - Serializable *s = *it; - - this->InsertObject(s); + this->InsertObject(obj); } this->updated_items.clear(); @@ -185,10 +183,9 @@ class DatabaseRedis : public Module, public Pipe return EVENT_CONTINUE; } - const std::vector type_order = Serialize::Type::GetTypeOrder(); - for (unsigned i = 0; i < type_order.size(); ++i) + for (const auto &type_order : Serialize::Type::GetTypeOrder()) { - Serialize::Type *sb = Serialize::Type::Find(type_order[i]); + Serialize::Type *sb = Serialize::Type::Find(type_order); this->OnSerializeTypeCreate(sb); } @@ -266,10 +263,8 @@ void TypeLoader::OnResult(const Reply &r) return; } - for (unsigned i = 0; i < r.multi_bulk.size(); ++i) + for (auto *reply : r.multi_bulk) { - const Reply *reply = r.multi_bulk[i]; - if (reply->type != Reply::BULK) continue; @@ -440,12 +435,8 @@ void Updater::OnResult(const Reply &r) args.emplace_back("HMSET"); args.push_back("hash:" + this->type + ":" + stringify(obj->id)); - typedef std::map items; - for (items::iterator it = data.data.begin(), it_end = data.data.end(); it != it_end; ++it) + for (const auto &[key, value] : data.data) { - const Anope::string &key = it->first; - std::stringstream *value = it->second; - args.push_back(key); args.emplace_back(value->str()); @@ -549,12 +540,8 @@ void SubscriptionListener::OnResult(const Reply &r) /* Transaction start */ me->redis->StartTransaction(); - typedef std::map items; - for (items::iterator it = data.data.begin(), it_end = data.data.end(); it != it_end; ++it) + for (const auto &[k, value] : data.data) { - const Anope::string &k = it->first; - std::stringstream *value = it->second; - std::vector args; args.emplace_back("SREM"); args.push_back("value:" + type + ":" + k + ":" + value->str()); @@ -602,12 +589,8 @@ void ModifiedObject::OnResult(const Reply &r) obj->Serialize(data); - typedef std::map items; - for (items::iterator it = data.data.begin(), it_end = data.data.end(); it != it_end; ++it) + for (auto &[key, value] : data.data) { - const Anope::string &key = it->first; - std::stringstream *value = it->second; - std::vector args; args.emplace_back("SREM"); args.push_back("value:" + st->GetName() + ":" + key + ":" + value->str()); @@ -635,12 +618,8 @@ void ModifiedObject::OnResult(const Reply &r) obj->UpdateCache(data); /* Insert new object values */ - typedef std::map items; - for (items::iterator it = data.data.begin(), it_end = data.data.end(); it != it_end; ++it) + for (const auto &[key, value] : data.data) { - const Anope::string &key = it->first; - std::stringstream *value = it->second; - std::vector args; args.emplace_back("SADD"); args.push_back("value:" + st->GetName() + ":" + key + ":" + value->str()); diff --git a/modules/database/db_sql.cpp b/modules/database/db_sql.cpp index 6494914c4..0f89de06d 100644 --- a/modules/database/db_sql.cpp +++ b/modules/database/db_sql.cpp @@ -100,10 +100,8 @@ class DBSQL : public Module, public Pipe void OnNotify() override { - for (std::set::iterator it = this->updated_items.begin(), it_end = this->updated_items.end(); it != it_end; ++it) + for (auto *obj : this->updated_items) { - Serializable *obj = *it; - if (this->sql) { Data data; @@ -127,15 +125,15 @@ class DBSQL : public Module, public Pipe if (this->imported) { - for (unsigned i = 0; i < create.size(); ++i) - this->RunBackground(create[i]); + for (const auto &query : create) + this->RunBackground(query); this->RunBackground(insert, new ResultSQLSQLInterface(this, obj)); } else { - for (unsigned i = 0; i < create.size(); ++i) - this->sql->RunQuery(create[i]); + for (const auto &query : create) + this->sql->RunQuery(query); /* We are importing objects from another database module, so don't do asynchronous * queries in case the core has to shut down, it will cut short the import @@ -180,10 +178,9 @@ class DBSQL : public Module, public Pipe this->loading_databases = true; - const std::vector type_order = Serialize::Type::GetTypeOrder(); - for (unsigned i = 0; i < type_order.size(); ++i) + for (const auto &type_order : Serialize::Type::GetTypeOrder()) { - Serialize::Type *sb = Serialize::Type::Find(type_order[i]); + Serialize::Type *sb = Serialize::Type::Find(type_order); this->OnSerializeTypeCreate(sb); } @@ -235,9 +232,8 @@ class DBSQL : public Module, public Pipe { Data data; - const std::map &row = res.Row(j); - for (std::map::const_iterator rit = row.begin(), rit_end = row.end(); rit != rit_end; ++rit) - data[rit->first] << rit->second; + for (const auto &[key, value] : res.Row(j)) + data[key] << value; Serializable *obj = sb->Unserialize(NULL, data); try diff --git a/modules/database/db_sql_live.cpp b/modules/database/db_sql_live.cpp index 73bcb6ba8..825628127 100644 --- a/modules/database/db_sql_live.cpp +++ b/modules/database/db_sql_live.cpp @@ -88,10 +88,8 @@ class DBMySQL : public Module, public Pipe if (!this->CheckInit()) return; - for (std::set::iterator it = this->updated_items.begin(), it_end = this->updated_items.end(); it != it_end; ++it) + for (auto *obj : this->updated_items) { - Serializable *obj = *it; - if (obj && this->SQL) { Data data; @@ -107,8 +105,8 @@ class DBMySQL : public Module, public Pipe continue; std::vector create = this->SQL->CreateTable(this->prefix + s_type->GetName(), data); - for (unsigned i = 0; i < create.size(); ++i) - this->RunQueryResult(create[i]); + for (const auto &query : create) + this->RunQueryResult(query); Result res = this->RunQueryResult(this->SQL->BuildInsert(this->prefix + s_type->GetName(), obj->id, data)); if (res.GetID() && obj->id != res.GetID()) @@ -207,8 +205,8 @@ class DBMySQL : public Module, public Pipe { Data data; - for (std::map::const_iterator it = row.begin(), it_end = row.end(); it != it_end; ++it) - data[it->first] << it->second; + for (const auto &[key, value] : row) + data[key] << value; Serializable *s = NULL; std::map::iterator it = obj->objects.find(id); -- cgit