filter_job.h
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2013, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /* Author: Suat Gedikli */
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_)  // not canceled !
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_)  // not canceled !
00133       exec_();
00134 
00135     done_ = true;
00136     condition_.notify_all();
00137   }
00138 
00139 private:
00140   boost::function<void()> exec_;
00141 };
00142 }
00143 #endif


perception
Author(s): Ioan Sucan , Jon Binney , Suat Gedikli
autogenerated on Mon Jul 24 2017 02:21:13