File indexing completed on 2025-01-18 09:29:58
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_COMPUTE_ASYNC_FUTURE_HPP
0012 #define BOOST_COMPUTE_ASYNC_FUTURE_HPP
0013
0014 #include <boost/compute/event.hpp>
0015
0016 namespace boost {
0017 namespace compute {
0018
0019
0020
0021
0022
0023 template<class T>
0024 class future
0025 {
0026 public:
0027 future()
0028 : m_event(0)
0029 {
0030 }
0031
0032 future(const T &result, const event &event)
0033 : m_result(result),
0034 m_event(event)
0035 {
0036 }
0037
0038 future(const future<T> &other)
0039 : m_result(other.m_result),
0040 m_event(other.m_event)
0041 {
0042 }
0043
0044 future& operator=(const future<T> &other)
0045 {
0046 if(this != &other){
0047 m_result = other.m_result;
0048 m_event = other.m_event;
0049 }
0050
0051 return *this;
0052 }
0053
0054 ~future()
0055 {
0056 }
0057
0058
0059
0060 T get()
0061 {
0062 wait();
0063
0064 return m_result;
0065 }
0066
0067
0068 bool valid() const
0069 {
0070 return m_event != 0;
0071 }
0072
0073
0074 void wait() const
0075 {
0076 m_event.wait();
0077 }
0078
0079
0080 event get_event() const
0081 {
0082 return m_event;
0083 }
0084
0085 #if defined(BOOST_COMPUTE_CL_VERSION_1_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
0086
0087
0088
0089
0090
0091
0092 template<class Function>
0093 future& then(Function callback)
0094 {
0095 m_event.set_callback(callback);
0096 return *this;
0097 }
0098 #endif
0099
0100 private:
0101 T m_result;
0102 event m_event;
0103 };
0104
0105
0106 template<>
0107 class future<void>
0108 {
0109 public:
0110 future()
0111 : m_event(0)
0112 {
0113 }
0114
0115 template<class T>
0116 future(const future<T> &other)
0117 : m_event(other.get_event())
0118 {
0119 }
0120
0121 explicit future(const event &event)
0122 : m_event(event)
0123 {
0124 }
0125
0126 template<class T>
0127 future<void> &operator=(const future<T> &other)
0128 {
0129 m_event = other.get_event();
0130
0131 return *this;
0132 }
0133
0134 future<void> &operator=(const future<void> &other)
0135 {
0136 if(this != &other){
0137 m_event = other.m_event;
0138 }
0139
0140 return *this;
0141 }
0142
0143 ~future()
0144 {
0145 }
0146
0147 void get()
0148 {
0149 wait();
0150 }
0151
0152 bool valid() const
0153 {
0154 return m_event != 0;
0155 }
0156
0157 void wait() const
0158 {
0159 m_event.wait();
0160 }
0161
0162 event get_event() const
0163 {
0164 return m_event;
0165 }
0166
0167 #if defined(BOOST_COMPUTE_CL_VERSION_1_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
0168
0169
0170
0171
0172
0173
0174 template<class Function>
0175 future<void> &then(Function callback)
0176 {
0177 m_event.set_callback(callback);
0178 return *this;
0179 }
0180 #endif
0181
0182 private:
0183 event m_event;
0184 };
0185
0186
0187 template<class Result>
0188 inline future<Result> make_future(const Result &result, const event &event)
0189 {
0190 return future<Result>(result, event);
0191 }
0192
0193 }
0194 }
0195
0196 #endif