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 <moveit/macros/class_forward.h>
00041 #include <boost/thread/condition.hpp>
00042 #include <boost/thread/mutex.hpp>
00043 #include <boost/thread/locks.hpp>
00044
00045 namespace mesh_filter
00046 {
00047 MOVEIT_CLASS_FORWARD(Job);
00048
00053 class Job
00054 {
00055 public:
00056 Job() : done_(false)
00057 {
00058 }
00059 inline void wait() const;
00060 virtual void execute() = 0;
00061 inline void cancel();
00062 inline bool isDone() const;
00063
00064 protected:
00065 bool done_;
00066 mutable boost::condition_variable condition_;
00067 mutable boost::mutex mutex_;
00068 };
00069
00070 void Job::wait() const
00071 {
00072 boost::unique_lock<boost::mutex> lock(mutex_);
00073 while (!done_)
00074 condition_.wait(lock);
00075 }
00076
00077 void Job::cancel()
00078 {
00079 boost::unique_lock<boost::mutex> lock(mutex_);
00080 done_ = true;
00081 condition_.notify_all();
00082 }
00083
00084 bool Job::isDone() const
00085 {
00086 return done_;
00087 }
00088
00089 template <typename ReturnType>
00090 class FilterJob : public Job
00091 {
00092 public:
00093 FilterJob(const boost::function<ReturnType()>& exec) : Job(), exec_(exec)
00094 {
00095 }
00096 virtual void execute();
00097 const ReturnType& getResult() const;
00098
00099 private:
00100 boost::function<ReturnType()> exec_;
00101 ReturnType result_;
00102 };
00103
00104 template <typename ReturnType>
00105 void FilterJob<ReturnType>::execute()
00106 {
00107 boost::unique_lock<boost::mutex> lock(mutex_);
00108 if (!done_)
00109 result_ = exec_();
00110
00111 done_ = true;
00112 condition_.notify_all();
00113 }
00114
00115 template <typename ReturnType>
00116 const ReturnType& FilterJob<ReturnType>::getResult() const
00117 {
00118 wait();
00119 return result_;
00120 }
00121
00122 template <>
00123 class FilterJob<void> : public Job
00124 {
00125 public:
00126 FilterJob(const boost::function<void()>& exec) : Job(), exec_(exec)
00127 {
00128 }
00129 virtual void execute()
00130 {
00131 boost::unique_lock<boost::mutex> lock(mutex_);
00132 if (!done_)
00133 exec_();
00134
00135 done_ = true;
00136 condition_.notify_all();
00137 }
00138
00139 private:
00140 boost::function<void()> exec_;
00141 };
00142 }
00143 #endif