filter_job.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Suat Gedikli */
36 
37 #ifndef MOVEIT_MESH_FILTER_FILTER_JOB_
38 #define MOVEIT_MESH_FILTER_FILTER_JOB_
39 
41 #include <boost/thread/condition.hpp>
42 #include <boost/thread/mutex.hpp>
43 #include <boost/thread/locks.hpp>
44 
45 namespace mesh_filter
46 {
48 
53 class Job
54 {
55 public:
56  Job() : done_(false)
57  {
58  }
59  inline void wait() const;
60  virtual void execute() = 0;
61  inline void cancel();
62  inline bool isDone() const;
63 
64 protected:
65  bool done_;
66  mutable boost::condition_variable condition_;
67  mutable boost::mutex mutex_;
68 };
69 
70 void Job::wait() const
71 {
72  boost::unique_lock<boost::mutex> lock(mutex_);
73  while (!done_)
74  condition_.wait(lock);
75 }
76 
78 {
79  boost::unique_lock<boost::mutex> lock(mutex_);
80  done_ = true;
81  condition_.notify_all();
82 }
83 
84 bool Job::isDone() const
85 {
86  return done_;
87 }
88 
89 template <typename ReturnType>
90 class FilterJob : public Job
91 {
92 public:
93  FilterJob(const boost::function<ReturnType()>& exec) : Job(), exec_(exec)
94  {
95  }
96  virtual void execute();
97  const ReturnType& getResult() const;
98 
99 private:
100  boost::function<ReturnType()> exec_;
101  ReturnType result_;
102 };
103 
104 template <typename ReturnType>
106 {
107  boost::unique_lock<boost::mutex> lock(mutex_);
108  if (!done_) // not canceled !
109  result_ = exec_();
110 
111  done_ = true;
112  condition_.notify_all();
113 }
114 
115 template <typename ReturnType>
116 const ReturnType& FilterJob<ReturnType>::getResult() const
117 {
118  wait();
119  return result_;
120 }
121 
122 template <>
123 class FilterJob<void> : public Job
124 {
125 public:
126  FilterJob(const boost::function<void()>& exec) : Job(), exec_(exec)
127  {
128  }
129  virtual void execute()
130  {
131  boost::unique_lock<boost::mutex> lock(mutex_);
132  if (!done_) // not canceled !
133  exec_();
134 
135  done_ = true;
136  condition_.notify_all();
137  }
138 
139 private:
140  boost::function<void()> exec_;
141 };
142 }
143 #endif
FilterJob(const boost::function< void()> &exec)
Definition: filter_job.h:126
virtual void execute()=0
bool isDone() const
Definition: filter_job.h:84
This class is used to execute functions within the thread that holds the OpenGL context.
Definition: filter_job.h:53
MOVEIT_CLASS_FORWARD(Job)
boost::function< ReturnType()> exec_
Definition: filter_job.h:100
boost::mutex mutex_
Definition: filter_job.h:67
virtual void execute()
Definition: filter_job.h:105
void wait() const
Definition: filter_job.h:70
const ReturnType & getResult() const
Definition: filter_job.h:116
boost::condition_variable condition_
Definition: filter_job.h:66
boost::function< void()> exec_
Definition: filter_job.h:140
FilterJob(const boost::function< ReturnType()> &exec)
Definition: filter_job.h:93


perception
Author(s): Ioan Sucan , Jon Binney , Suat Gedikli
autogenerated on Sun Oct 18 2020 13:17:23