00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef GRIDFUTURE_IMPL_INCLUDED
00011 #define GRIDFUTURE_IMPL_INCLUDED
00012
00013 #include <boost/thread/future.hpp>
00014 #include <boost/variant.hpp>
00015
00016 #include "gridgain/gridfuture.hpp"
00017 #include "gridgain/gridclientexception.hpp"
00018 #include "gridgain/impl/utils/gridthreadpool.hpp"
00019
00023 template<class T>
00024 class GridThreadPoolTaskPackagedTaskAdapter: public GridThreadPoolTask {
00025 public:
00031 GridThreadPoolTaskPackagedTaskAdapter(boost::packaged_task<T>&& pt):
00032 pt(static_cast<boost::packaged_task<T>&&>(pt)) {
00033 }
00034
00038 virtual void run() {
00039 pt();
00040 }
00041
00045 virtual void cancel() {
00046
00047
00048 pt = boost::packaged_task<T>();
00049 }
00050
00051 private:
00053 boost::packaged_task<T> pt;
00054 };
00055
00059 class GridBoolFutureImpl: public GridClientBoolFuture {
00060 public:
00061 GridBoolFutureImpl(TGridThreadPoolPtr& threadPool): threadPool(threadPool) {}
00062
00068 virtual bool get() const {
00069 return const_cast<GridBoolFutureImpl*>(this)->fut_.get();
00070 }
00071
00080 virtual bool get(const boost::posix_time::time_duration& timeout) const {
00081 bool waitRes = const_cast<GridBoolFutureImpl*>(this)->fut_.timed_wait(timeout);
00082
00083 if(waitRes) {
00084 return const_cast<GridBoolFutureImpl*>(this)->fut_.get();
00085 }
00086 else {
00087 throw GridClientFutureTimeoutException();
00088 }
00089 }
00090
00096 void task(boost::packaged_task<bool>& pt) {
00097 fut_ = pt.get_future();
00098
00099 TGridThreadPoolTaskPtr task(new GridThreadPoolTaskPackagedTaskAdapter<bool>(boost::move(pt)));
00100
00101 threadPool->execute(task);
00102 }
00103
00104 private:
00106 boost::unique_future<bool> fut_;
00107
00109 TGridThreadPoolPtr threadPool;
00110 };
00111
00120 template<class ExT>
00121 class GridBoolFailFutureImpl: public GridClientBoolFuture {
00122 public:
00123 GridBoolFailFutureImpl() {}
00124
00130 virtual bool get() const {
00131 throw ExT();
00132 }
00133
00139 virtual bool get(const boost::posix_time::time_duration& timeout) const {
00140 throw ExT();
00141 }
00142 };
00143
00147 template<class T>
00148 class GridFutureImpl: public GridClientFuture<T> {
00149 public:
00150
00152 GridFutureImpl(TGridThreadPoolPtr& threadPool)
00153 : success_(false), threadPool(threadPool) {
00154 }
00155
00156 virtual ~GridFutureImpl() {
00157 }
00158
00164 virtual bool success() const {
00165 return success_;
00166 }
00167
00173 virtual T result() const {
00174 return t_;
00175 }
00176
00182 virtual T get() {
00183 t_ = const_cast<GridFutureImpl*>(this)->fut_.get();
00184
00185 success_ = true;
00186
00187 return t_;
00188 }
00189
00198 virtual T get(const boost::posix_time::time_duration& timeout) {
00199 bool waitRes = const_cast<GridFutureImpl*>(this)->fut_.timed_wait(timeout);
00200
00201 if(waitRes) {
00202 t_ = const_cast<GridFutureImpl*>(this)->fut_.get();
00203
00204 success_ = true;
00205
00206 return t_;
00207 }
00208 else {
00209 success_ = false;
00210
00211 throw GridClientFutureTimeoutException();
00212 }
00213 }
00214
00220 void task(boost::packaged_task<T>& pt) {
00221 fut_ = pt.get_future();
00222
00223 TGridThreadPoolTaskPtr task(new GridThreadPoolTaskPackagedTaskAdapter<T>(boost::move(pt)));
00224
00225 threadPool->execute(task);
00226 }
00227
00228 private:
00230 T t_;
00231
00233 boost::unique_future<T> fut_;
00234
00236 bool success_;
00237
00239 TGridThreadPoolPtr threadPool;
00240 };
00241
00251 template<class T, class ExT>
00252 class GridFailFutureImpl: public GridClientFuture<T> {
00253 public:
00257 virtual bool success() const {
00258 return false;
00259 }
00260
00264 virtual T result() const {
00265 throw ExT();
00266 }
00267
00273 virtual T get() {
00274 throw ExT();
00275 }
00276
00282 virtual T get(const boost::posix_time::time_duration& timeout) {
00283 throw ExT();
00284 }
00285 };
00286
00287 #endif