From eac38b249d2bd258f6e2b2a0238392ae4fa77a20 Mon Sep 17 00:00:00 2001 From: ctraizet <cedric.traizet@c-s.fr> Date: Fri, 8 Mar 2019 15:25:34 +0100 Subject: [PATCH] ENH: StandardOneLineFilterWatcher now use a callback --- .../include/otbStandardOneLineFilterWatcher.h | 14 ++++- .../otbStandardOneLineFilterWatcher.hxx} | 52 +++++++++++----- .../include/otbStandardOutputPrintCallback.h | 62 +++++++++++++++++++ Modules/Core/Common/src/CMakeLists.txt | 2 +- .../src/otbStandardOutputPrintCallback.cxx | 47 ++++++++++++++ .../otbStandardOneLineFilterWatcherTest.cxx | 2 +- .../IOGDAL/test/otbGDALOverviewsBuilder.cxx | 2 +- .../src/mvdVectorImageModel.cxx | 2 +- .../include/otbWrapperCommandLineLauncher.h | 2 +- .../src/otbWrapperCommandLineLauncher.cxx | 2 +- 10 files changed, 165 insertions(+), 22 deletions(-) rename Modules/Core/Common/{src/otbStandardOneLineFilterWatcher.cxx => include/otbStandardOneLineFilterWatcher.hxx} (67%) create mode 100644 Modules/Core/Common/include/otbStandardOutputPrintCallback.h create mode 100644 Modules/Core/Common/src/otbStandardOutputPrintCallback.cxx diff --git a/Modules/Core/Common/include/otbStandardOneLineFilterWatcher.h b/Modules/Core/Common/include/otbStandardOneLineFilterWatcher.h index 9cbf761b4c..8c3b6b47fe 100644 --- a/Modules/Core/Common/include/otbStandardOneLineFilterWatcher.h +++ b/Modules/Core/Common/include/otbStandardOneLineFilterWatcher.h @@ -25,6 +25,7 @@ #include <iosfwd> #include "otbFilterWatcherBase.h" +#include "otbStandardOutputPrintCallback.h" namespace otb { @@ -51,10 +52,10 @@ namespace otb * * \ingroup OTBCommon */ +template <class PrintCallbackType = StandardOutputPrintCallback> class OTBCommon_EXPORT StandardOneLineFilterWatcher : public FilterWatcherBase { public: - /** Constructor. Takes a ProcessObject to monitor and an optional * comment string that is prepended to each event message. */ StandardOneLineFilterWatcher(itk::ProcessObject* process, @@ -66,6 +67,9 @@ public: /** Default constructor */ StandardOneLineFilterWatcher(); + /** Destrucotr */ + virtual ~StandardOneLineFilterWatcher(); + /** Get/Set number of stars */ void SetStars(int count) { @@ -97,8 +101,16 @@ private: bool m_CoutIsConsole; std::string m_Buffer; + + PrintCallbackType * m_Callback; + + PrintCallbackType * m_DefaultCallback; }; } // end namespace otb +#ifndef OTB_MANUAL_INSTANTIATION +#include "otbStandardOneLineFilterWatcher.hxx" +#endif + #endif diff --git a/Modules/Core/Common/src/otbStandardOneLineFilterWatcher.cxx b/Modules/Core/Common/include/otbStandardOneLineFilterWatcher.hxx similarity index 67% rename from Modules/Core/Common/src/otbStandardOneLineFilterWatcher.cxx rename to Modules/Core/Common/include/otbStandardOneLineFilterWatcher.hxx index f247061da6..129ccefa6b 100644 --- a/Modules/Core/Common/src/otbStandardOneLineFilterWatcher.cxx +++ b/Modules/Core/Common/include/otbStandardOneLineFilterWatcher.hxx @@ -19,6 +19,9 @@ * limitations under the License. */ +#ifndef otbStandardOneLineFilterWatcher_hxx +#define otbStandardOneLineFilterWatcher_hxx + #include <iostream> #include <sstream> @@ -28,16 +31,19 @@ namespace otb { - -StandardOneLineFilterWatcher +template<class PrintCallbackType> +StandardOneLineFilterWatcher<PrintCallbackType> ::StandardOneLineFilterWatcher() : m_StarsCount(50), m_CurrentNbStars(-1), m_CoutIsConsole( System::IsInteractive(1) ) { + m_DefaultCallback = new PrintCallbackType; + m_Callback = m_DefaultCallback; } -StandardOneLineFilterWatcher +template<class PrintCallbackType> +StandardOneLineFilterWatcher<PrintCallbackType> ::StandardOneLineFilterWatcher(itk::ProcessObject* process, const char *comment) : FilterWatcherBase(process, comment), @@ -45,9 +51,12 @@ StandardOneLineFilterWatcher m_CurrentNbStars(-1), m_CoutIsConsole( System::IsInteractive(1) ) { + m_DefaultCallback = new PrintCallbackType; + m_Callback = m_DefaultCallback; } -StandardOneLineFilterWatcher +template<class PrintCallbackType> +StandardOneLineFilterWatcher<PrintCallbackType> ::StandardOneLineFilterWatcher(itk::ProcessObject* process, const std::string& comment) : FilterWatcherBase(process, comment.c_str()), @@ -55,10 +64,20 @@ StandardOneLineFilterWatcher m_CurrentNbStars(-1), m_CoutIsConsole( System::IsInteractive(1) ) { + m_DefaultCallback = new PrintCallbackType; + m_Callback = m_DefaultCallback; } +template<class PrintCallbackType> +StandardOneLineFilterWatcher<PrintCallbackType> +::~StandardOneLineFilterWatcher() +{ + delete m_DefaultCallback; +} + +template<class PrintCallbackType> void -StandardOneLineFilterWatcher +StandardOneLineFilterWatcher<PrintCallbackType> ::ShowProgress() { if (m_Process) @@ -90,9 +109,10 @@ StandardOneLineFilterWatcher oss << m_Comment << ": " << progressPercent << "% [" << stars << blanks << "]"; - if (m_CoutIsConsole) + if (m_Callback->IsInteractive()) { - std::cout << "\r" << oss.str() << std::flush; + m_Callback->Call("\r" + oss.str()); + m_Callback->Flush(); } else { @@ -104,29 +124,31 @@ StandardOneLineFilterWatcher } } +template<class PrintCallbackType> void -StandardOneLineFilterWatcher +StandardOneLineFilterWatcher<PrintCallbackType> ::StartFilter() { m_Stopwatch.Start(); } +template<class PrintCallbackType> void -StandardOneLineFilterWatcher +StandardOneLineFilterWatcher<PrintCallbackType> ::EndFilter() { m_Stopwatch.Stop(); - if (m_Process && !m_CoutIsConsole) + if (m_Process && !m_Callback->IsInteractive()) { - std::cout << m_Buffer; + m_Callback->Call(m_Buffer); m_Buffer = std::string(""); } - std::cout << " ("; - m_Stopwatch.GetElapsedHumanReadableTime(std::cout); - std::cout << ")" - << std::endl; + m_Callback->Call(" (" + m_Stopwatch.GetElapsedHumanReadableTime() + ")\n"); + } } // end namespace otb + +#endif diff --git a/Modules/Core/Common/include/otbStandardOutputPrintCallback.h b/Modules/Core/Common/include/otbStandardOutputPrintCallback.h new file mode 100644 index 0000000000..bc5b39a9c4 --- /dev/null +++ b/Modules/Core/Common/include/otbStandardOutputPrintCallback.h @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES) + * + * This file is part of Orfeo Toolbox + * + * https://www.orfeo-toolbox.org/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef otbStandardOutputPrintCallback_h +#define otbStandardOutputPrintCallback_h + +#include "otbSystem.h" + +namespace otb +{ + +/** \class StandardOutputPrintCallback + * \brief Class with printing callback methods using the standard output + * + * This class defines the Call method, used to write a string the standard + * output, the Flush method, used to flush it, and the IsInteractive + * method used to determine if the output is the console. + */ + +class StandardOutputPrintCallback +{ +public: + /** Constructor */ + StandardOutputPrintCallback() :m_IsInteractive( System::IsInteractive(1)) {}; + + /** Destructor */ + virtual ~StandardOutputPrintCallback() = default; + + /** Write a string to a buffer */ + void Call(std::string const& content); + + /** Flush the buffer */ + void Flush(); + + /** Determine if the output is interactive */ + bool IsInteractive(); + +private: + /** flag determining if the output is interactive */ + bool m_IsInteractive; +}; + +} // namespace otb + +#endif // otbStandardOutputPrintCallback_h diff --git a/Modules/Core/Common/src/CMakeLists.txt b/Modules/Core/Common/src/CMakeLists.txt index 91abc0b63f..cfbef0afd4 100644 --- a/Modules/Core/Common/src/CMakeLists.txt +++ b/Modules/Core/Common/src/CMakeLists.txt @@ -25,12 +25,12 @@ set(OTBCommon_SRC otbStandardWriterWatcher.cxx otbUtils.cxx otbConfigurationManager.cxx - otbStandardOneLineFilterWatcher.cxx otbWriterWatcherBase.cxx otbStopwatch.cxx otbStringToHTML.cxx otbExtendedFilenameHelper.cxx otbLogger.cxx + otbStandardOutputPrintCallback.cxx ) add_library(OTBCommon ${OTBCommon_SRC}) diff --git a/Modules/Core/Common/src/otbStandardOutputPrintCallback.cxx b/Modules/Core/Common/src/otbStandardOutputPrintCallback.cxx new file mode 100644 index 0000000000..2f41413405 --- /dev/null +++ b/Modules/Core/Common/src/otbStandardOutputPrintCallback.cxx @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES) + * + * This file is part of Orfeo Toolbox + * + * https://www.orfeo-toolbox.org/ + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "otbStandardOutputPrintCallback.h" + +namespace otb +{ + +void +StandardOutputPrintCallback +::Call(std::string const& content) +{ + std::cout << content; +} + +void +StandardOutputPrintCallback +::Flush() +{ + std::cout << std::flush; +} + +bool +StandardOutputPrintCallback +::IsInteractive() +{ + return m_IsInteractive; +} + +} // namespace otb diff --git a/Modules/Core/Common/test/otbStandardOneLineFilterWatcherTest.cxx b/Modules/Core/Common/test/otbStandardOneLineFilterWatcherTest.cxx index 5ef1f5eac1..dfd9bfcdac 100644 --- a/Modules/Core/Common/test/otbStandardOneLineFilterWatcherTest.cxx +++ b/Modules/Core/Common/test/otbStandardOneLineFilterWatcherTest.cxx @@ -38,7 +38,7 @@ int otbStandardOneLineFilterWatcherTest(int itkNotUsed(argc), char * argv[]) typedef itk::GradientMagnitudeImageFilter<ImageType, ImageType> FilterType; FilterType::Pointer gradient = FilterType::New(); - typedef otb::StandardOneLineFilterWatcher WatcherType; + typedef otb::StandardOneLineFilterWatcher<> WatcherType; WatcherType watcher1(gradient, "Gradient"); gradient->SetInput(reader->GetOutput()); diff --git a/Modules/IO/IOGDAL/test/otbGDALOverviewsBuilder.cxx b/Modules/IO/IOGDAL/test/otbGDALOverviewsBuilder.cxx index 4452cb65fc..f250c83b96 100644 --- a/Modules/IO/IOGDAL/test/otbGDALOverviewsBuilder.cxx +++ b/Modules/IO/IOGDAL/test/otbGDALOverviewsBuilder.cxx @@ -42,7 +42,7 @@ int otbGDALOverviewsBuilder(int itkNotUsed(argc), char* argv[]) filter->SetResamplingMethod(resamp); { - StandardOneLineFilterWatcher watcher(filter,"Overviews creation"); + StandardOneLineFilterWatcher<> watcher(filter,"Overviews creation"); filter->Update(); } diff --git a/Modules/Visualization/MonteverdiCore/src/mvdVectorImageModel.cxx b/Modules/Visualization/MonteverdiCore/src/mvdVectorImageModel.cxx index 6470adfd61..24772864ba 100644 --- a/Modules/Visualization/MonteverdiCore/src/mvdVectorImageModel.cxx +++ b/Modules/Visualization/MonteverdiCore/src/mvdVectorImageModel.cxx @@ -260,7 +260,7 @@ VectorImageModel try { - otb::StandardOneLineFilterWatcher watcher( + otb::StandardOneLineFilterWatcher<> watcher( filter, ToStdString( tr( "Overviews creation: " ) ) ); diff --git a/Modules/Wrappers/CommandLine/include/otbWrapperCommandLineLauncher.h b/Modules/Wrappers/CommandLine/include/otbWrapperCommandLineLauncher.h index cc28ee8e9c..9e097a67cf 100644 --- a/Modules/Wrappers/CommandLine/include/otbWrapperCommandLineLauncher.h +++ b/Modules/Wrappers/CommandLine/include/otbWrapperCommandLineLauncher.h @@ -76,7 +76,7 @@ public: INVALIDNUMBEROFVALUE, DEFAULT} ParamResultType; /** Filter watcher list type */ - typedef std::vector<StandardOneLineFilterWatcher *> WatcherListType; + typedef std::vector<StandardOneLineFilterWatcher<> *> WatcherListType; /** Command Member */ typedef itk::MemberCommand< Self > AddProcessCommandType; diff --git a/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx b/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx index bed8cd21dc..a68201fa8f 100644 --- a/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx +++ b/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx @@ -596,7 +596,7 @@ void CommandLineLauncher::LinkWatchers(itk::Object * itkNotUsed(caller), const i { const AddProcessToWatchEvent* eventToWatch = dynamic_cast<const AddProcessToWatchEvent*> (&event); - StandardOneLineFilterWatcher * watch = new StandardOneLineFilterWatcher(eventToWatch->GetProcess(), + auto watch = new StandardOneLineFilterWatcher<>(eventToWatch->GetProcess(), eventToWatch->GetProcessDescription()); m_WatcherList.push_back(watch); } -- GitLab