From cce3600e3f789f6f2984d61c925170f92b35ee7d Mon Sep 17 00:00:00 2001
From: Gabor Bencze <b.gabor98@gmail.com>
Date: Thu, 25 Jul 2019 18:51:34 +0200
Subject: cmCommand refactor: cmForEachCommand

---
 Source/cmCommands.cxx       |  2 +-
 Source/cmForEachCommand.cxx | 30 ++++++++++++++++++------------
 Source/cmForEachCommand.h   | 28 ++--------------------------
 3 files changed, 21 insertions(+), 39 deletions(-)

diff --git a/Source/cmCommands.cxx b/Source/cmCommands.cxx
index ea9bddb..f4f4c4a 100644
--- a/Source/cmCommands.cxx
+++ b/Source/cmCommands.cxx
@@ -133,7 +133,7 @@ void GetScriptingCommands(cmState* state)
   state->AddBuiltinCommand("find_path", cm::make_unique<cmFindPathCommand>());
   state->AddBuiltinCommand("find_program",
                            cm::make_unique<cmFindProgramCommand>());
-  state->AddBuiltinCommand("foreach", cm::make_unique<cmForEachCommand>());
+  state->AddBuiltinCommand("foreach", cmForEachCommand);
   state->AddBuiltinCommand("function", cm::make_unique<cmFunctionCommand>());
   state->AddBuiltinCommand("get_cmake_property",
                            cm::make_unique<cmGetCMakePropertyCommand>());
diff --git a/Source/cmForEachCommand.cxx b/Source/cmForEachCommand.cxx
index 9c6f1b4..f0633aa 100644
--- a/Source/cmForEachCommand.cxx
+++ b/Source/cmForEachCommand.cxx
@@ -20,6 +20,9 @@
 #include "cmStringAlgorithms.h"
 #include "cmSystemTools.h"
 
+namespace {
+bool HandleInMode(std::vector<std::string> const& args, cmMakefile& makefile);
+
 class cmForEachFunctionBlocker : public cmFunctionBlocker
 {
 public:
@@ -102,20 +105,21 @@ bool cmForEachFunctionBlocker::Replay(
   mf.AddDefinition(this->Args[0], oldDef);
   return true;
 }
+}
 
-bool cmForEachCommand::InitialPass(std::vector<std::string> const& args,
-                                   cmExecutionStatus&)
+bool cmForEachCommand(std::vector<std::string> const& args,
+                      cmExecutionStatus& status)
 {
   if (args.empty()) {
-    this->SetError("called with incorrect number of arguments");
+    status.SetError("called with incorrect number of arguments");
     return false;
   }
   if (args.size() > 1 && args[1] == "IN") {
-    return this->HandleInMode(args);
+    return HandleInMode(args, status.GetMakefile());
   }
 
   // create a function blocker
-  auto fb = cm::make_unique<cmForEachFunctionBlocker>(this->Makefile);
+  auto fb = cm::make_unique<cmForEachFunctionBlocker>(&status.GetMakefile());
   if (args.size() > 1) {
     if (args[1] == "RANGE") {
       int start = 0;
@@ -145,7 +149,7 @@ bool cmForEachCommand::InitialPass(std::vector<std::string> const& args,
         std::ostringstream str;
         str << "called with incorrect range specification: start ";
         str << start << ", stop " << stop << ", step " << step;
-        this->SetError(str.str());
+        status.SetError(str.str());
         return false;
       }
       std::vector<std::string> range;
@@ -169,14 +173,15 @@ bool cmForEachCommand::InitialPass(std::vector<std::string> const& args,
   } else {
     fb->Args = args;
   }
-  this->Makefile->AddFunctionBlocker(std::move(fb));
+  status.GetMakefile().AddFunctionBlocker(std::move(fb));
 
   return true;
 }
 
-bool cmForEachCommand::HandleInMode(std::vector<std::string> const& args)
+namespace {
+bool HandleInMode(std::vector<std::string> const& args, cmMakefile& makefile)
 {
-  auto fb = cm::make_unique<cmForEachFunctionBlocker>(this->Makefile);
+  auto fb = cm::make_unique<cmForEachFunctionBlocker>(&makefile);
   fb->Args.push_back(args[0]);
 
   enum Doing
@@ -194,7 +199,7 @@ bool cmForEachCommand::HandleInMode(std::vector<std::string> const& args)
     } else if (args[i] == "ITEMS") {
       doing = DoingItems;
     } else if (doing == DoingLists) {
-      const char* value = this->Makefile->GetDefinition(args[i]);
+      const char* value = makefile.GetDefinition(args[i]);
       if (value && *value) {
         cmExpandList(value, fb->Args, true);
       }
@@ -202,12 +207,13 @@ bool cmForEachCommand::HandleInMode(std::vector<std::string> const& args)
       std::ostringstream e;
       e << "Unknown argument:\n"
         << "  " << args[i] << "\n";
-      this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
+      makefile.IssueMessage(MessageType::FATAL_ERROR, e.str());
       return true;
     }
   }
 
-  this->Makefile->AddFunctionBlocker(std::move(fb));
+  makefile.AddFunctionBlocker(std::move(fb));
 
   return true;
 }
+}
diff --git a/Source/cmForEachCommand.h b/Source/cmForEachCommand.h
index 135abf0..1feb965 100644
--- a/Source/cmForEachCommand.h
+++ b/Source/cmForEachCommand.h
@@ -8,33 +8,9 @@
 #include <string>
 #include <vector>
 
-#include "cm_memory.hxx"
-
-#include "cmCommand.h"
-
 class cmExecutionStatus;
 
 /// Starts foreach() ... endforeach() block
-class cmForEachCommand : public cmCommand
-{
-public:
-  /**
-   * This is a virtual constructor for the command.
-   */
-  std::unique_ptr<cmCommand> Clone() override
-  {
-    return cm::make_unique<cmForEachCommand>();
-  }
-
-  /**
-   * This is called when the command is first encountered in
-   * the CMakeLists.txt file.
-   */
-  bool InitialPass(std::vector<std::string> const& args,
-                   cmExecutionStatus& status) override;
-
-private:
-  bool HandleInMode(std::vector<std::string> const& args);
-};
-
+bool cmForEachCommand(std::vector<std::string> const& args,
+                      cmExecutionStatus& status);
 #endif
-- 
cgit v0.12