Browse Source

cmArgumentParser: Simplify internal method signatures

Record `Parse` parameters during construction of the internal instance
instead of passing them to every method.
Brad King 3 years ago
parent
commit
197ef69aa1
3 changed files with 46 additions and 32 deletions
  1. 10 13
      Source/cmArgumentParser.cxx
  2. 35 18
      Source/cmArgumentParser.h
  3. 1 1
      Utilities/IWYU/mapping.imp

+ 10 - 13
Source/cmArgumentParser.cxx

@@ -74,19 +74,16 @@ void Instance::Bind(std::vector<std::vector<std::string>>& val)
   this->ExpectValue = false;
 }
 
-void Instance::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 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;
   }

+ 35 - 18
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;
   }
 

+ 1 - 1
Utilities/IWYU/mapping.imp

@@ -91,7 +91,7 @@
   { symbol: [ "std::__decay_and_strip<cmFindPackageCommand::PathLabel &>::__type", private, "\"cmConfigure.h\"", public ] },
   { symbol: [ "std::__decay_and_strip<cmGlobalNinjaGenerator::TargetAlias &>::__type", private, "\"cmConfigure.h\"", public ] },
   { symbol: [ "std::__decay_and_strip<__gnu_cxx::__normal_iterator<const cmCTestTestHandler::cmCTestTestProperties *, std::vector<cmCTestTestHandler::cmCTestTestProperties, std::allocator<cmCTestTestHandler::cmCTestTestProperties> > > &>::__type", private, "\"cmConfigure.h\"", public ] },
-  { symbol: [ "std::__decay_and_strip<const __gnu_cxx::__normal_iterator<std::pair<cm::string_view, std::function<void (ArgumentParser::Instance &, void *)> > *, std::vector<std::pair<cm::string_view, std::function<void (ArgumentParser::Instance &, void *)> >, std::allocator<std::pair<cm::string_view, std::function<void (ArgumentParser::Instance &, void *)> > > > > &>::__type", private, "\"cmConfigure.h\"", public ] },
+  { symbol: [ "std::__decay_and_strip<const __gnu_cxx::__normal_iterator<std::pair<cm::string_view, std::function<void (ArgumentParser::Instance &)> > *, std::vector<std::pair<cm::string_view, std::function<void (ArgumentParser::Instance &)> >, std::allocator<std::pair<cm::string_view, std::function<void (ArgumentParser::Instance &)> > > > > &>::__type", private, "\"cmConfigure.h\"", public ] },
   { symbol: [ "std::__success_type<std::chrono::duration<double, std::ratio<1, 1> > >::type", private, "\"cmConfigure.h\"", public ] },
   { symbol: [ "std::__success_type<std::chrono::duration<long, std::ratio<1, 1000000000> > >::type", private, "\"cmConfigure.h\"", public ] },
   { symbol: [ "std::enable_if<true, std::chrono::duration<long, std::ratio<1, 1> > >::type", private, "\"cmConfigure.h\"", public ] },