diff options
author | Regina Pfeifer <regina@mailbox.org> | 2019-01-25 21:31:33 +0100 |
---|---|---|
committer | Regina Pfeifer <regina@mailbox.org> | 2019-01-29 16:32:52 +0100 |
commit | bcee24aecc1eaf6615eac9f24ae06acdf38845d3 (patch) | |
tree | 7a5ce493d66e1ff9a154bbd7503bd6e646624444 | |
parent | 9620cb935a49e7b4955f5b1d0ffa2e93b4327591 (diff) | |
download | cmake-bcee24aecc1eaf6615eac9f24ae06acdf38845d3.tar.gz |
Use `std::function` for callbacks
-rw-r--r-- | Source/CPack/cmCPackGenerator.cxx | 10 | ||||
-rw-r--r-- | Source/CPack/cpack.cxx | 8 | ||||
-rw-r--r-- | Source/CTest/cmCTestBuildAndTestHandler.cxx | 50 | ||||
-rw-r--r-- | Source/CTest/cmCTestScriptHandler.cxx | 15 | ||||
-rw-r--r-- | Source/CursesDialog/ccmake.cxx | 12 | ||||
-rw-r--r-- | Source/CursesDialog/cmCursesMainForm.cxx | 22 | ||||
-rw-r--r-- | Source/CursesDialog/cmCursesMainForm.h | 3 | ||||
-rw-r--r-- | Source/QtDialog/QCMake.cxx | 49 | ||||
-rw-r--r-- | Source/QtDialog/QCMake.h | 12 | ||||
-rw-r--r-- | Source/cmServer.cxx | 26 | ||||
-rw-r--r-- | Source/cmServer.h | 5 | ||||
-rw-r--r-- | Source/cmSystemTools.cxx | 47 | ||||
-rw-r--r-- | Source/cmSystemTools.h | 25 | ||||
-rw-r--r-- | Source/cmake.cxx | 10 | ||||
-rw-r--r-- | Source/cmake.h | 6 | ||||
-rw-r--r-- | Source/cmakemain.cxx | 46 |
16 files changed, 159 insertions, 187 deletions
diff --git a/Source/CPack/cmCPackGenerator.cxx b/Source/CPack/cmCPackGenerator.cxx index 4728f69577..3309e1b058 100644 --- a/Source/CPack/cmCPackGenerator.cxx +++ b/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; @@ -696,7 +690,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); diff --git a/Source/CPack/cpack.cxx b/Source/CPack/cpack.cxx index d4c867b7fd..0413422b2d 100644 --- a/Source/CPack/cpack.cxx +++ b/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()); diff --git a/Source/CTest/cmCTestBuildAndTestHandler.cxx b/Source/CTest/cmCTestBuildAndTestHandler.cxx index 312d126da2..d6f0d185f5 100644 --- a/Source/CTest/cmCTestBuildAndTestHandler.cxx +++ b/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*/, bool& /*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); } }; diff --git a/Source/CTest/cmCTestScriptHandler.cxx b/Source/CTest/cmCTestScriptHandler.cxx index b94902394a..efc335afbf 100644 --- a/Source/CTest/cmCTestScriptHandler.cxx +++ b/Source/CTest/cmCTestScriptHandler.cxx @@ -265,15 +265,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 @@ -299,7 +290,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); diff --git a/Source/CursesDialog/ccmake.cxx b/Source/CursesDialog/ccmake.cxx index dbf4a28e3f..82295b7b68 100644 --- a/Source/CursesDialog/ccmake.cxx +++ b/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, bool& /*unused*/) { + myform->AddError(message, title); + }); cmCursesForm::CurrentForm = myform; diff --git a/Source/CursesDialog/cmCursesMainForm.cxx b/Source/CursesDialog/cmCursesMainForm.cxx index 188ad69a3a..8ca7802f1f 100644 --- a/Source/CursesDialog/cmCursesMainForm.cxx +++ b/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()) { diff --git a/Source/CursesDialog/cmCursesMainForm.h b/Source/CursesDialog/cmCursesMainForm.h index 824025bcd1..88864bc545 100644 --- a/Source/CursesDialog/cmCursesMainForm.h +++ b/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 diff --git a/Source/QtDialog/QCMake.cxx b/Source/QtDialog/QCMake.cxx index 2eecce65b1..8c71cc07e8 100644 --- a/Source/QtDialog/QCMake.cxx +++ b/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, bool& cancel) { + this->messageCallback(msg, title, cancel); + }); + 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,41 @@ 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) + bool& /*stop*/) { - 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(); } diff --git a/Source/QtDialog/QCMake.h b/Source/QtDialog/QCMake.h index c84da58701..ad85630174 100644 --- a/Source/QtDialog/QCMake.h +++ b/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, bool&); + void stdoutCallback(const char* msg, size_t len); + void stderrCallback(const char* msg, size_t len); + bool WarnUninitializedMode; bool WarnUnusedMode; bool WarnUnusedAllMode; diff --git a/Source/cmServer.cxx b/Source/cmServer.cxx index f7d38799fa..44826fa1d5 100644 --- a/Source/cmServer.cxx +++ b/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, bool& cancel) { + reportMessage(msg, title, cancel, 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) + bool& /*cancel*/, 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) diff --git a/Source/cmServer.h b/Source/cmServer.h index ca37ce276d..10588a0736 100644 --- a/Source/cmServer.h +++ b/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 reportProgress(const char* msg, float progress, + const cmServerRequest& request); static void reportMessage(const char* msg, const char* title, bool& cancel, - void* data); + const cmServerRequest& request); // Handle requests: cmServerResponse SetProtocolVersion(const cmServerRequest& request); diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index f7de3aa71e..f660b3976d 100644 --- a/Source/cmSystemTools.cxx +++ b/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, s_DisableMessages); return; } std::cerr << m1 << std::endl << std::flush; diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index b1d57512da..754929db89 100644 --- a/Source/cmSystemTools.h +++ b/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,14 @@ 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*, bool&)>; /** * 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). */ - static void SetMessageCallback(MessageCallback f, - void* clientData = nullptr); + static void SetMessageCallback(MessageCallback f); /** * Display an error message. @@ -81,19 +81,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 +547,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 diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 70316f10bd..787e7691a7 100644 --- a/Source/cmake.cxx +++ b/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); } } diff --git a/Source/cmake.h b/Source/cmake.h index c60fc33a8c..afd41175a9 100644 --- a/Source/cmake.h +++ b/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; diff --git a/Source/cmakemain.cxx b/Source/cmakemain.cxx index 0ec2552296..f1a223bf80 100644 --- a/Source/cmakemain.cxx +++ b/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) + bool& /*unused*/, 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,13 @@ 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, bool& cancel) { + cmakemainMessageCallback(msg, title, cancel, &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 +500,13 @@ 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, bool& cancel) { + cmakemainMessageCallback(msg, title, cancel, &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 +543,13 @@ 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, bool& cancel) { + cmakemainMessageCallback(msg, title, cancel, &cm); + }); + cm.SetProgressCallback([&cm](const char* msg, float prog) { + cmakemainProgressCallback(msg, prog, &cm); + }); return cm.Open(dir, false) ? 0 : 1; #endif } |