From 197ef69aa1758040ccf8a01dd0a9be6ddb8f8c17 Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 17 Jun 2022 12:48:59 -0400 Subject: cmArgumentParser: Simplify internal method signatures Record `Parse` parameters during construction of the internal instance instead of passing them to every method. --- Source/cmArgumentParser.cxx | 23 +++++++++----------- Source/cmArgumentParser.h | 53 ++++++++++++++++++++++++++++++--------------- Utilities/IWYU/mapping.imp | 2 +- 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/Source/cmArgumentParser.cxx b/Source/cmArgumentParser.cxx index f0c4cfc..355cfdc 100644 --- a/Source/cmArgumentParser.cxx +++ b/Source/cmArgumentParser.cxx @@ -74,19 +74,16 @@ void Instance::Bind(std::vector>& val) this->ExpectValue = false; } -void Instance::Consume(cm::string_view arg, void* result, - std::vector* unparsedArguments, - std::vector* keywordsMissingValue, - std::vector* parsedKeywords) +void Instance::Consume(cm::string_view arg) { auto const it = this->Bindings.Find(arg); if (it != this->Bindings.end()) { - if (parsedKeywords != nullptr) { - parsedKeywords->emplace_back(it->first); + if (this->ParsedKeywords != nullptr) { + this->ParsedKeywords->emplace_back(it->first); } - it->second(*this, result); - if (this->ExpectValue && keywordsMissingValue != nullptr) { - keywordsMissingValue->emplace_back(it->first); + it->second(*this); + if (this->ExpectValue && this->KeywordsMissingValue != nullptr) { + this->KeywordsMissingValue->emplace_back(it->first); } return; } @@ -97,13 +94,13 @@ void Instance::Consume(cm::string_view arg, void* result, this->CurrentList = nullptr; } else if (this->CurrentList != nullptr) { this->CurrentList->emplace_back(arg); - } else if (unparsedArguments != nullptr) { - unparsedArguments->emplace_back(arg); + } else if (this->UnparsedArguments != nullptr) { + this->UnparsedArguments->emplace_back(arg); } if (this->ExpectValue) { - if (keywordsMissingValue != nullptr) { - keywordsMissingValue->pop_back(); + if (this->KeywordsMissingValue != nullptr) { + this->KeywordsMissingValue->pop_back(); } this->ExpectValue = false; } diff --git a/Source/cmArgumentParser.h b/Source/cmArgumentParser.h index 26148d9..a26195d 100644 --- a/Source/cmArgumentParser.h +++ b/Source/cmArgumentParser.h @@ -16,10 +16,13 @@ #include "cmArgumentParserTypes.h" // IWYU pragma: keep +template +class cmArgumentParser; // IWYU pragma: keep + namespace ArgumentParser { class Instance; -using Action = std::function; +using Action = std::function; // using ActionMap = cm::flat_map; class ActionMap : public std::vector> @@ -32,8 +35,16 @@ public: class Instance { public: - Instance(ActionMap const& bindings) + Instance(ActionMap const& bindings, + std::vector* unparsedArguments, + std::vector* keywordsMissingValue, + std::vector* parsedKeywords, + void* result = nullptr) : Bindings(bindings) + , UnparsedArguments(unparsedArguments) + , KeywordsMissingValue(keywordsMissingValue) + , ParsedKeywords(parsedKeywords) + , Result(result) { } @@ -54,16 +65,21 @@ public: this->Bind(*optVal); } - void Consume(cm::string_view arg, void* result, - std::vector* unparsedArguments, - std::vector* keywordsMissingValue, - std::vector* parsedKeywords); + void Consume(cm::string_view arg); private: ActionMap const& Bindings; + std::vector* UnparsedArguments = nullptr; + std::vector* KeywordsMissingValue = nullptr; + std::vector* ParsedKeywords = nullptr; + void* Result = nullptr; + std::string* CurrentString = nullptr; std::vector* CurrentList = nullptr; bool ExpectValue = false; + + template + friend class ::cmArgumentParser; }; } // namespace ArgumentParser @@ -80,8 +96,9 @@ public: bool const inserted = this->Bindings .Emplace(name, - [member](ArgumentParser::Instance& instance, void* result) { - instance.Bind(static_cast(result)->*member); + [member](ArgumentParser::Instance& instance) { + instance.Bind( + static_cast(instance.Result)->*member); }) .second; assert(inserted), (void)inserted; @@ -94,10 +111,11 @@ public: std::vector* keywordsMissingValue = nullptr, std::vector* parsedKeywords = nullptr) const { - ArgumentParser::Instance instance(this->Bindings); + ArgumentParser::Instance instance(this->Bindings, unparsedArguments, + keywordsMissingValue, parsedKeywords, + &result); for (cm::string_view arg : args) { - instance.Consume(arg, &result, unparsedArguments, keywordsMissingValue, - parsedKeywords); + instance.Consume(arg); } } @@ -133,10 +151,10 @@ public: std::vector* keywordsMissingValue = nullptr, std::vector* parsedKeywords = nullptr) const { - ArgumentParser::Instance instance(this->Bindings); + ArgumentParser::Instance instance(this->Bindings, unparsedArguments, + keywordsMissingValue, parsedKeywords); for (cm::string_view arg : args) { - instance.Consume(arg, nullptr, unparsedArguments, keywordsMissingValue, - parsedKeywords); + instance.Consume(arg); } } @@ -145,10 +163,9 @@ protected: bool Bind(cm::string_view name, T& ref) { return this->Bindings - .Emplace(name, - [&ref](ArgumentParser::Instance& instance, void*) { - instance.Bind(ref); - }) + .Emplace( + name, + [&ref](ArgumentParser::Instance& instance) { instance.Bind(ref); }) .second; } diff --git a/Utilities/IWYU/mapping.imp b/Utilities/IWYU/mapping.imp index b80fc22..b0ed911 100644 --- a/Utilities/IWYU/mapping.imp +++ b/Utilities/IWYU/mapping.imp @@ -91,7 +91,7 @@ { symbol: [ "std::__decay_and_strip::__type", private, "\"cmConfigure.h\"", public ] }, { symbol: [ "std::__decay_and_strip::__type", private, "\"cmConfigure.h\"", public ] }, { symbol: [ "std::__decay_and_strip<__gnu_cxx::__normal_iterator > > &>::__type", private, "\"cmConfigure.h\"", public ] }, - { symbol: [ "std::__decay_and_strip > *, std::vector >, std::allocator > > > > &>::__type", private, "\"cmConfigure.h\"", public ] }, + { symbol: [ "std::__decay_and_strip > *, std::vector >, std::allocator > > > > &>::__type", private, "\"cmConfigure.h\"", public ] }, { symbol: [ "std::__success_type > >::type", private, "\"cmConfigure.h\"", public ] }, { symbol: [ "std::__success_type > >::type", private, "\"cmConfigure.h\"", public ] }, { symbol: [ "std::enable_if > >::type", private, "\"cmConfigure.h\"", public ] }, -- cgit v0.12