-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpercent.h
209 lines (160 loc) · 3.55 KB
/
percent.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#ifndef _GMTK_PERCENT_H_
#define _GMTK_PERCENT_H_
//
#include "util.h"
//
namespace GMTK_NAMESPACE
{////
template<typename T = float>
struct perc
{////
///////////////////
//! DATA MEMBERS //
///////////////////
enum PercentUnits
{
Value = 0,
Percent = 1
};
///////////////////
//! CONSTRUCTORS //
///////////////////
//! Default constructor
inline perc()
{
_val = static_cast<T>(0);
}
//! Copy constructor
inline perc(const perc<T> &a)
{
_val = a._val;
}
//! Type conversion copy constructor
template<typename U>
inline perc(const perc<U> &a)
{
_val = static_cast<T>(a._val);
}
//! Selectable constructor
inline perc(T amount, PercentUnits units)
{
switch (units)
{
case Value:
_val = amount;
break;
case Percent:
_val = amount * static_cast<T>(0.01f);
break;
}
};
///////////////////////////
//! RIGHT-HAND OPERATORS //
///////////////////////////
//! Unary negative percentage
inline perc<T> operator-() {
return perc<T>(-_val);
}
//! Percent addition
inline perc<T> operator+(const perc<T> &a) {
return perc<T>(_val + a._val);
}
//! Percent subtraction
inline perc<T> operator-(const perc<T> &a) {
return perc<T>(_val - a._val);
}
//! Percent multiplication
inline perc<T> operator*(const T &s) {
return perc<T>(_val * s);
}
//! Percent division
inline perc<T> operator/(const T &s) {
return perc<T>(_val - s);
}
//! Percent reference addition
inline perc<T> &operator+=(const perc<T> &a) {
_val += a._val;
return *this;
}
//! Percent reference subtraction
inline perc<T> &operator-=(const perc<T> &a) {
_val -= a._val;
return *this;
}
//! Percent reference multiplication
inline perc<T> &operator*=(const T &s) {
_val *= s;
return *this;
}
//! Percent reference division
inline perc<T> &operator/=(const T &s) {
_val /= s;
return *this;
}
///////////////////////
//! ACCESS FUNCTIONS //
///////////////////////
inline T percent() const
{
return _val * static_cast<T>(100.0f);
}
inline T value() const
{
return _val;
}
private:
//! Private inline constructor.
inline perc(const T &v) : _val(v) { }
T _val = 0;
};////
//////////////////////////
//! GENERATOR FUNCTIONS //
//////////////////////////
//! Creates an percentage from a value (0..1)
inline perc<> value(float val)
{
return perc<float>(val, perc<float>::Value);
}
//! Creates an percentage from a percentage (0..100)
inline perc<> percent(float per)
{
return perc<float>(per, perc<float>::Percent);
}
///////////////////////
//! TYPE DEFINITIONS //
///////////////////////
typedef perc<float> percf;
typedef perc<double> percd;
typedef perc<int> perci;
//////////////////////
//! MISC. OPERATORS //
//////////////////////
inline std::ostream& operator<<(std::ostream& os, const perc<> &a)
{
os << a.percent() << "%";
return os;
}
}////
///////////////
//! LITERALS //
///////////////
#ifndef GMTK_DISABLE_LITERALS
inline GMTK_NAMESPACE::perc<> operator "" _per(unsigned long long value)
{
return GMTK_NAMESPACE::percent(static_cast<float>(value));
}
inline GMTK_NAMESPACE::perc<> operator "" _per(long double value)
{
return GMTK_NAMESPACE::percent(static_cast<float>(value));
}
inline GMTK_NAMESPACE::perc<> operator "" _val(unsigned long long value)
{
return GMTK_NAMESPACE::value(static_cast<float>(value));
}
inline GMTK_NAMESPACE::perc<> operator "" _val(long double value)
{
return GMTK_NAMESPACE::value(static_cast<float>(value));
}
#endif
//
#endif