-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCriterion.h
125 lines (104 loc) · 2.61 KB
/
Criterion.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma once
#include"BaseLib/Policy/MinLimit.h"
#include"BaseLib/Policy/Reaction.h"
#include"BaseLib/Export.h"
namespace BaseLib { namespace Policy {
/**
* Usage:
* - Execution criterion for commands
* - Circuit breaker
*/
class DLL_STATE Criterion
{
public:
enum class DLL_STATE Kind : char
{
ALL = 'A',
MINIMUM = 'M',
UNCONDITIONAL = 'U'
};
public:
Criterion(MinLimit<int> limit, Reaction reaction, Kind kind, Interval withinInterval)
: limit_(limit)
, withinInterval_(withinInterval)
, reaction_(reaction)
, kind_(kind)
{ }
~Criterion()
{ }
// -----------------------------------------
// Getters
// -----------------------------------------
const MinLimit<int>& Limit() const
{
return limit_;
}
const Interval& InInterval() const
{
return withinInterval_;
}
const Reaction& GetReaction() const
{
return reaction_;
}
Kind GetKind() const
{
return kind_;
}
bool IsAll() const
{
return Kind::ALL == kind_;
}
bool IsMinimum() const
{
return Kind::MINIMUM == kind_;
}
bool IsUnconditional() const
{
return Kind::UNCONDITIONAL == kind_;
}
// --------------------------------------------
// Static constructors
// --------------------------------------------
static Criterion All()
{
return Criterion(
MinLimit<int>::LimitTo<int>(LimitKind::INCLUSIVE, std::numeric_limits<int>::max()),
Reaction::Stop(),
Kind::ALL,
Policy::Interval::Infinite()
);
}
static Criterion MinimumAnd(MinLimit<int> minLimit, Reaction reaction)
{
return Criterion(
minLimit,
reaction,
Kind::MINIMUM,
Policy::Interval::Infinite()
);
}
static Criterion Unconditional()
{
return Criterion(
MinLimit<int>::LimitTo<int>(LimitKind::INCLUSIVE, 0),
Reaction::Resume(),
Kind::UNCONDITIONAL,
Policy::Interval::Infinite()
);
}
// -----------------------------------------
// friend operators
// -----------------------------------------
friend std::ostream& operator<<(std::ostream& ostr, const Criterion& t)
{
ostr << "Criterion(" << t.limit_.Limit() << "," << t.reaction_ << "," << char(t.kind_) << ")";
return ostr;
}
private:
MinLimit<int> limit_;
Interval withinInterval_;
Reaction reaction_;
Kind kind_;
};
}}