Browse Source

Merge topic 'functional-callbacks'

8c92db829b MessageCallback: Remove unused bool& argument
bcee24aecc Use `std::function` for callbacks

Acked-by: Kitware Robot <[email protected]>
Acked-by: vvs31415 <[email protected]>
Acked-by: Daniel Pfeifer <[email protected]>
Merge-request: !2872
Brad King 6 years ago
parent
commit
c30f9b1cde

+ 3 - 7
Source/CPack/cmCPackGenerator.cxx

@@ -43,12 +43,6 @@ cmCPackGenerator::~cmCPackGenerator()
   this->MakefileMap = nullptr;
 }
 
-void cmCPackGeneratorProgress(const char* msg, float prog, void* ptr)
-{
-  cmCPackGenerator* self = static_cast<cmCPackGenerator*>(ptr);
-  self->DisplayVerboseOutput(msg, prog);
-}
-
 void cmCPackGenerator::DisplayVerboseOutput(const char* msg, float progress)
 {
   (void)progress;
@@ -695,7 +689,9 @@ int cmCPackGenerator::InstallCMakeProject(
   cm.SetHomeOutputDirectory("");
   cm.GetCurrentSnapshot().SetDefaultDefinitions();
   cm.AddCMakePaths();
-  cm.SetProgressCallback(cmCPackGeneratorProgress, this);
+  cm.SetProgressCallback([this](const char* msg, float prog) {
+    this->DisplayVerboseOutput(msg, prog);
+  });
   cm.SetTrace(this->Trace);
   cm.SetTraceExpand(this->TraceExpand);
   cmGlobalGenerator gg(&cm);

+ 2 - 6
Source/CPack/cpack.cxx

@@ -90,12 +90,8 @@ int cpackDefinitionArgument(const char* argument, const char* cValue,
   return 1;
 }
 
-static void cpackProgressCallback(const char* message, float progress,
-                                  void* clientdata)
+static void cpackProgressCallback(const char* message, float /*unused*/)
 {
-  (void)progress;
-  (void)clientdata;
-
   std::cout << "-- " << message << std::endl;
 }
 
@@ -212,7 +208,7 @@ int main(int argc, char const* const* argv)
   cmake cminst(cmake::RoleScript, cmState::CPack);
   cminst.SetHomeDirectory("");
   cminst.SetHomeOutputDirectory("");
-  cminst.SetProgressCallback(cpackProgressCallback, nullptr);
+  cminst.SetProgressCallback(cpackProgressCallback);
   cminst.GetCurrentSnapshot().SetDefaultDefinitions();
   cmGlobalGenerator cmgg(&cminst);
   cmMakefile globalMF(&cmgg, cminst.GetCurrentSnapshot());

+ 21 - 29
Source/CTest/cmCTestBuildAndTestHandler.cxx

@@ -109,27 +109,6 @@ int cmCTestBuildAndTestHandler::RunCMake(std::string* outstring,
   return 0;
 }
 
-void CMakeMessageCallback(const char* m, const char* /*unused*/,
-                          bool& /*unused*/, void* s)
-{
-  std::string* out = static_cast<std::string*>(s);
-  *out += m;
-  *out += "\n";
-}
-
-void CMakeProgressCallback(const char* msg, float /*unused*/, void* s)
-{
-  std::string* out = static_cast<std::string*>(s);
-  *out += msg;
-  *out += "\n";
-}
-
-void CMakeOutputCallback(const char* m, size_t len, void* s)
-{
-  std::string* out = static_cast<std::string*>(s);
-  out->append(m, len);
-}
-
 class cmCTestBuildAndTestCaptureRAII
 {
   cmake& CM;
@@ -138,17 +117,30 @@ public:
   cmCTestBuildAndTestCaptureRAII(cmake& cm, std::string& s)
     : CM(cm)
   {
-    cmSystemTools::SetMessageCallback(CMakeMessageCallback, &s);
-    cmSystemTools::SetStdoutCallback(CMakeOutputCallback, &s);
-    cmSystemTools::SetStderrCallback(CMakeOutputCallback, &s);
-    this->CM.SetProgressCallback(CMakeProgressCallback, &s);
+    cmSystemTools::SetMessageCallback(
+      [&s](const char* msg, const char* /*unused*/) {
+        s += msg;
+        s += "\n";
+      });
+
+    cmSystemTools::SetStdoutCallback(
+      [&s](const char* m, size_t len) { s.append(m, len); });
+
+    cmSystemTools::SetStderrCallback(
+      [&s](const char* m, size_t len) { s.append(m, len); });
+
+    this->CM.SetProgressCallback([&s](const char* msg, float /*unused*/) {
+      s += msg;
+      s += "\n";
+    });
   }
+
   ~cmCTestBuildAndTestCaptureRAII()
   {
-    this->CM.SetProgressCallback(nullptr, nullptr);
-    cmSystemTools::SetStderrCallback(nullptr, nullptr);
-    cmSystemTools::SetStdoutCallback(nullptr, nullptr);
-    cmSystemTools::SetMessageCallback(nullptr, nullptr);
+    this->CM.SetProgressCallback(nullptr);
+    cmSystemTools::SetStderrCallback(nullptr);
+    cmSystemTools::SetStdoutCallback(nullptr);
+    cmSystemTools::SetMessageCallback(nullptr);
   }
 };
 

+ 5 - 10
Source/CTest/cmCTestScriptHandler.cxx

@@ -263,15 +263,6 @@ int cmCTestScriptHandler::ExecuteScript(const std::string& total_script_arg)
   return retVal;
 }
 
-static void ctestScriptProgressCallback(const char* m, float /*unused*/,
-                                        void* cd)
-{
-  cmCTest* ctest = static_cast<cmCTest*>(cd);
-  if (m && *m) {
-    cmCTestLog(ctest, HANDLER_OUTPUT, "-- " << m << std::endl);
-  }
-}
-
 void cmCTestScriptHandler::CreateCMake()
 {
   // create a cmake instance to read the configuration script
@@ -297,7 +288,11 @@ void cmCTestScriptHandler::CreateCMake()
       this->ParentMakefile->GetRecursionDepth());
   }
 
-  this->CMake->SetProgressCallback(ctestScriptProgressCallback, this->CTest);
+  this->CMake->SetProgressCallback([this](const char* m, float /*unused*/) {
+    if (m && *m) {
+      cmCTestLog(this->CTest, HANDLER_OUTPUT, "-- " << m << std::endl);
+    }
+  });
 
   this->AddCTestCommand("ctest_build", new cmCTestBuildCommand);
   this->AddCTestCommand("ctest_configure", new cmCTestConfigureCommand);

+ 4 - 8
Source/CursesDialog/ccmake.cxx

@@ -65,13 +65,6 @@ void onsig(int /*unused*/)
 }
 }
 
-void CMakeMessageHandler(const char* message, const char* title,
-                         bool& /*unused*/, void* clientData)
-{
-  cmCursesForm* self = static_cast<cmCursesForm*>(clientData);
-  self->AddError(message, title);
-}
-
 int main(int argc, char const* const* argv)
 {
   cmsys::Encoding::CommandLineArguments encoding_args =
@@ -156,7 +149,10 @@ int main(int argc, char const* const* argv)
     return 1;
   }
 
-  cmSystemTools::SetMessageCallback(CMakeMessageHandler, myform);
+  cmSystemTools::SetMessageCallback(
+    [myform](const char* message, const char* title) {
+      myform->AddError(message, title);
+    });
 
   cmCursesForm::CurrentForm = myform;
 

+ 9 - 13
Source/CursesDialog/cmCursesMainForm.cxx

@@ -506,12 +506,8 @@ void cmCursesMainForm::UpdateStatusBar(const char* message)
   pos_form_cursor(this->Form);
 }
 
-void cmCursesMainForm::UpdateProgress(const char* msg, float prog, void* vp)
+void cmCursesMainForm::UpdateProgress(const char* msg, float prog)
 {
-  cmCursesMainForm* cm = static_cast<cmCursesMainForm*>(vp);
-  if (!cm) {
-    return;
-  }
   char tmp[1024];
   const char* cmsg = tmp;
   if (prog >= 0) {
@@ -519,8 +515,8 @@ void cmCursesMainForm::UpdateProgress(const char* msg, float prog, void* vp)
   } else {
     cmsg = msg;
   }
-  cm->UpdateStatusBar(cmsg);
-  cm->PrintKeys(1);
+  this->UpdateStatusBar(cmsg);
+  this->PrintKeys(1);
   curses_move(1, 1);
   touchwin(stdscr);
   refresh();
@@ -536,8 +532,8 @@ int cmCursesMainForm::Configure(int noconfigure)
   this->PrintKeys(1);
   touchwin(stdscr);
   refresh();
-  this->CMakeInstance->SetProgressCallback(cmCursesMainForm::UpdateProgress,
-                                           this);
+  this->CMakeInstance->SetProgressCallback(
+    [this](const char* msg, float prog) { this->UpdateProgress(msg, prog); });
 
   // always save the current gui values to disk
   this->FillCacheManagerFromUI();
@@ -560,7 +556,7 @@ int cmCursesMainForm::Configure(int noconfigure)
   } else {
     retVal = this->CMakeInstance->Configure();
   }
-  this->CMakeInstance->SetProgressCallback(nullptr, nullptr);
+  this->CMakeInstance->SetProgressCallback(nullptr);
 
   keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
 
@@ -606,8 +602,8 @@ int cmCursesMainForm::Generate()
   this->PrintKeys(1);
   touchwin(stdscr);
   refresh();
-  this->CMakeInstance->SetProgressCallback(cmCursesMainForm::UpdateProgress,
-                                           this);
+  this->CMakeInstance->SetProgressCallback(
+    [this](const char* msg, float prog) { this->UpdateProgress(msg, prog); });
 
   // Get rid of previous errors
   this->Errors = std::vector<std::string>();
@@ -615,7 +611,7 @@ int cmCursesMainForm::Generate()
   // run the generate process
   int retVal = this->CMakeInstance->Generate();
 
-  this->CMakeInstance->SetProgressCallback(nullptr, nullptr);
+  this->CMakeInstance->SetProgressCallback(nullptr);
   keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
 
   if (retVal != 0 || !this->Errors.empty()) {

+ 1 - 2
Source/CursesDialog/cmCursesMainForm.h

@@ -101,8 +101,7 @@ public:
   /**
    * Progress callback
    */
-  static void UpdateProgressOld(const char* msg, float prog, void*);
-  static void UpdateProgress(const char* msg, float prog, void*);
+  void UpdateProgress(const char* msg, float prog);
 
 protected:
   // Copy the cache values from the user interface to the actual

+ 27 - 23
Source/QtDialog/QCMake.cxx

@@ -23,16 +23,26 @@ QCMake::QCMake(QObject* p)
 
   cmSystemTools::DisableRunCommandOutput();
   cmSystemTools::SetRunCommandHideConsole(true);
-  cmSystemTools::SetMessageCallback(QCMake::messageCallback, this);
-  cmSystemTools::SetStdoutCallback(QCMake::stdoutCallback, this);
-  cmSystemTools::SetStderrCallback(QCMake::stderrCallback, this);
+
+  cmSystemTools::SetMessageCallback(
+    [this](const char* msg, const char* title) {
+      this->messageCallback(msg, title);
+    });
+  cmSystemTools::SetStdoutCallback(
+    [this](const char* msg, size_t len) { this->stdoutCallback(msg, len); });
+  cmSystemTools::SetStderrCallback(
+    [this](const char* msg, size_t len) { this->stderrCallback(msg, len); });
 
   this->CMakeInstance = new cmake(cmake::RoleProject, cmState::Project);
   this->CMakeInstance->SetCMakeEditCommand(
     cmSystemTools::GetCMakeGUICommand());
-  this->CMakeInstance->SetProgressCallback(QCMake::progressCallback, this);
+  this->CMakeInstance->SetProgressCallback(
+    [this](const char* msg, float percent) {
+      this->progressCallback(msg, percent);
+    });
 
-  cmSystemTools::SetInterruptCallback(QCMake::interruptCallback, this);
+  cmSystemTools::SetInterruptCallback(
+    [this] { return this->interruptCallback(); });
 
   std::vector<cmake::GeneratorInfo> generators;
   this->CMakeInstance->GetRegisteredGenerators(
@@ -330,46 +340,40 @@ void QCMake::interrupt()
   this->InterruptFlag.ref();
 }
 
-bool QCMake::interruptCallback(void* cd)
+bool QCMake::interruptCallback()
 {
-  QCMake* self = reinterpret_cast<QCMake*>(cd);
 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
-  return self->InterruptFlag;
+  return this->InterruptFlag;
 #else
-  return self->InterruptFlag.load();
+  return this->InterruptFlag.load();
 #endif
 }
 
-void QCMake::progressCallback(const char* msg, float percent, void* cd)
+void QCMake::progressCallback(const char* msg, float percent)
 {
-  QCMake* self = reinterpret_cast<QCMake*>(cd);
   if (percent >= 0) {
-    emit self->progressChanged(QString::fromLocal8Bit(msg), percent);
+    emit this->progressChanged(QString::fromLocal8Bit(msg), percent);
   } else {
-    emit self->outputMessage(QString::fromLocal8Bit(msg));
+    emit this->outputMessage(QString::fromLocal8Bit(msg));
   }
   QCoreApplication::processEvents();
 }
 
-void QCMake::messageCallback(const char* msg, const char* /*title*/,
-                             bool& /*stop*/, void* cd)
+void QCMake::messageCallback(const char* msg, const char* /*title*/)
 {
-  QCMake* self = reinterpret_cast<QCMake*>(cd);
-  emit self->errorMessage(QString::fromLocal8Bit(msg));
+  emit this->errorMessage(QString::fromLocal8Bit(msg));
   QCoreApplication::processEvents();
 }
 
-void QCMake::stdoutCallback(const char* msg, size_t len, void* cd)
+void QCMake::stdoutCallback(const char* msg, size_t len)
 {
-  QCMake* self = reinterpret_cast<QCMake*>(cd);
-  emit self->outputMessage(QString::fromLocal8Bit(msg, int(len)));
+  emit this->outputMessage(QString::fromLocal8Bit(msg, int(len)));
   QCoreApplication::processEvents();
 }
 
-void QCMake::stderrCallback(const char* msg, size_t len, void* cd)
+void QCMake::stderrCallback(const char* msg, size_t len)
 {
-  QCMake* self = reinterpret_cast<QCMake*>(cd);
-  emit self->outputMessage(QString::fromLocal8Bit(msg, int(len)));
+  emit this->outputMessage(QString::fromLocal8Bit(msg, int(len)));
   QCoreApplication::processEvents();
 }
 

+ 6 - 6
Source/QtDialog/QCMake.h

@@ -167,12 +167,12 @@ signals:
 protected:
   cmake* CMakeInstance;
 
-  static bool interruptCallback(void*);
-  static void progressCallback(const char* msg, float percent, void* cd);
-  static void messageCallback(const char* msg, const char* title, bool&,
-                              void* cd);
-  static void stdoutCallback(const char* msg, size_t len, void* cd);
-  static void stderrCallback(const char* msg, size_t len, void* cd);
+  bool interruptCallback();
+  void progressCallback(const char* msg, float percent);
+  void messageCallback(const char* msg, const char* title);
+  void stdoutCallback(const char* msg, size_t len);
+  void stderrCallback(const char* msg, size_t len);
+
   bool WarnUninitializedMode;
   bool WarnUnusedMode;
   bool WarnUnusedAllMode;

+ 14 - 12
Source/cmServer.cxx

@@ -96,11 +96,16 @@ void cmServer::ProcessRequest(cmConnection* connection,
     return;
   }
 
-  cmSystemTools::SetMessageCallback(reportMessage,
-                                    const_cast<cmServerRequest*>(&request));
+  cmSystemTools::SetMessageCallback(
+    [&request](const char* msg, const char* title) {
+      reportMessage(msg, title, request);
+    });
+
   if (this->Protocol) {
     this->Protocol->CMakeInstance()->SetProgressCallback(
-      reportProgress, const_cast<cmServerRequest*>(&request));
+      [&request](const char* msg, float prog) {
+        reportProgress(msg, prog, request);
+      });
     this->WriteResponse(connection, this->Protocol->Process(request),
                         debug.get());
   } else {
@@ -150,28 +155,25 @@ void cmServer::PrintHello(cmConnection* connection) const
   this->WriteJsonObject(connection, hello, nullptr);
 }
 
-void cmServer::reportProgress(const char* msg, float progress, void* data)
+void cmServer::reportProgress(const char* msg, float progress,
+                              const cmServerRequest& request)
 {
-  const cmServerRequest* request = static_cast<const cmServerRequest*>(data);
-  assert(request);
   if (progress < 0.0f || progress > 1.0f) {
-    request->ReportMessage(msg, "");
+    request.ReportMessage(msg, "");
   } else {
-    request->ReportProgress(0, static_cast<int>(progress * 1000), 1000, msg);
+    request.ReportProgress(0, static_cast<int>(progress * 1000), 1000, msg);
   }
 }
 
 void cmServer::reportMessage(const char* msg, const char* title,
-                             bool& /* cancel */, void* data)
+                             const cmServerRequest& request)
 {
-  const cmServerRequest* request = static_cast<const cmServerRequest*>(data);
-  assert(request);
   assert(msg);
   std::string titleString;
   if (title) {
     titleString = title;
   }
-  request->ReportMessage(std::string(msg), titleString);
+  request.ReportMessage(std::string(msg), titleString);
 }
 
 cmServerResponse cmServer::SetProtocolVersion(const cmServerRequest& request)

+ 4 - 3
Source/cmServer.h

@@ -118,9 +118,10 @@ public:
   void OnConnected(cmConnection* connection) override;
 
 private:
-  static void reportProgress(const char* msg, float progress, void* data);
-  static void reportMessage(const char* msg, const char* title, bool& cancel,
-                            void* data);
+  static void reportProgress(const char* msg, float progress,
+                             const cmServerRequest& request);
+  static void reportMessage(const char* msg, const char* title,
+                            const cmServerRequest& request);
 
   // Handle requests:
   cmServerResponse SetProtocolVersion(const cmServerRequest& request);

+ 21 - 26
Source/cmSystemTools.cxx

@@ -76,6 +76,15 @@
 #  include <malloc.h> /* for malloc/free on QNX */
 #endif
 
+namespace {
+
+cmSystemTools::InterruptCallback s_InterruptCallback;
+cmSystemTools::MessageCallback s_MessageCallback;
+cmSystemTools::OutputCallback s_StderrCallback;
+cmSystemTools::OutputCallback s_StdoutCallback;
+
+} // namespace
+
 static bool cm_isspace(char c)
 {
   return ((c & 0x80) == 0) && isspace(c);
@@ -161,15 +170,6 @@ bool cmSystemTools::s_FatalErrorOccured = false;
 bool cmSystemTools::s_DisableMessages = false;
 bool cmSystemTools::s_ForceUnixPaths = false;
 
-cmSystemTools::MessageCallback cmSystemTools::s_MessageCallback;
-cmSystemTools::OutputCallback cmSystemTools::s_StdoutCallback;
-cmSystemTools::OutputCallback cmSystemTools::s_StderrCallback;
-cmSystemTools::InterruptCallback cmSystemTools::s_InterruptCallback;
-void* cmSystemTools::s_MessageCallbackClientData;
-void* cmSystemTools::s_StdoutCallbackClientData;
-void* cmSystemTools::s_StderrCallbackClientData;
-void* cmSystemTools::s_InterruptCallbackClientData;
-
 // replace replace with with as many times as it shows up in source.
 // write the result into source.
 #if defined(_WIN32) && !defined(__CYGWIN__)
@@ -277,42 +277,38 @@ void cmSystemTools::Error(const std::string& m)
   cmSystemTools::Message(message, "Error");
 }
 
-void cmSystemTools::SetInterruptCallback(InterruptCallback f, void* clientData)
+void cmSystemTools::SetInterruptCallback(InterruptCallback f)
 {
-  s_InterruptCallback = f;
-  s_InterruptCallbackClientData = clientData;
+  s_InterruptCallback = std::move(f);
 }
 
 bool cmSystemTools::GetInterruptFlag()
 {
   if (s_InterruptCallback) {
-    return (*s_InterruptCallback)(s_InterruptCallbackClientData);
+    return s_InterruptCallback();
   }
   return false;
 }
 
-void cmSystemTools::SetMessageCallback(MessageCallback f, void* clientData)
+void cmSystemTools::SetMessageCallback(MessageCallback f)
 {
-  s_MessageCallback = f;
-  s_MessageCallbackClientData = clientData;
+  s_MessageCallback = std::move(f);
 }
 
-void cmSystemTools::SetStdoutCallback(OutputCallback f, void* clientData)
+void cmSystemTools::SetStdoutCallback(OutputCallback f)
 {
-  s_StdoutCallback = f;
-  s_StdoutCallbackClientData = clientData;
+  s_StdoutCallback = std::move(f);
 }
 
-void cmSystemTools::SetStderrCallback(OutputCallback f, void* clientData)
+void cmSystemTools::SetStderrCallback(OutputCallback f)
 {
-  s_StderrCallback = f;
-  s_StderrCallbackClientData = clientData;
+  s_StderrCallback = std::move(f);
 }
 
 void cmSystemTools::Stderr(const std::string& s)
 {
   if (s_StderrCallback) {
-    (*s_StderrCallback)(s.c_str(), s.length(), s_StderrCallbackClientData);
+    s_StderrCallback(s.c_str(), s.length());
   } else {
     std::cerr << s << std::flush;
   }
@@ -321,7 +317,7 @@ void cmSystemTools::Stderr(const std::string& s)
 void cmSystemTools::Stdout(const std::string& s)
 {
   if (s_StdoutCallback) {
-    (*s_StdoutCallback)(s.c_str(), s.length(), s_StdoutCallbackClientData);
+    s_StdoutCallback(s.c_str(), s.length());
   } else {
     std::cout << s << std::flush;
   }
@@ -333,8 +329,7 @@ void cmSystemTools::Message(const char* m1, const char* title)
     return;
   }
   if (s_MessageCallback) {
-    (*s_MessageCallback)(m1, title, s_DisableMessages,
-                         s_MessageCallbackClientData);
+    s_MessageCallback(m1, title);
     return;
   }
   std::cerr << m1 << std::endl << std::flush;

+ 9 - 19
Source/cmSystemTools.h

@@ -10,6 +10,7 @@
 #include "cmProcessOutput.h"
 #include "cmsys/Process.h"
 #include "cmsys/SystemTools.hxx" // IWYU pragma: export
+#include <functional>
 #include <stddef.h>
 #include <string>
 #include <vector>
@@ -55,15 +56,13 @@ public:
    */
   static std::string TrimWhitespace(const std::string& s);
 
-  typedef void (*MessageCallback)(const char*, const char*, bool&, void*);
+  using MessageCallback = std::function<void(const char*, const char*)>;
   /**
    *  Set the function used by GUIs to display error messages
    *  Function gets passed: message as a const char*,
-   *  title as a const char*, and a reference to bool that when
-   *  set to false, will disable further messages (cancel).
+   *  title as a const char*.
    */
-  static void SetMessageCallback(MessageCallback f,
-                                 void* clientData = nullptr);
+  static void SetMessageCallback(MessageCallback f);
 
   /**
    * Display an error message.
@@ -81,19 +80,18 @@ public:
     Message(m.c_str(), title);
   }
 
-  typedef void (*OutputCallback)(const char*, size_t length, void*);
+  using OutputCallback = std::function<void(const char*, size_t)>;
 
   ///! Send a string to stdout
   static void Stdout(const std::string& s);
-  static void SetStdoutCallback(OutputCallback, void* clientData = nullptr);
+  static void SetStdoutCallback(OutputCallback f);
 
   ///! Send a string to stderr
   static void Stderr(const std::string& s);
-  static void SetStderrCallback(OutputCallback, void* clientData = nullptr);
+  static void SetStderrCallback(OutputCallback f);
 
-  typedef bool (*InterruptCallback)(void*);
-  static void SetInterruptCallback(InterruptCallback f,
-                                   void* clientData = nullptr);
+  using InterruptCallback = std::function<bool()>;
+  static void SetInterruptCallback(InterruptCallback f);
   static bool GetInterruptFlag();
 
   ///! Return true if there was an error at any point.
@@ -548,14 +546,6 @@ private:
   static bool s_FatalErrorOccured;
   static bool s_DisableMessages;
   static bool s_DisableRunCommandOutput;
-  static MessageCallback s_MessageCallback;
-  static OutputCallback s_StdoutCallback;
-  static OutputCallback s_StderrCallback;
-  static InterruptCallback s_InterruptCallback;
-  static void* s_MessageCallbackClientData;
-  static void* s_StdoutCallbackClientData;
-  static void* s_StderrCallbackClientData;
-  static void* s_InterruptCallbackClientData;
 };
 
 #endif

+ 3 - 7
Source/cmake.cxx

@@ -157,8 +157,6 @@ cmake::cmake(Role role, cmState::Mode mode)
 #endif
 
   this->GlobalGenerator = nullptr;
-  this->ProgressCallback = nullptr;
-  this->ProgressCallbackClientData = nullptr;
   this->CurrentWorkingMode = NORMAL_MODE;
 
 #ifdef CMAKE_BUILD_WITH_CMAKE
@@ -1922,17 +1920,15 @@ bool cmake::DeleteCache(const std::string& path)
   return this->State->DeleteCache(path);
 }
 
-void cmake::SetProgressCallback(ProgressCallbackType f, void* cd)
+void cmake::SetProgressCallback(ProgressCallbackType f)
 {
-  this->ProgressCallback = f;
-  this->ProgressCallbackClientData = cd;
+  this->ProgressCallback = std::move(f);
 }
 
 void cmake::UpdateProgress(const char* msg, float prog)
 {
   if (this->ProgressCallback && !this->State->GetIsInTryCompile()) {
-    (*this->ProgressCallback)(msg, prog, this->ProgressCallbackClientData);
-    return;
+    this->ProgressCallback(msg, prog);
   }
 }
 

+ 3 - 3
Source/cmake.h

@@ -5,6 +5,7 @@
 
 #include "cmConfigure.h" // IWYU pragma: keep
 
+#include <functional>
 #include <map>
 #include <memory> // IWYU pragma: keep
 #include <set>
@@ -271,7 +272,7 @@ public:
   ///! Parse command line arguments that might set cache values
   bool SetCacheArgs(const std::vector<std::string>&);
 
-  typedef void (*ProgressCallbackType)(const char* msg, float progress, void*);
+  using ProgressCallbackType = std::function<void(const char*, float)>;
   /**
    *  Set the function used by GUIs to receive progress updates
    *  Function gets passed: message as a const char*, a progress
@@ -279,7 +280,7 @@ public:
    *  number provided may be negative in cases where a message is
    *  to be displayed without any progress percentage.
    */
-  void SetProgressCallback(ProgressCallbackType f, void* clientData = nullptr);
+  void SetProgressCallback(ProgressCallbackType f);
 
   ///! this is called by generators to update the progress
   void UpdateProgress(const char* msg, float prog);
@@ -485,7 +486,6 @@ protected:
 
 private:
   ProgressCallbackType ProgressCallback;
-  void* ProgressCallbackClientData;
   bool InTryCompile;
   WorkingMode CurrentWorkingMode;
   bool DebugOutput;

+ 26 - 17
Source/cmakemain.cxx

@@ -117,9 +117,8 @@ int do_cmake(int ac, char const* const* av);
 static int do_build(int ac, char const* const* av);
 static int do_open(int ac, char const* const* av);
 
-static cmMakefile* cmakemainGetMakefile(void* clientdata)
+static cmMakefile* cmakemainGetMakefile(cmake* cm)
 {
-  cmake* cm = static_cast<cmake*>(clientdata);
   if (cm && cm->GetDebugOutput()) {
     cmGlobalGenerator* gg = cm->GetGlobalGenerator();
     if (gg) {
@@ -129,10 +128,10 @@ static cmMakefile* cmakemainGetMakefile(void* clientdata)
   return nullptr;
 }
 
-static std::string cmakemainGetStack(void* clientdata)
+static std::string cmakemainGetStack(cmake* cm)
 {
   std::string msg;
-  cmMakefile* mf = cmakemainGetMakefile(clientdata);
+  cmMakefile* mf = cmakemainGetMakefile(cm);
   if (mf) {
     msg = mf->FormatListFileStack();
     if (!msg.empty()) {
@@ -144,15 +143,14 @@ static std::string cmakemainGetStack(void* clientdata)
 }
 
 static void cmakemainMessageCallback(const char* m, const char* /*unused*/,
-                                     bool& /*unused*/, void* clientdata)
+                                     cmake* cm)
 {
-  std::cerr << m << cmakemainGetStack(clientdata) << std::endl << std::flush;
+  std::cerr << m << cmakemainGetStack(cm) << std::endl << std::flush;
 }
 
-static void cmakemainProgressCallback(const char* m, float prog,
-                                      void* clientdata)
+static void cmakemainProgressCallback(const char* m, float prog, cmake* cm)
 {
-  cmMakefile* mf = cmakemainGetMakefile(clientdata);
+  cmMakefile* mf = cmakemainGetMakefile(cm);
   std::string dir;
   if ((mf) && (strstr(m, "Configuring") == m) && (prog < 0)) {
     dir = " ";
@@ -163,8 +161,7 @@ static void cmakemainProgressCallback(const char* m, float prog,
   }
 
   if ((prog < 0) || (!dir.empty())) {
-    std::cout << "-- " << m << dir << cmakemainGetStack(clientdata)
-              << std::endl;
+    std::cout << "-- " << m << dir << cmakemainGetStack(cm) << std::endl;
   }
 
   std::cout.flush();
@@ -322,8 +319,12 @@ int do_cmake(int ac, char const* const* av)
   cmake cm(role, mode);
   cm.SetHomeDirectory("");
   cm.SetHomeOutputDirectory("");
-  cmSystemTools::SetMessageCallback(cmakemainMessageCallback, &cm);
-  cm.SetProgressCallback(cmakemainProgressCallback, &cm);
+  cmSystemTools::SetMessageCallback([&cm](const char* msg, const char* title) {
+    cmakemainMessageCallback(msg, title, &cm);
+  });
+  cm.SetProgressCallback([&cm](const char* msg, float prog) {
+    cmakemainProgressCallback(msg, prog, &cm);
+  });
   cm.SetWorkingMode(workingMode);
 
   int res = cm.Run(args, view_only);
@@ -498,8 +499,12 @@ static int do_build(int ac, char const* const* av)
   }
 
   cmake cm(cmake::RoleInternal, cmState::Unknown);
-  cmSystemTools::SetMessageCallback(cmakemainMessageCallback, &cm);
-  cm.SetProgressCallback(cmakemainProgressCallback, &cm);
+  cmSystemTools::SetMessageCallback([&cm](const char* msg, const char* title) {
+    cmakemainMessageCallback(msg, title, &cm);
+  });
+  cm.SetProgressCallback([&cm](const char* msg, float prog) {
+    cmakemainProgressCallback(msg, prog, &cm);
+  });
   return cm.Build(jobs, dir, target, config, nativeOptions, clean, verbose);
 #endif
 }
@@ -536,8 +541,12 @@ static int do_open(int ac, char const* const* av)
   }
 
   cmake cm(cmake::RoleInternal, cmState::Unknown);
-  cmSystemTools::SetMessageCallback(cmakemainMessageCallback, &cm);
-  cm.SetProgressCallback(cmakemainProgressCallback, &cm);
+  cmSystemTools::SetMessageCallback([&cm](const char* msg, const char* title) {
+    cmakemainMessageCallback(msg, title, &cm);
+  });
+  cm.SetProgressCallback([&cm](const char* msg, float prog) {
+    cmakemainProgressCallback(msg, prog, &cm);
+  });
   return cm.Open(dir, false) ? 0 : 1;
 #endif
 }