future.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_ASYNC_FUTURE_HPP
  11. #define BOOST_COMPUTE_ASYNC_FUTURE_HPP
  12. #include <boost/compute/event.hpp>
  13. namespace boost {
  14. namespace compute {
  15. /// \class future
  16. /// \brief Holds the result of an asynchronous computation.
  17. ///
  18. /// \see event, wait_list
  19. template<class T>
  20. class future
  21. {
  22. public:
  23. future()
  24. : m_event(0)
  25. {
  26. }
  27. future(const T &result, const event &event)
  28. : m_result(result),
  29. m_event(event)
  30. {
  31. }
  32. future(const future<T> &other)
  33. : m_result(other.m_result),
  34. m_event(other.m_event)
  35. {
  36. }
  37. future& operator=(const future<T> &other)
  38. {
  39. if(this != &other){
  40. m_result = other.m_result;
  41. m_event = other.m_event;
  42. }
  43. return *this;
  44. }
  45. ~future()
  46. {
  47. }
  48. /// Returns the result of the computation. This will block until
  49. /// the result is ready.
  50. T get()
  51. {
  52. wait();
  53. return m_result;
  54. }
  55. /// Returns \c true if the future is valid.
  56. bool valid() const
  57. {
  58. return m_event != 0;
  59. }
  60. /// Blocks until the computation is complete.
  61. void wait() const
  62. {
  63. m_event.wait();
  64. }
  65. /// Returns the underlying event object.
  66. event get_event() const
  67. {
  68. return m_event;
  69. }
  70. private:
  71. T m_result;
  72. event m_event;
  73. };
  74. /// \internal_
  75. template<>
  76. class future<void>
  77. {
  78. public:
  79. future()
  80. : m_event(0)
  81. {
  82. }
  83. template<class T>
  84. future(const future<T> &other)
  85. : m_event(other.get_event())
  86. {
  87. }
  88. explicit future(const event &event)
  89. : m_event(event)
  90. {
  91. }
  92. template<class T>
  93. future<void> &operator=(const future<T> &other)
  94. {
  95. m_event = other.get_event();
  96. return *this;
  97. }
  98. future<void> &operator=(const future<void> &other)
  99. {
  100. if(this != &other){
  101. m_event = other.m_event;
  102. }
  103. return *this;
  104. }
  105. ~future()
  106. {
  107. }
  108. void get()
  109. {
  110. wait();
  111. }
  112. bool valid() const
  113. {
  114. return m_event != 0;
  115. }
  116. void wait() const
  117. {
  118. m_event.wait();
  119. }
  120. event get_event() const
  121. {
  122. return m_event;
  123. }
  124. private:
  125. event m_event;
  126. };
  127. /// \internal_
  128. template<class Result>
  129. inline future<Result> make_future(const Result &result, const event &event)
  130. {
  131. return future<Result>(result, event);
  132. }
  133. } // end compute namespace
  134. } // end boost namespace
  135. #endif // BOOST_COMPUTE_ASYNC_FUTURE_HPP