Leosac  0.7.0
OpenSourceAccessControl
Task.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include <atomic>
23 #include <condition_variable>
24 #include <memory>
25 
26 namespace Leosac
27 {
28 namespace Tasks
29 {
36 class Task
37 {
38  public:
39  Task();
40  virtual ~Task()
41  {
42  }
43  Task(const Task &) = delete;
44  Task(Task &&) = delete;
45  Task &operator=(const Task &) = delete;
46  Task &operator=(Task &&) = delete;
47 
52  bool is_complete() const;
53 
60  void wait();
61 
62  void run();
63 
64  bool succeed() const;
65 
66  std::exception_ptr get_exception() const;
67 
75  template <typename Callback>
76  void set_on_completion(Callback c)
77  {
78  on_completion_ = [=]() { c(); };
79  }
80 
87  template <typename Callback>
88  void set_on_success(Callback c)
89  {
90  on_success_ = [=]() { c(); };
91  }
92 
99  template <typename Callback>
100  void set_on_failure(Callback c)
101  {
102  on_failure_ = [=]() { c(); };
103  }
104 
105  const std::string &get_guid() const;
106 
107  private:
108  virtual bool do_run() = 0;
109 
110  std::function<void(void)> on_completion_;
111  std::function<void(void)> on_success_;
112  std::function<void(void)> on_failure_;
113 
114  bool success_;
115  std::exception_ptr eptr_;
116 
117  std::mutex mutex_;
118  std::atomic_bool complete_;
119  std::condition_variable cv_;
120  std::string guid_;
121 };
122 }
123 }
const std::string & get_guid() const
Definition: Task.cpp:98
std::function< void(void)> on_success_
Definition: Task.hpp:111
std::exception_ptr get_exception() const
Definition: Task.cpp:93
std::exception_ptr eptr_
Definition: Task.hpp:115
virtual ~Task()
Definition: Task.hpp:40
This is the header file for a generated source file, GitSHA1.cpp.
bool succeed() const
Definition: Task.cpp:88
std::string guid_
Definition: Task.hpp:120
void set_on_completion(Callback c)
Set a callback that will be invoked when the tasks is completed, no matter if it succeeded or not...
Definition: Task.hpp:76
Task & operator=(const Task &)=delete
A base class for a tasks.
Definition: Task.hpp:36
virtual bool do_run()=0
bool is_complete() const
Has the tasks completed its execution.
Definition: Task.cpp:38
std::condition_variable cv_
Definition: Task.hpp:119
void wait()
Instead of spinlocking over is_complete() one can call wait() to hum...
Definition: Task.cpp:80
std::function< void(void)> on_completion_
Definition: Task.hpp:110
std::mutex mutex_
Definition: Task.hpp:117
void set_on_failure(Callback c)
Set a callback that will be invoked if and when the task fails.
Definition: Task.hpp:100
std::function< void(void)> on_failure_
Definition: Task.hpp:112
std::atomic_bool complete_
Definition: Task.hpp:118
void set_on_success(Callback c)
Set a callback that will be invoked if and when the task succeed.
Definition: Task.hpp:88