-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttempt.h
95 lines (76 loc) · 1.95 KB
/
Attempt.h
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once
#include"BaseLib/CommonDefines.h"
#include"BaseLib/Export.h"
namespace BaseLib { namespace Policy {
/**
* LifetimeKind
*/
namespace AttemptKind {
enum Type
{
UNTIL_SUCCESS,
NUM_SUCCESSFUL_TIMES,
FOREVER
};
}
/**
* Attempt: Policy on max number of times to complete some task.
*/
class DLL_STATE Attempt
{
public:
Attempt(AttemptKind::Type kind, int numSuccessfulTimes, int maxNumAttempts)
: kind_(kind)
, numSuccessfulTimes_(numSuccessfulTimes)
, maxNumAttempts_(maxNumAttempts)
{ }
virtual ~Attempt()
{ }
// -----------------------------------------
// Getters
// -----------------------------------------
AttemptKind::Type Kind() const
{
return kind_;
}
int MaxNumAttempts() const
{
return maxNumAttempts_;
}
int MinNumSuccesses() const
{
return numSuccessfulTimes_;
}
// -----------------------------------------
// Static constructors
// -----------------------------------------
static Attempt UntilSuccess(int maxNumAttempts)
{
return Attempt(AttemptKind::UNTIL_SUCCESS, 1, maxNumAttempts);
}
static Attempt NumSuccessfulTimes(int numSuccessfulTimes, int maxNumAttempts)
{
return Attempt(AttemptKind::NUM_SUCCESSFUL_TIMES, numSuccessfulTimes, maxNumAttempts);
}
static Attempt Forever()
{
return Attempt(AttemptKind::FOREVER, INT_MAX, INT_MAX);
}
static Attempt Default()
{
return UntilSuccess(5);
}
// -----------------------------------------
// various
// -----------------------------------------
friend std::ostream& operator<<(std::ostream& ostr, const Attempt& qos)
{
ostr << TYPE_NAME(qos) << "(" << qos.kind_ << "," << qos.numSuccessfulTimes_ << "," << qos.maxNumAttempts_ << ")";
return ostr;
}
private:
AttemptKind::Type kind_;
int numSuccessfulTimes_;
int maxNumAttempts_;
};
}}