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 #pragma once
38 
40 
41 namespace mesh_filter
42 {
43 MOVEIT_CLASS_FORWARD(Job); // Defines JobPtr, ConstPtr, WeakPtr... etc
44 
49 class Job
50 {
51 public:
52  Job() : done_(false)
53  {
54  }
55  virtual ~Job() = default;
56 
57  inline void wait() const;
58  virtual void execute() = 0;
59  inline void cancel();
60  inline bool isDone() const;
61 
62 protected:
63  bool done_;
64  mutable std::condition_variable condition_;
65  mutable std::mutex mutex_;
66 };
67 
68 void Job::wait() const
69 {
70  std::unique_lock<std::mutex> lock(mutex_);
71  while (!done_)
72  condition_.wait(lock);
73 }
74 
75 void Job::cancel()
76 {
77  std::unique_lock<std::mutex> lock(mutex_);
78  done_ = true;
79  condition_.notify_all();
80 }
81 
82 bool Job::isDone() const
83 {
84  return done_;
85 }
86 
87 template <typename ReturnType>
88 class FilterJob : public Job
89 {
90 public:
91  FilterJob(const std::function<ReturnType()>& exec) : Job(), exec_(exec)
92  {
93  }
94  void execute() override;
95  const ReturnType& getResult() const;
96 
97 private:
98  std::function<ReturnType()> exec_;
99  ReturnType result_;
100 };
101 
102 template <typename ReturnType>
104 {
105  std::unique_lock<std::mutex> lock(mutex_);
106  if (!done_) // not canceled !
107  result_ = exec_();
108 
109  done_ = true;
110  condition_.notify_all();
111 }
112 
113 template <typename ReturnType>
114 const ReturnType& FilterJob<ReturnType>::getResult() const
115 {
116  wait();
117  return result_;
118 }
119 
120 template <>
121 class FilterJob<void> : public Job
122 {
123 public:
124  FilterJob(const std::function<void()>& exec) : Job(), exec_(exec)
125  {
126  }
127  void execute() override
128  {
129  std::unique_lock<std::mutex> lock(mutex_);
130  if (!done_) // not canceled !
131  exec_();
132 
133  done_ = true;
134  condition_.notify_all();
135  }
136 
137 private:
138  std::function<void()> exec_;
139 };
140 } // namespace mesh_filter
mesh_filter::Job::isDone
bool isDone() const
Definition: filter_job.h:114
mesh_filter::FilterJob
Definition: filter_job.h:120
mesh_filter::Job::cancel
void cancel()
Definition: filter_job.h:107
mesh_filter::Job::mutex_
std::mutex mutex_
Definition: filter_job.h:97
mesh_filter::Job::condition_
std::condition_variable condition_
Definition: filter_job.h:96
wait
void wait(int seconds)
mesh_filter::FilterJob::getResult
const ReturnType & getResult() const
Definition: filter_job.h:146
mesh_filter::Job::Job
Job()
Definition: filter_job.h:84
mesh_filter::FilterJob::execute
void execute() override
Definition: filter_job.h:135
mesh_filter::Job::done_
bool done_
Definition: filter_job.h:95
mesh_filter::FilterJob::FilterJob
FilterJob(const std::function< ReturnType()> &exec)
Definition: filter_job.h:123
mutex_
boost::mutex mutex_
mesh_filter::Job::execute
virtual void execute()=0
mesh_filter::FilterJob::result_
ReturnType result_
Definition: filter_job.h:131
mesh_filter::Job::~Job
virtual ~Job()=default
mesh_filter::FilterJob::exec_
std::function< ReturnType()> exec_
Definition: filter_job.h:130
mesh_filter::MOVEIT_CLASS_FORWARD
MOVEIT_CLASS_FORWARD(Job)
class_forward.h
mesh_filter::Job
This class is used to execute functions within the thread that holds the OpenGL context.
Definition: filter_job.h:81
mesh_filter::Job::wait
void wait() const
Definition: filter_job.h:100
mesh_filter
Definition: depth_self_filter_nodelet.h:47


perception
Author(s): Ioan Sucan , Jon Binney , Suat Gedikli
autogenerated on Thu Jun 27 2024 02:27:08