-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxLimit.h
118 lines (96 loc) · 2.42 KB
/
MaxLimit.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
#pragma once
#include"BaseLib/Policy/CommonDefines.h"
#include"BaseLib/Templates/BaseTypes.h"
#include"BaseLib/Debug.h"
#include"BaseLib/Export.h"
namespace BaseLib { namespace Policy {
template <typename T = long>
class MaxLimit
: public Templates::ComparableImmutableType<T>
{
public:
// ----------------------------------
// Constructor
// ----------------------------------
/**
* User defined limit. Default is 0.
*/
MaxLimit(LimitKind::Type type, T maxLimit = 0)
: Templates::ComparableImmutableType<T>(maxLimit)
, type_(type)
{ }
/**
* User defined limit. Default is 0.
*/
MaxLimit(T maxLimit = 0)
: Templates::ComparableImmutableType<T>(maxLimit)
, type_(LimitKind::Type::INCLUSIVE)
{ }
virtual ~MaxLimit()
{ }
// ----------------------------------
// Getters and checkers
// ----------------------------------
T Limit() const
{
return this->Clone();
}
LimitKind::Type Type() const
{
return type_;
}
bool IsWithin(T limit) const
{
switch(type_)
{
case LimitKind::INCLUSIVE:
return this->Clone() >= limit;
case LimitKind::EXCLUSIVE:
return this->Clone() > limit;
default:
IWARNING() << "LimitKind not recognized";
break;
}
return this->Clone() >= limit;
}
bool IsInclusive() const
{
return type_ == LimitKind::INCLUSIVE;
}
bool IsExclusive() const
{
return type_ == LimitKind::EXCLUSIVE;
}
// ----------------------------------
// Static functions
// ----------------------------------
static MaxLimit Infinite()
{
return MaxLimit(LimitKind::INCLUSIVE, std::numeric_limits<T>::max());
}
static MaxLimit LimitTo(LimitKind::Type type, T maxLimit)
{
return MaxLimit<T>(type, maxLimit);
}
static MaxLimit InclusiveLimitTo(T maxLimit)
{
return MaxLimit(LimitKind::INCLUSIVE, maxLimit);
}
static MaxLimit InclusiveZero()
{
return MaxLimit(LimitKind::INCLUSIVE, 0);
}
private:
LimitKind::Type type_;
};
class DLL_STATE MaxLimitULong
: public MaxLimit<int64>
{
public:
MaxLimitULong(LimitKind::Type type, long maxLimit = 0)
: MaxLimit<int64>(type, maxLimit)
{ }
virtual ~MaxLimitULong()
{ }
};
}}