Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef MOVEIT_MESH_FILTER_FILTER_JOB_
00038 #define MOVEIT_MESH_FILTER_FILTER_JOB_
00039
00040 #include <boost/thread/condition.hpp>
00041 #include <boost/thread/mutex.hpp>
00042 #include <boost/thread/locks.hpp>
00043
00044 namespace mesh_filter
00045 {
00050 class Job
00051 {
00052 public:
00053 Job ()
00054 : done_ (false)
00055 {}
00056 inline void wait () const;
00057 virtual void execute () = 0;
00058 inline void cancel ();
00059 inline bool isDone () const;
00060 protected:
00061 bool done_;
00062 mutable boost::condition_variable condition_;
00063 mutable boost::mutex mutex_;
00064 };
00065
00066 void Job::wait () const
00067 {
00068 boost::unique_lock<boost::mutex> lock (mutex_);
00069 while (!done_)
00070 condition_.wait (lock);
00071 }
00072
00073 void Job::cancel ()
00074 {
00075 boost::unique_lock<boost::mutex> lock (mutex_);
00076 done_ = true;
00077 condition_.notify_all ();
00078 }
00079
00080 bool Job::isDone () const
00081 {
00082 return done_;
00083 }
00084
00085 template <typename ReturnType>
00086 class FilterJob : public Job
00087 {
00088 public:
00089 FilterJob (const boost::function<ReturnType ()>& exec)
00090 : Job ()
00091 , exec_ (exec)
00092 {}
00093 virtual void execute ();
00094 const ReturnType& getResult () const;
00095 private:
00096 boost::function<ReturnType ()> exec_;
00097 ReturnType result_;
00098 };
00099
00100 template <typename ReturnType>
00101 void FilterJob<ReturnType>::execute ()
00102 {
00103 boost::unique_lock<boost::mutex> lock (mutex_);
00104 if (!done_)
00105 result_ = exec_ ();
00106
00107 done_ = true;
00108 condition_.notify_all ();
00109 }
00110
00111 template <typename ReturnType>
00112 const ReturnType& FilterJob<ReturnType>::getResult () const
00113 {
00114 wait ();
00115 return result_;
00116 }
00117
00118 template <>
00119 class FilterJob<void> : public Job
00120 {
00121 public:
00122 FilterJob (const boost::function<void ()>& exec)
00123 : Job ()
00124 , exec_ (exec)
00125 {}
00126 virtual void execute ()
00127 {
00128 boost::unique_lock<boost::mutex> lock (mutex_);
00129 if (!done_)
00130 exec_ ();
00131
00132 done_ = true;
00133 condition_.notify_all ();
00134 }
00135
00136 private:
00137 boost::function<void ()> exec_;
00138 };
00139
00140 }
00141 #endif