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 <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_) // not canceled !
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_) // not canceled !
00130         exec_ ();
00131 
00132       done_ = true;
00133       condition_.notify_all ();
00134     }
00135 
00136   private:
00137     boost::function<void ()> exec_;
00138 };
00139 
00140 }
00141 #endif


perception
Author(s): Ioan Sucan , Jon Binney , Suat Gedikli
autogenerated on Wed Aug 26 2015 12:43:21