forked from infinit/elle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackgroundOperation.hh
75 lines (67 loc) · 1.77 KB
/
BackgroundOperation.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
#include <elle/optional.hh>
#include <elle/reactor/Operation.hh>
namespace elle
{
namespace reactor
{
namespace
{
template <typename T>
class BackgroundOperationResult
{
protected:
void
_result_set(T&& v);
ELLE_ATTRIBUTE_R(boost::optional<T>, result, protected);
};
template <>
class BackgroundOperationResult<void>
{
protected:
void
_result_set(bool);
};
}
/// BackgroundOperation is a specialized Operation to run background
/// operations in the Scheduler.
///
/// BackgroundOperation run the action in an std::thread and handle the
/// locking for you.
template<typename T>
class BackgroundOperation
: public Operation
, public BackgroundOperationResult<T>
{
public:
using Action = std::function<T ()>;
struct Status
{
bool aborted;
};
public:
/// Construct a BackgroundOperation from an Action.
///
/// \param action The Action to perform.
BackgroundOperation(Action const& action);
~BackgroundOperation();
ELLE_ATTRIBUTE(Action, action);
ELLE_ATTRIBUTE(std::shared_ptr<Status>, status);
protected:
/// Start the Operation by running the Action via
/// Scheduler::_run_background.
///
/// When the operation is over, store the in
/// BackgroundOperationResult::_result and call Operation::done();
void
_start() override;
/// Abort the current BackgroundOperation, ignoring its result.
///
/// Operation::done() is called but BackgroundOperationResult::_result is
/// is left empty.
void
_abort() override;
};
}
}
#include <elle/reactor/BackgroundOperation.hxx>