-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPromise_close.cpp
More file actions
58 lines (48 loc) · 1.45 KB
/
Promise_close.cpp
File metadata and controls
58 lines (48 loc) · 1.45 KB
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
#include <atomic>
#include <cassert>
#include <exception>
#include <iostream>
#include <poolqueue/Promise.hpp>
using poolqueue::Promise;
// Movable but not copyable class.
struct Uncopyable {
Uncopyable() {}
Uncopyable(Uncopyable&&) {
std::cout << "move constructor\n";
}
Uncopyable(const Uncopyable&) = delete;
Uncopyable& operator=(const Uncopyable&) = delete;
};
int main() {
// A closed Promise cannot have then() or except() called.
// A Promise may be closed explicitly with the close() method:
Promise p0;
p0.close();
assert(p0.closed());
#if 0
// DON'T DO THIS - Cannot call then() or except() after a
// Promise is closed.
p0.then([]() { return nullptr; });
p0.except([]() { return nullptr; });
#endif
// A Promise may be closed implicitly by calling then() with an
// onResolve callback that takes an rvalue reference argument:
Promise p1;
p1.then([](Uncopyable&& s) {
Uncopyable local(std::move(s));
return nullptr;
});
assert(p1.closed());
#if 0
// DON'T DO THIS - Cannot call then() or except() after a
// Promise is closed.
p1.then([]() { return nullptr; });
p1.except([]() { return nullptr; });
#endif
// Rvalue references can be used to avoid copies, though copying is
// necessary in certain scenarios. A closed Promise may settle
// slightly faster because it can avoid locking a mutex.
Uncopyable foo;
p1.settle(std::move(foo));
return 0;
}