diff options
author | Brad King <brad.king@kitware.com> | 2022-06-17 16:48:59 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2022-07-20 20:03:12 (GMT) |
commit | 197ef69aa1758040ccf8a01dd0a9be6ddb8f8c17 (patch) | |
tree | 33a19c811cb610e08a4aa6cf2bca26108701df4f /Source/cmArgumentParser.h | |
parent | e98fd9d87ecbc231632653b2f3b31d8a01864874 (diff) | |
download | CMake-197ef69aa1758040ccf8a01dd0a9be6ddb8f8c17.zip CMake-197ef69aa1758040ccf8a01dd0a9be6ddb8f8c17.tar.gz CMake-197ef69aa1758040ccf8a01dd0a9be6ddb8f8c17.tar.bz2 |
cmArgumentParser: Simplify internal method signatures
Record `Parse` parameters during construction of the internal instance
instead of passing them to every method.
Diffstat (limited to 'Source/cmArgumentParser.h')
-rw-r--r-- | Source/cmArgumentParser.h | 53 |
1 files changed, 35 insertions, 18 deletions
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 <typename Result> +class cmArgumentParser; // IWYU pragma: keep + namespace ArgumentParser { class Instance; -using Action = std::function<void(Instance&, void*)>; +using Action = std::function<void(Instance&)>; // using ActionMap = cm::flat_map<cm::string_view, Action>; class ActionMap : public std::vector<std::pair<cm::string_view, Action>> @@ -32,8 +35,16 @@ public: class Instance { public: - Instance(ActionMap const& bindings) + Instance(ActionMap const& bindings, + std::vector<std::string>* unparsedArguments, + std::vector<cm::string_view>* keywordsMissingValue, + std::vector<cm::string_view>* 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<std::string>* unparsedArguments, - std::vector<cm::string_view>* keywordsMissingValue, - std::vector<cm::string_view>* parsedKeywords); + void Consume(cm::string_view arg); private: ActionMap const& Bindings; + std::vector<std::string>* UnparsedArguments = nullptr; + std::vector<cm::string_view>* KeywordsMissingValue = nullptr; + std::vector<cm::string_view>* ParsedKeywords = nullptr; + void* Result = nullptr; + std::string* CurrentString = nullptr; std::vector<std::string>* CurrentList = nullptr; bool ExpectValue = false; + + template <typename Result> + 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*>(result)->*member); + [member](ArgumentParser::Instance& instance) { + instance.Bind( + static_cast<Result*>(instance.Result)->*member); }) .second; assert(inserted), (void)inserted; @@ -94,10 +111,11 @@ public: std::vector<cm::string_view>* keywordsMissingValue = nullptr, std::vector<cm::string_view>* 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<cm::string_view>* keywordsMissingValue = nullptr, std::vector<cm::string_view>* 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; } |