diff options
author | Brad King <brad.king@kitware.com> | 2022-06-20 18:37:21 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2022-07-22 14:32:08 (GMT) |
commit | 119e1f7fbc6a8244bd93ce54aa265c303395cfbc (patch) | |
tree | 6a418ae7ecf13880ea1df6415b4d968c7aae53e0 | |
parent | 84b335c2868d245841f08656b96cad3d9df10422 (diff) | |
download | CMake-119e1f7fbc6a8244bd93ce54aa265c303395cfbc.zip CMake-119e1f7fbc6a8244bd93ce54aa265c303395cfbc.tar.gz CMake-119e1f7fbc6a8244bd93ce54aa265c303395cfbc.tar.bz2 |
cmArgumentParser: Factor out common static/dynamic implementation
-rw-r--r-- | Source/cmArgumentParser.h | 61 |
1 files changed, 32 insertions, 29 deletions
diff --git a/Source/cmArgumentParser.h b/Source/cmArgumentParser.h index 2d96f07..75f7a82 100644 --- a/Source/cmArgumentParser.h +++ b/Source/cmArgumentParser.h @@ -32,6 +32,26 @@ public: const_iterator Find(cm::string_view name) const; }; +class Base +{ +public: + using Instance = ArgumentParser::Instance; + + ArgumentParser::ActionMap Bindings; + + bool MaybeBind(cm::string_view name, Action action) + { + return this->Bindings.Emplace(name, std::move(action)).second; + } + + void Bind(cm::string_view name, Action action) + { + bool const inserted = this->MaybeBind(name, std::move(action)); + assert(inserted); + static_cast<void>(inserted); + } +}; + class Instance { public: @@ -96,7 +116,7 @@ private: } // namespace ArgumentParser template <typename Result> -class cmArgumentParser +class cmArgumentParser : private ArgumentParser::Base { public: // I *think* this function could be made `constexpr` when the code is @@ -104,15 +124,9 @@ public: template <typename T> cmArgumentParser& Bind(cm::static_string_view name, T Result::*member) { - bool const inserted = - this->Bindings - .Emplace(name, - [member](ArgumentParser::Instance& instance) { - instance.Bind( - static_cast<Result*>(instance.Result)->*member); - }) - .second; - assert(inserted), (void)inserted; + this->Base::Bind(name, [member](Instance& instance) { + instance.Bind(static_cast<Result*>(instance.Result)->*member); + }); return *this; } @@ -122,9 +136,8 @@ public: std::vector<cm::string_view>* keywordsMissingValue = nullptr, std::vector<cm::string_view>* parsedKeywords = nullptr) const { - ArgumentParser::Instance instance(this->Bindings, unparsedArguments, - keywordsMissingValue, parsedKeywords, - &result); + Instance instance(this->Bindings, unparsedArguments, keywordsMissingValue, + parsedKeywords, &result); instance.Parse(args); } @@ -138,20 +151,16 @@ public: parsedKeywords); return result; } - -private: - ArgumentParser::ActionMap Bindings; }; template <> -class cmArgumentParser<void> +class cmArgumentParser<void> : private ArgumentParser::Base { public: template <typename T> cmArgumentParser& Bind(cm::static_string_view name, T& ref) { - bool const inserted = this->Bind(cm::string_view(name), ref); - assert(inserted), (void)inserted; + this->Base::Bind(name, [&ref](Instance& instance) { instance.Bind(ref); }); return *this; } @@ -160,8 +169,8 @@ public: std::vector<cm::string_view>* keywordsMissingValue = nullptr, std::vector<cm::string_view>* parsedKeywords = nullptr) const { - ArgumentParser::Instance instance(this->Bindings, unparsedArguments, - keywordsMissingValue, parsedKeywords); + Instance instance(this->Bindings, unparsedArguments, keywordsMissingValue, + parsedKeywords); instance.Parse(args); } @@ -169,13 +178,7 @@ protected: template <typename T> bool Bind(cm::string_view name, T& ref) { - return this->Bindings - .Emplace( - name, - [&ref](ArgumentParser::Instance& instance) { instance.Bind(ref); }) - .second; + return this->MaybeBind(name, + [&ref](Instance& instance) { instance.Bind(ref); }); } - -private: - ArgumentParser::ActionMap Bindings; }; |