Leosac  0.8.0
Open Source Access Control
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 #include <functional>
26 
27 namespace Leosac
28 {
29 namespace Tasks
30 {
37 class Task
38 {
39  public:
40  Task();
41  virtual ~Task()
42  {
43  }
44  Task(const Task &) = delete;
45  Task(Task &&) = delete;
46  Task &operator=(const Task &) = delete;
47  Task &operator=(Task &&) = delete;
48 
53  bool is_complete() const;
54 
61  void wait();
62 
63  void run();
64 
65  bool succeed() const;
66 
67  std::exception_ptr get_exception() const;
68 
76  template <typename Callback>
77  void set_on_completion(Callback c)
78  {
79  on_completion_ = [=]() { c(); };
80  }
81 
88  template <typename Callback>
89  void set_on_success(Callback c)
90  {
91  on_success_ = [=]() { c(); };
92  }
93 
100  template <typename Callback>
101  void set_on_failure(Callback c)
102  {
103  on_failure_ = [=]() { c(); };
104  }
105 
106  const std::string &get_guid() const;
107 
108  private:
109  virtual bool do_run() = 0;
110 
111  std::function<void(void)> on_completion_;
112  std::function<void(void)> on_success_;
113  std::function<void(void)> on_failure_;
114 
115  bool success_;
116  std::exception_ptr eptr_;
117 
118  std::mutex mutex_;
119  std::atomic_bool complete_;
120  std::condition_variable cv_;
121  std::string guid_;
122 };
123 }
124 }
Leosac::Tasks::Task::on_completion_
std::function< void(void)> on_completion_
Definition: Task.hpp:111
Leosac::Tasks::Task::mutex_
std::mutex mutex_
Definition: Task.hpp:118
Leosac::Tasks::Task::run
void run()
Definition: Task.cpp:43
Leosac::Tasks::Task::set_on_failure
void set_on_failure(Callback c)
Set a callback that will be invoked if and when the task fails.
Definition: Task.hpp:101
Leosac::Tasks::Task::do_run
virtual bool do_run()=0
Leosac::Tasks::Task
A base class for a tasks.
Definition: Task.hpp:37
Leosac::Tasks::Task::on_success_
std::function< void(void)> on_success_
Definition: Task.hpp:112
Leosac::Tasks::Task::eptr_
std::exception_ptr eptr_
Definition: Task.hpp:116
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Tasks::Task::guid_
std::string guid_
Definition: Task.hpp:121
Leosac::Tasks::Task::cv_
std::condition_variable cv_
Definition: Task.hpp:120
Leosac::Tasks::Task::operator=
Task & operator=(const Task &)=delete
Leosac::Tasks::Task::set_on_completion
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:77
Leosac::Tasks::Task::wait
void wait()
Instead of spinlocking over is_complete() one can call wait() to hum...
Definition: Task.cpp:80
Leosac::Tasks::Task::on_failure_
std::function< void(void)> on_failure_
Definition: Task.hpp:113
Leosac::Tasks::Task::Task
Task()
Definition: Task.cpp:27
Leosac::Tasks::Task::complete_
std::atomic_bool complete_
Definition: Task.hpp:119
Leosac::Tasks::Task::success_
bool success_
Definition: Task.hpp:115
Leosac::Tasks::Task::is_complete
bool is_complete() const
Has the tasks completed its execution.
Definition: Task.cpp:38
Leosac::Tasks::Task::set_on_success
void set_on_success(Callback c)
Set a callback that will be invoked if and when the task succeed.
Definition: Task.hpp:89
Leosac::Tasks::Task::get_guid
const std::string & get_guid() const
Definition: Task.cpp:98
Leosac::Tasks::Task::~Task
virtual ~Task()
Definition: Task.hpp:41
Leosac::Tasks::Task::get_exception
std::exception_ptr get_exception() const
Definition: Task.cpp:93
Leosac::Tasks::Task::succeed
bool succeed() const
Definition: Task.cpp:88