async.hpp 2.7 KB

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