From 33a987368e577c117bff53c240b9a3167a981056 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Tue, 3 Nov 2020 19:01:53 +0000 Subject: [PATCH] Replace ConfigTag::create with a public constructor. --- include/configreader.h | 14 +++++++++----- src/configparser.cpp | 23 ++++++----------------- src/configreader.cpp | 13 +++---------- src/modules/m_sqloper.cpp | 5 ++--- src/users.cpp | 8 +++----- 5 files changed, 23 insertions(+), 40 deletions(-) diff --git a/include/configreader.h b/include/configreader.h index 4f93c2c7e..bd15dd586 100644 --- a/include/configreader.h +++ b/include/configreader.h @@ -54,6 +54,13 @@ public: const std::string src_name; const int src_line; + /** Creates a new ConfigTag instance with the specified tag name, file, and line. + * @param Tag The name of this config tag (e.g. "foo" for \). + * @param file The file this config tag was read from. + * @param line The line of \p file that this config tag exists in. + */ + ConfigTag(const std::string& Tag, const std::string& file, int line); + /** Get the value of an option, using def if it does not exist */ std::string getString(const std::string& key, const std::string& def, const std::function& validator) const; /** Get the value of an option, using def if it does not exist */ @@ -104,12 +111,9 @@ public: std::string getTagLocation() const; + /** Retrieves the underlying map of config entries. */ inline const Items& GetItems() const { return items; } - - /** Create a new ConfigTag, giving access to the private ConfigItems item list */ - static std::shared_ptr create(const std::string& Tag, const std::string& file, int line, Items*& Items); - private: - ConfigTag(const std::string& Tag, const std::string& file, int line); + inline Items& GetItems() { return items; } }; /** Defines the server's length limits on various length-limited diff --git a/src/configparser.cpp b/src/configparser.cpp index af7d052cd..7bb55bb50 100644 --- a/src/configparser.cpp +++ b/src/configparser.cpp @@ -179,7 +179,7 @@ struct Parser unget(ch); } - bool kv(ConfigTag::Items* items) + bool kv() { std::string key; nextword(key); @@ -266,10 +266,8 @@ struct Parser value.push_back(ch); } - if (items->find(key) != items->end()) + if (!tag->GetItems().insert({key, value}).second) throw CoreException("Duplicate key '" + key + "' found"); - - (*items)[key] = value; return true; } @@ -288,10 +286,8 @@ struct Parser if (name.empty()) throw CoreException("Empty tag name"); - ConfigTag::Items* items; - tag = ConfigTag::create(name, current.name, current.line, items); - - while (kv(items)) + tag = std::make_shared(name, current.name, current.line); + while (kv()) { // Do nothing here (silences a GCC warning). } @@ -308,12 +304,12 @@ struct Parser } else if (stdalgo::string::equalsci(name, "files")) { - for (const auto& [key, value] : *items) + for (const auto& [key, value] : tag->GetItems()) stack.DoReadFile(key, value, flags, false); } else if (stdalgo::string::equalsci(name, "execfiles")) { - for (const auto& [key, value] : *items) + for (const auto& [key, value] : tag->GetItems()) stack.DoReadFile(key, value, flags, true); } else if (stdalgo::string::equalsci(name, "define")) @@ -687,13 +683,6 @@ std::string ConfigTag::getTagLocation() const return src_name + ":" + ConvToStr(src_line); } -std::shared_ptr ConfigTag::create(const std::string& Tag, const std::string& file, int line, Items*& Items) -{ - std::shared_ptr rv(new ConfigTag(Tag, file, line)); - Items = &rv->items; - return rv; -} - ConfigTag::ConfigTag(const std::string& Tag, const std::string& file, int line) : tag(Tag), src_name(file), src_line(line) { diff --git a/src/configreader.cpp b/src/configreader.cpp index dd0fe826a..5a3fc8c1c 100644 --- a/src/configreader.cpp +++ b/src/configreader.cpp @@ -60,14 +60,8 @@ ServerConfig::ServerPaths::ServerPaths(std::shared_ptr tag) { } -static std::shared_ptr CreateEmptyTag() -{ - ConfigTag::Items* items; - return ConfigTag::create("empty", "", 0, items); -} - ServerConfig::ServerConfig() - : EmptyTag(CreateEmptyTag()) + : EmptyTag(std::make_shared("empty", "", 0)) , Limits(EmptyTag) , Paths(EmptyTag) , CaseMapping("ascii") @@ -181,9 +175,8 @@ void ServerConfig::CrossCheckConnectBlocks(ServerConfig* current) if (blk_count == 0) { // No connect blocks found; make a trivial default block - ConfigTag::Items* items; - auto tag = ConfigTag::create("connect", "", 0, items); - (*items)["allow"] = "*"; + auto tag = std::make_shared("connect", "", 0); + tag->GetItems()["allow"] = "*"; config_data.insert(std::make_pair("connect", tag)); blk_count = 1; } diff --git a/src/modules/m_sqloper.cpp b/src/modules/m_sqloper.cpp index 5d6d86aa9..05331e3b1 100644 --- a/src/modules/m_sqloper.cpp +++ b/src/modules/m_sqloper.cpp @@ -70,8 +70,7 @@ class OperQuery : public SQL::Query res.GetCols(cols); // Create the oper tag as if we were the conf file. - ConfigTag::Items* items; - auto tag = ConfigTag::create("oper", MODNAME, 0, items); + auto tag = std::make_shared("oper", MODNAME, 0); /** Iterate through each column in the SQLOpers table. An infinite number of fields can be specified. * Column 'x' with cell value 'y' will be the same as x=y in an OPER block in opers.conf. @@ -79,7 +78,7 @@ class OperQuery : public SQL::Query for (unsigned int i=0; i < cols.size(); ++i) { if (!row[i].IsNull()) - (*items)[cols[i]] = row[i]; + tag->GetItems()[cols[i]] = row[i]; } const std::string name = tag->getString("name"); diff --git a/src/users.cpp b/src/users.cpp index 9be5cdada..e8c0e4c56 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -1239,9 +1239,7 @@ ConnectClass::ConnectClass(std::shared_ptr tag, char t, const std::st // Connect classes can inherit from each other but this is problematic for modules which can't use // ConnectClass::Update so we build a hybrid tag containing all of the values set on this class as // well as the parent class. - ConfigTag::Items* items; - config = ConfigTag::create(tag->tag, tag->src_name, tag->src_line, items); - + config = std::make_shared(tag->tag, tag->src_name, tag->src_line); for (const auto& [key, value] : parent->config->GetItems()) { // The class name and parent name are not inherited @@ -1250,13 +1248,13 @@ ConnectClass::ConnectClass(std::shared_ptr tag, char t, const std::st // Store the item in the config tag. If this item also // exists in the child it will be overwritten. - (*items)[key] = value; + config->GetItems()[key] = value; } for (const auto& [key, value] : tag->GetItems()) { // This will overwrite the parent value if present. - (*items)[key] = value; + config->GetItems()[key] = value; } }