async.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #ifndef OPENCV_CORE_ASYNC_HPP
  5. #define OPENCV_CORE_ASYNC_HPP
  6. #include <opencv2/core/mat.hpp>
  7. #ifdef CV_CXX11
  8. //#include <future>
  9. #include <chrono>
  10. #endif
  11. namespace cv {
  12. /** @addtogroup core_async
  13. @{
  14. */
  15. /** @brief Returns result of asynchronous operations
  16. Object has attached asynchronous state.
  17. Assignment operator doesn't clone asynchronous state (it is shared between all instances).
  18. Result can be fetched via get() method only once.
  19. */
  20. class CV_EXPORTS_W AsyncArray
  21. {
  22. public:
  23. ~AsyncArray() CV_NOEXCEPT;
  24. CV_WRAP AsyncArray() CV_NOEXCEPT;
  25. AsyncArray(const AsyncArray& o) CV_NOEXCEPT;
  26. AsyncArray& operator=(const AsyncArray& o) CV_NOEXCEPT;
  27. CV_WRAP void release() CV_NOEXCEPT;
  28. /** Fetch the result.
  29. @param[out] dst destination array
  30. Waits for result until container has valid result.
  31. Throws exception if exception was stored as a result.
  32. Throws exception on invalid container state.
  33. @note Result or stored exception can be fetched only once.
  34. */
  35. CV_WRAP void get(OutputArray dst) const;
  36. /** Retrieving the result with timeout
  37. @param[out] dst destination array
  38. @param[in] timeoutNs timeout in nanoseconds, -1 for infinite wait
  39. @returns true if result is ready, false if the timeout has expired
  40. @note Result or stored exception can be fetched only once.
  41. */
  42. bool get(OutputArray dst, int64 timeoutNs) const;
  43. CV_WRAP inline
  44. bool get(OutputArray dst, double timeoutNs) const { return get(dst, (int64)timeoutNs); }
  45. bool wait_for(int64 timeoutNs) const;
  46. CV_WRAP inline
  47. bool wait_for(double timeoutNs) const { return wait_for((int64)timeoutNs); }
  48. CV_WRAP bool valid() const CV_NOEXCEPT;
  49. #ifdef CV_CXX11
  50. inline AsyncArray(AsyncArray&& o) { p = o.p; o.p = NULL; }
  51. inline AsyncArray& operator=(AsyncArray&& o) CV_NOEXCEPT { std::swap(p, o.p); return *this; }
  52. template<typename _Rep, typename _Period>
  53. inline bool get(OutputArray dst, const std::chrono::duration<_Rep, _Period>& timeout)
  54. {
  55. return get(dst, (int64)(std::chrono::nanoseconds(timeout).count()));
  56. }
  57. template<typename _Rep, typename _Period>
  58. inline bool wait_for(const std::chrono::duration<_Rep, _Period>& timeout)
  59. {
  60. return wait_for((int64)(std::chrono::nanoseconds(timeout).count()));
  61. }
  62. #if 0
  63. std::future<Mat> getFutureMat() const;
  64. std::future<UMat> getFutureUMat() const;
  65. #endif
  66. #endif
  67. // PImpl
  68. struct Impl; friend struct Impl;
  69. inline void* _getImpl() const CV_NOEXCEPT { return p; }
  70. protected:
  71. Impl* p;
  72. };
  73. //! @}
  74. } // namespace
  75. #endif // OPENCV_CORE_ASYNC_HPP