diff options
author | Subv <s.v.h21@hotmail.com> | 2012-10-04 19:39:09 -0500 |
---|---|---|
committer | Subv <s.v.h21@hotmail.com> | 2012-10-04 19:39:09 -0500 |
commit | f7d3600e7e7ffaba971b77e4371a705e55593cfd (patch) | |
tree | 2d4e898bb4e09ede5dba97918741597da6fd2d2e /src/server/shared/Utilities | |
parent | d2437407f4f7ad1af3baeb351626b41bd700065c (diff) | |
parent | 74707a08d3d63dc66e7b6943431c91f50258b0f9 (diff) |
Merge branch 'master' of github.com:TrinityCore/TrinityCore into mmaps
Conflicts:
src/server/game/Movement/MovementGenerators/FleeingMovementGenerator.cpp
Diffstat (limited to 'src/server/shared/Utilities')
-rwxr-xr-x | src/server/shared/Utilities/ByteConverter.h | 4 | ||||
-rwxr-xr-x | src/server/shared/Utilities/EventProcessor.h | 7 | ||||
-rwxr-xr-x | src/server/shared/Utilities/Util.cpp | 44 | ||||
-rwxr-xr-x | src/server/shared/Utilities/Util.h | 347 |
4 files changed, 143 insertions, 259 deletions
diff --git a/src/server/shared/Utilities/ByteConverter.h b/src/server/shared/Utilities/ByteConverter.h index d0790acadd3..05e8b8cc959 100755 --- a/src/server/shared/Utilities/ByteConverter.h +++ b/src/server/shared/Utilities/ByteConverter.h @@ -24,12 +24,12 @@ */ #include "Define.h" -#include<algorithm> +#include <algorithm> namespace ByteConverter { template<size_t T> - inline void convert(char *val) + inline void convert(char *val) { std::swap(*val, *(val + T - 1)); convert<T - 2>(val + 1); diff --git a/src/server/shared/Utilities/EventProcessor.h b/src/server/shared/Utilities/EventProcessor.h index 149ca9a4098..0f1a7c15216 100755 --- a/src/server/shared/Utilities/EventProcessor.h +++ b/src/server/shared/Utilities/EventProcessor.h @@ -21,7 +21,7 @@ #include "Define.h" -#include<map> +#include <map> // Note. All times are in milliseconds here. @@ -29,9 +29,7 @@ class BasicEvent { public: BasicEvent() { to_Abort = false; } - virtual ~BasicEvent() // override destructor to perform some actions on event removal - { - }; + virtual ~BasicEvent() {} // override destructor to perform some actions on event removal // this method executes when the event is triggered // return false if event does not want to be deleted @@ -68,4 +66,3 @@ class EventProcessor bool m_aborting; }; #endif - diff --git a/src/server/shared/Utilities/Util.cpp b/src/server/shared/Utilities/Util.cpp index 89942b978df..9778e86d444 100755 --- a/src/server/shared/Utilities/Util.cpp +++ b/src/server/shared/Utilities/Util.cpp @@ -16,8 +16,8 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include <iostream> #include "Util.h" +#include "Common.h" #include "utf8.h" #include "SFMT.h" #include <ace/TSS_T.h> @@ -56,13 +56,13 @@ double rand_chance(void) return sfmtRand->Random() * 100.0; } -Tokens::Tokens(const std::string &src, const char sep, uint32 vectorReserve) +Tokenizer::Tokenizer(const std::string &src, const char sep, uint32 vectorReserve) { m_str = new char[src.length() + 1]; memcpy(m_str, src.c_str(), src.length() + 1); if (vectorReserve) - reserve(vectorReserve); + m_storage.reserve(vectorReserve); char* posold = m_str; char* posnew = m_str; @@ -71,17 +71,17 @@ Tokens::Tokens(const std::string &src, const char sep, uint32 vectorReserve) { if (*posnew == sep) { - push_back(posold); + m_storage.push_back(posold); posold = posnew + 1; - *posnew = 0x00; + *posnew = '\0'; } - else if (*posnew == 0x00) + else if (*posnew == '\0') { // Hack like, but the old code accepted these kind of broken strings, // so changing it would break other things if (posold != posnew) - push_back(posold); + m_storage.push_back(posold); break; } @@ -471,32 +471,24 @@ void vutf8printf(FILE* out, const char *str, va_list* ap) #endif } -void hexEncodeByteArray(uint8* bytes, uint32 arrayLen, std::string& result) +std::string ByteArrayToHexStr(uint8 const* bytes, uint32 arrayLen, bool reverse /* = false */) { - std::ostringstream ss; - for (uint32 i=0; i<arrayLen; ++i) + int32 init = 0; + int32 end = arrayLen; + int8 op = 1; + + if (reverse) { - for (uint8 j=0; j<2; ++j) - { - unsigned char nibble = 0x0F & (bytes[i]>>((1-j)*4)); - char encodedNibble; - if (nibble < 0x0A) - encodedNibble = '0'+nibble; - else - encodedNibble = 'A'+nibble-0x0A; - ss << encodedNibble; - } + init = arrayLen - 1; + end = -1; + op = -1; } - result = ss.str(); -} -std::string ByteArrayToHexStr(uint8* bytes, uint32 length) -{ std::ostringstream ss; - for (uint32 i = 0; i < length; ++i) + for (int32 i = init; i != end; i += op) { char buffer[4]; - sprintf(buffer, "%02X ", bytes[i]); + sprintf(buffer, "%02X", bytes[i]); ss << buffer; } diff --git a/src/server/shared/Utilities/Util.h b/src/server/shared/Utilities/Util.h index 37782c31d8b..21aaa36498d 100755 --- a/src/server/shared/Utilities/Util.h +++ b/src/server/shared/Utilities/Util.h @@ -19,27 +19,49 @@ #ifndef _UTIL_H #define _UTIL_H -#include "Common.h" -#include "Containers.h" +#include "Define.h" + +#include <algorithm> #include <string> #include <vector> +#include <list> // Searcher for map of structs template<typename T, class S> struct Finder { T val_; T S::* idMember_; - + Finder(T val, T S::* idMember) : val_(val), idMember_(idMember) {} bool operator()(const std::pair<int, S> &obj) { return obj.second.*idMember_ == val_; } }; -struct Tokens: public std::vector<char*> +class Tokenizer { - Tokens(const std::string &src, const char sep, uint32 vectorReserve = 0); - ~Tokens() { delete[] m_str; } +public: + typedef std::vector<char const *> StorageType; + + typedef StorageType::size_type size_type; + + typedef StorageType::const_iterator const_iterator; + typedef StorageType::reference reference; + typedef StorageType::const_reference const_reference; + +public: + Tokenizer(const std::string &src, char const sep, uint32 vectorReserve = 0); + ~Tokenizer() { delete[] m_str; } + + const_iterator begin() const { return m_storage.begin(); } + const_iterator end() const { return m_storage.end(); } + + size_type size() const { return m_storage.size(); } + + reference operator [] (size_type i) { return m_storage[i]; } + const_reference operator [] (size_type i) const { return m_storage[i]; } +private: char* m_str; + StorageType m_storage; }; void stripLineInvisibleChars(std::string &src); @@ -49,27 +71,29 @@ uint32 TimeStringToSecs(const std::string& timestring); std::string TimeToTimestampStr(time_t t); /* Return a random number in the range min..max; (max-min) must be smaller than 32768. */ - int32 irand(int32 min, int32 max); +int32 irand(int32 min, int32 max); /* Return a random number in the range min..max (inclusive). For reliable results, the difference * between max and min should be less than RAND32_MAX. */ - uint32 urand(uint32 min, uint32 max); +uint32 urand(uint32 min, uint32 max); /* Return a random number in the range 0 .. RAND32_MAX. */ - int32 rand32(); +int32 rand32(); - /* Return a random number in the range min..max */ - float frand(float min, float max); +/* Return a random number in the range min..max */ +float frand(float min, float max); /* Return a random double from 0.0 to 1.0 (exclusive). Floats support only 7 valid decimal digits. * A double supports up to 15 valid decimal digits and is used internally (RAND32_MAX has 10 digits). - * With an FPU, there is usually no difference in performance between float and double. */ - double rand_norm(void); + * With an FPU, there is usually no difference in performance between float and double. +*/ +double rand_norm(void); /* Return a random double from 0.0 to 99.9999999999999. Floats support only 7 valid decimal digits. * A double supports up to 15 valid decimal digits and is used internally (RAND32_MAX has 10 digits). - * With an FPU, there is usually no difference in performance between float and double. */ - double rand_chance(void); + * With an FPU, there is usually no difference in performance between float and double. +*/ +double rand_chance(void); /* Return true if a random roll fits in the specified chance (range 0-100). */ inline bool roll_chance_f(float chance) @@ -91,58 +115,22 @@ inline void ApplyPercentModFloatVar(float& var, float val, bool apply) } // 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) +template <class T, class U> +inline T CalculatePct(T base, U pct) { - return T(base * float(pct) / 100.0f); + return T(base * static_cast<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) +template <class T, class U> +inline T AddPct(T &base, U pct) { - return base = CalculatePctN(base, pct); + return base += CalculatePct(base, pct); } -template <class T> -inline T ApplyPctU(T& base, uint32 pct) +template <class T, class U> +inline T ApplyPct(T &base, U pct) { - return base = CalculatePctU(base, pct); + return base = CalculatePct(base, pct); } template <class T> @@ -355,8 +343,7 @@ void vutf8printf(FILE* out, const char *str, va_list* ap); bool IsIPAddress(char const* ipaddress); uint32 CreatePIDFile(const std::string& filename); -void hexEncodeByteArray(uint8* bytes, uint32 arrayLen, std::string& result); -std::string ByteArrayToHexStr(uint8* bytes, uint32 length); +std::string ByteArrayToHexStr(uint8 const* bytes, uint32 length, bool reverse = false); #endif //handler for operations on large flags @@ -404,232 +391,140 @@ class flag96 { private: uint32 part[3]; + public: - flag96(uint32 p1=0, uint32 p2=0, uint32 p3=0) + flag96(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0) { - part[0]=p1; - part[1]=p2; - part[2]=p3; + part[0] = p1; + part[1] = p2; + part[2] = p3; } flag96(uint64 p1, uint32 p2) { - part[0]=PAIR64_LOPART(p1); - part[1]=PAIR64_HIPART(p1); - part[2]=p2; + part[0] = PAIR64_LOPART(p1); + part[1] = PAIR64_HIPART(p1); + part[2] = p2; } - inline bool IsEqual(uint32 p1=0, uint32 p2=0, uint32 p3=0) const + inline bool IsEqual(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0) const { - return ( - part[0]==p1 && - part[1]==p2 && - part[2]==p3); - }; - - inline bool HasFlag(uint32 p1=0, uint32 p2=0, uint32 p3=0) const - { - return ( - part[0]&p1 || - part[1]&p2 || - part[2]&p3); - }; + return (part[0] == p1 && part[1] == p2 && part[2] == p3); + } - inline void Set(uint32 p1=0, uint32 p2=0, uint32 p3=0) + inline bool HasFlag(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0) const { - part[0]=p1; - part[1]=p2; - part[2]=p3; - }; + return (part[0] & p1 || part[1] & p2 || part[2] & p3); + } - template<class type> - inline bool operator < (type & right) + inline void Set(uint32 p1 = 0, uint32 p2 = 0, uint32 p3 = 0) { - for (uint8 i=3; i > 0; --i) - { - if (part[i-1]<right.part[i-1]) - return 1; - else if (part[i-1]>right.part[i-1]) - return 0; - } - return 0; + part[0] = p1; + part[1] = p2; + part[2] = p3; } - template<class type> - inline bool operator < (type & right) const + inline bool operator <(const flag96 &right) const { for (uint8 i = 3; i > 0; --i) { - if (part[i-1]<right.part[i-1]) - return 1; - else if (part[i-1]>right.part[i-1]) - return 0; - } - return 0; - } - - template<class type> - inline bool operator != (type & right) - { - if (part[0]!=right.part[0] - || part[1]!=right.part[1] - || part[2]!=right.part[2]) + if (part[i - 1] < right.part[i - 1]) return true; + else if (part[i - 1] > right.part[i - 1]) + return false; + } return false; } - template<class type> - inline bool operator != (type & right) const + inline bool operator ==(const flag96 &right) const { - if (part[0]!=right.part[0] - || part[1]!=right.part[1] - || part[2]!=right.part[2]) - return true; - return false; + return + ( + part[0] == right.part[0] && + part[1] == right.part[1] && + part[2] == right.part[2] + ); } - template<class type> - inline bool operator == (type & right) + inline bool operator !=(const flag96 &right) const { - if (part[0]!=right.part[0] - || part[1]!=right.part[1] - || part[2]!=right.part[2]) - return false; - return true; + return !this->operator ==(right); } - template<class type> - inline bool operator == (type & right) const + inline flag96 & operator =(const flag96 &right) { - if (part[0]!=right.part[0] - || part[1]!=right.part[1] - || part[2]!=right.part[2]) - return false; - return true; + part[0] = right.part[0]; + part[1] = right.part[1]; + part[2] = right.part[2]; + return *this; } - template<class type> - inline void operator = (type & right) + inline flag96 operator &(const flag96 &right) const { - part[0]=right.part[0]; - part[1]=right.part[1]; - part[2]=right.part[2]; + return flag96(part[0] & right.part[0], part[1] & right.part[1], + part[2] & right.part[2]); } - template<class type> - inline flag96 operator & (type & right) + inline flag96 & operator &=(const flag96 &right) { - flag96 ret(part[0] & right.part[0], part[1] & right.part[1], part[2] & right.part[2]); - return - ret; + part[0] &= right.part[0]; + part[1] &= right.part[1]; + part[2] &= right.part[2]; + return *this; } - template<class type> - inline flag96 operator & (type & right) const + inline flag96 operator |(const flag96 &right) const { - flag96 ret(part[0] & right.part[0], part[1] & right.part[1], part[2] & right.part[2]); - return - ret; + return flag96(part[0] | right.part[0], part[1] | right.part[1], + part[2] | right.part[2]); } - template<class type> - inline void operator &= (type & right) + inline flag96 & operator |=(const flag96 &right) { - *this=*this & right; + part[0] |= right.part[0]; + part[1] |= right.part[1]; + part[2] |= right.part[2]; + return *this; } - template<class type> - inline flag96 operator | (type & right) + inline flag96 operator ~() const { - flag96 ret(part[0] | right.part[0], part[1] | right.part[1], part[2] | right.part[2]); - return - ret; + return flag96(~part[0], ~part[1], ~part[2]); } - template<class type> - inline flag96 operator | (type & right) const + inline flag96 operator ^(const flag96 &right) const { - flag96 ret(part[0] | right.part[0], part[1] | right.part[1], part[2] | right.part[2]); - return - ret; + return flag96(part[0] ^ right.part[0], part[1] ^ right.part[1], + part[2] ^ right.part[2]); } - template<class type> - inline void operator |= (type & right) + inline flag96 & operator ^=(const flag96 &right) { - *this=*this | right; + part[0] ^= right.part[0]; + part[1] ^= right.part[1]; + part[2] ^= right.part[2]; + return *this; } - inline void operator ~ () - { - part[2]=~part[2]; - part[1]=~part[1]; - part[0]=~part[0]; - }; - - template<class type> - inline flag96 operator ^ (type & right) + inline operator bool() const { - flag96 ret(part[0] ^ right.part[0], part[1] ^ right.part[1], part[2] ^ right.part[2]); - return - ret; + return (part[0] != 0 || part[1] != 0 || part[2] != 0); } - template<class type> - inline flag96 operator ^ (type & right) const + inline bool operator !() const { - flag96 ret(part[0] ^ right.part[0], part[1] ^ right.part[1], part[2] ^ right.part[2]); - return - ret; + return !this->operator bool(); } - template<class type> - inline void operator ^= (type & right) + inline uint32 & operator [](uint8 el) { - *this=*this^right; + return part[el]; } - inline operator bool() const - { - return( - part[0] != 0 || - part[1] != 0 || - part[2] != 0); - }; - - inline operator bool() + inline const uint32 & operator [](uint8 el) const { - return( - part[0] != 0 || - part[1] != 0 || - part[2] != 0); - }; - - inline bool operator ! () const - { - return( - part[0] == 0 && - part[1] == 0 && - part[2] == 0); - }; - - inline bool operator ! () - { - return( - part[0] == 0 && - part[1] == 0 && - part[2] == 0); - }; - - inline uint32 & operator[](uint8 el) - { - return (part[el]); - }; - - inline uint32 operator[](uint8 el) const - { - return (part[el]); - }; + return part[el]; + } }; #endif |