aboutsummaryrefslogtreecommitdiff
path: root/src/server/shared/Utilities/Util.h
diff options
context:
space:
mode:
authorazazel <none@none>2010-12-11 20:37:38 +0600
committerazazel <none@none>2010-12-11 20:37:38 +0600
commitd2d62eab50fc03bdba66676e508354521e385631 (patch)
treef4278268481ec42649142a4027aaacae0ee5ee9e /src/server/shared/Utilities/Util.h
parent81db111ffd70338de973fa8b1969367d8ca9624e (diff)
Cleanup: implemented helper methods for manipulating percentage calculation and used it where appropriate (plus fixed some other warnings).
NOTE: Initially I just wanted to fix some warnings, but noticed that there is no common method for percentage calculation and various formulas are used many time in the code making it difficult to read and understand what the code actually does. So, I introduced several template methods for calculating percent values and adding those values to the original base. I replaced all the raw calculations throughout the code where found, but I could have missed something or could have made a mistake. So, please report any strange behaviour after this commit. If you ask me why I did it: for the sake of consistency and exact understanding what code means. If you see CalculatePct method, you clearly understand, that it find the value of x percent of y. And you can easily express, for example, spell behviour "reduces smth by x%" by the means of a method instead of recalling school maths. --HG-- branch : trunk
Diffstat (limited to 'src/server/shared/Utilities/Util.h')
-rwxr-xr-xsrc/server/shared/Utilities/Util.h62
1 files changed, 59 insertions, 3 deletions
diff --git a/src/server/shared/Utilities/Util.h b/src/server/shared/Utilities/Util.h
index 8a49d7d9681..f6b408f84c1 100755
--- a/src/server/shared/Utilities/Util.h
+++ b/src/server/shared/Utilities/Util.h
@@ -80,7 +80,7 @@ inline void ApplyModUInt32Var(uint32& var, int32 val, bool apply)
{
int32 cur = var;
cur += (apply ? val : -val);
- if(cur < 0)
+ if (cur < 0)
cur = 0;
var = cur;
}
@@ -88,7 +88,7 @@ inline void ApplyModUInt32Var(uint32& var, int32 val, bool apply)
inline void ApplyModFloatVar(float& var, float val, bool apply)
{
var += (apply ? val : -val);
- if(var < 0)
+ if (var < 0)
var = 0;
}
@@ -96,9 +96,65 @@ inline void ApplyPercentModFloatVar(float& var, float val, bool apply)
{
if (val == -100.0f) // prevent set var to zero
val = -99.99f;
- var *= (apply?(100.0f+val)/100.0f : 100.0f / (100.0f+val));
+ var *= (apply ? (100.0f + val) / 100.0f : 100.0f / (100.0f + val));
}
+// Percentage calculation
+template <class T>
+inline T CalculatePctF(T base, float pct)
+{
+ return T(base * pct / 100.0f);
+}
+
+template <class T>
+inline T CalculatePctN(T base, int32 pct)
+{
+ return T(base * float(pct) / 100.0f);
+}
+
+template <class T>
+inline T CalculatePctU(T base, uint32 pct)
+{
+ return T(base * float(pct) / 100.0f);
+}
+
+template <class T>
+inline T AddPctF(T& base, float pct)
+{
+ return base += CalculatePctF(base, pct);
+}
+
+template <class T>
+inline T AddPctN(T& base, int32 pct)
+{
+ return base += CalculatePctN(base, pct);
+}
+
+template <class T>
+inline T AddPctU(T& base, uint32 pct)
+{
+ return base += CalculatePctU(base, pct);
+}
+
+template <class T>
+inline T ApplyPctF(T& base, float pct)
+{
+ return base = CalculatePctF(base, pct);
+}
+
+template <class T>
+inline T ApplyPctN(T& base, int32 pct)
+{
+ return base = CalculatePctN(base, pct);
+}
+
+template <class T>
+inline T ApplyPctU(T& base, uint32 pct)
+{
+ return base = CalculatePctU(base, pct);
+}
+
+// UTF8 handling
bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr);
// in wsize==max size of buffer, out wsize==real string size
bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize);