ctkCmdLineModuleFutureWatcher.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #ifndef CTKCMDLINEMODULEFUTUREWATCHER_H
  16. #define CTKCMDLINEMODULEFUTUREWATCHER_H
  17. #include "ctkCommandLineModulesCoreExport.h"
  18. #include "ctkCmdLineModuleResult.h"
  19. #include "ctkCmdLineModuleFutureInterface.h"
  20. #include <QFutureWatcher>
  21. class ctkCmdLineModuleFuture;
  22. struct ctkCmdLineModuleFutureWatcherPrivate;
  23. /**
  24. * @ingroup CommandLineModulesCore_API
  25. *
  26. * @brief The ctkCmdLineModuleFutureWatcher class provides enhanced monitoring of a
  27. * ctkCmdLineModuleFuture using signals and slots.
  28. *
  29. * This class enhances the standard QFutureWatcher class by adding the two signals
  30. * outputDataReady() and errorDataReady(). These signals are fired whenever the watched
  31. * future reports new output data (usually text written to the standard output channel) or
  32. * new error data (usually text written to the standard error channel).
  33. *
  34. * Use readPendingOutputData() or readPendingErrorData() to get the newly added data.
  35. *
  36. * \warning While you could use a QFutureWatcher<ctkCmdLineModuleResult> instance directly (and
  37. * provide a ctkCmdLineModuleFuture via QFutureWatcher<ctkCmdLineModuleResult>::setFuture(future)
  38. * by virtue of "slicing") this is discouraged. The reason is that a member variable of type
  39. * QFutureWatcher<ctkCmdLineModuleResult> will have a different size, depending on the inclusion
  40. * of the ctkCmdLineModuleFutureInterface.h header (this header provides a specialization of the
  41. * QFutureInterface template which adds a data member). This can lead to subtle heap corruptions.
  42. * Since the code compiles with or without the ctkCmdLindeModuleFutureInterface.h inclusion, you
  43. * should always use ctkCmdLineModuleFutureWatcher when working with ctkCmdLineModuleFuture objects
  44. * to avoid runtime heap corruptions.
  45. */
  46. class CTK_CMDLINEMODULECORE_EXPORT ctkCmdLineModuleFutureWatcher : public QFutureWatcher<ctkCmdLineModuleResult>
  47. {
  48. Q_OBJECT
  49. public:
  50. ctkCmdLineModuleFutureWatcher(QObject* parent = 0);
  51. ~ctkCmdLineModuleFutureWatcher();
  52. void setFuture(const ctkCmdLineModuleFuture& future);
  53. ctkCmdLineModuleFuture future() const;
  54. bool event(QEvent* event);
  55. QByteArray readPendingOutputData() const;
  56. QByteArray readPendingErrorData() const;
  57. QByteArray readAllOutputData() const;
  58. QByteArray readAllErrorData() const;
  59. Q_SIGNALS:
  60. void outputDataReady();
  61. void errorDataReady();
  62. private:
  63. friend struct ctkCmdLineModuleFutureWatcherPrivate;
  64. QScopedPointer<ctkCmdLineModuleFutureWatcherPrivate> d;
  65. const ctkCmdLineModuleFutureInterface& futureInterface() const;
  66. ctkCmdLineModuleFutureInterface& futureInterface();
  67. // not imlemented
  68. void setFuture(const QFuture<ctkCmdLineModuleResult>&);
  69. };
  70. #endif // CTKCMDLINEMODULEFUTUREWATCHER_H