00001
00002
00003
00004
00005
00006
00007
00009
00050 #ifndef __UTILS_HEADER__
00051 #define __UTILS_HEADER__
00052
00053 #include <stdlib.h>
00054 #include <cstdlib>
00055 #include <cmath>
00056 #include <string.h>
00057
00058 #include <iostream>
00059 #include <string>
00060 #include <sstream>
00061 #include <vector>
00062 #include <list>
00063 #include <map>
00064 #include <stack>
00065 #include <stdexcept>
00066 #include <sstream>
00067
00068 #ifdef _WIN32
00069
00070 #include <windows.h>
00071
00072 #define strcasecmp stricmp
00073 #endif
00074
00075 #include <SDL/SDL.h>
00076
00085 typedef Sint32 int32;
00086 typedef Uint32 uint32;
00087 typedef Sint16 int16;
00088 typedef Uint16 uint16;
00089 typedef Sint8 int8;
00090 typedef Uint8 uint8;
00092
00094 namespace hoa_utils {
00095
00097 extern bool UTILS_DEBUG;
00098
00105 const float UTILS_QUARTER_PI = 0.785398163f;
00106 const float UTILS_HALF_PI = 1.570796326f;
00107 const float UTILS_PI = 3.141592653f;
00108 const float UTILS_2PI = 6.283185307f;
00110
00115 uint32 RoundUpPow2(uint32 x);
00116
00121 bool IsPowerOfTwo(uint32 x);
00122
00128 bool IsOddNumber(uint32 x);
00129
00141 bool IsFloatInRange(float value, float lower, float upper);
00142
00143
00145
00150 float FloorToFloatMultiple (const float value, const float multiple);
00151
00152
00154
00158 template<class T>
00159 void DataToString(std::string &s, const T &data)
00160 {
00161 std::ostringstream stream;
00162 stream << data;
00163 s = stream.str();
00164 }
00165
00166
00194 class ustring {
00195 public:
00196 ustring();
00197
00198 ustring(const uint16*);
00199
00200 static const size_t npos;
00201
00202 void clear()
00203 { _str.clear(); _str.push_back(0); }
00204
00205 bool empty() const
00206 { return _str.size() <= 1; }
00207
00208 size_t length() const
00209
00210 { return _str.size() - 1; }
00211
00212 size_t size() const
00213 { return length(); }
00214
00215 const uint16* c_str() const
00216 { return &_str[0]; }
00217
00218 size_t find(uint16 c, size_t pos = 0) const;
00219
00220 size_t find(const ustring &s, size_t pos = 0) const;
00221
00222 ustring substr(size_t pos = 0, size_t n = npos) const;
00223
00224 ustring & operator + (const ustring& s);
00225
00226 ustring & operator += (uint16 c);
00227
00228 ustring & operator += (const ustring& s);
00229
00230 ustring & operator = (const ustring& s);
00231
00232 uint16 & operator [] (size_t pos)
00233 { return _str[pos]; }
00234
00235 const uint16 & operator [] (size_t pos) const
00236 { return _str[pos]; }
00237
00238 private:
00240 std::vector<uint16> _str;
00241 };
00242
00243
00280 template<typename T> class Singleton {
00281 protected:
00283 static T* _singleton_reference;
00284
00285 Singleton()
00286 {}
00287
00288 virtual ~Singleton()
00289 {}
00290
00291 public:
00293 static T* SingletonCreate() {
00294 if (_singleton_reference == NULL) {
00295 _singleton_reference = new T();
00296 }
00297 else {
00298 if (UTILS_DEBUG)
00299 std::cerr << "UTILS WARNING: Singleton::SingletonCreate() was invoked when the class object was already instantiated" << std::endl;
00300 }
00301 return _singleton_reference;
00302 }
00303
00305 static void SingletonDestroy() {
00306 if (_singleton_reference != NULL) {
00307 delete _singleton_reference;
00308 }
00309 else {
00310 if (UTILS_DEBUG)
00311 std::cerr << "UTILS WARNING: Singleton::SingletonDestroy() was invoked when the class object was not instantiated" << std::endl;
00312 }
00313 _singleton_reference = NULL;
00314 }
00315
00317 static const T* SingletonGetReference()
00318 { return _singleton_reference; }
00319
00323 virtual bool SingletonInitialize() = 0;
00324
00325 private:
00326 Singleton(const Singleton &s);
00327 Singleton& operator=(const Singleton &s);
00328 };
00329
00330
00331
00333
00334
00338 template <typename T>
00339 std::string NumberToString(const T t)
00340 {
00341 std::ostringstream text("");
00342 text << static_cast<int32>(t);
00343 return text.str();
00344 }
00345
00354 bool IsStringNumeric(const std::string& text);
00355
00364 hoa_utils::ustring MakeUnicodeString(const std::string& text);
00365
00374 std::string MakeStandardString(const hoa_utils::ustring& text);
00376
00377
00382 template <typename T, size_t N>
00383 size_t NumberElementsArray(T (&)[N])
00384 { return N; }
00385
00387
00388
00391 float RandomFloat();
00392
00400 int32 RandomBoundedInteger(int32 lower_bound, int32 upper_bound);
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00420 int32 GaussianRandomValue(int32 mean, float std_dev = 10.0f, bool positive_value = true);
00421
00422
00423
00424
00425
00426
00427
00432 bool Probability(uint32 chance);
00434
00435
00437
00438
00455 template <typename T>
00456 void InsertionSort(std::vector<T>& swap_vec) {
00457 int32 i, j;
00458 T value;
00459 for (i = 1; i < swap_vec.size(); i++) {
00460 value = swap_vec[i];
00461 for (j = i - 1; j >= 0 && swap_vec[j] > value; j--) {
00462 swap_vec[j+1] = swap_vec[j];
00463 }
00464 swap_vec[j+1] = value;
00465 }
00466 }
00468
00470
00471
00475 bool DoesFileExist(const std::string& file_name);
00476
00487 bool MoveFile(const std::string& source_name, const std::string& destination_name);
00488
00493 bool CleanDirectory(const std::string& dir_name);
00494
00499 bool MakeDirectory(const std::string& dir_name);
00500
00505 bool RemoveDirectory(const std::string& dir_name);
00507
00509
00510
00516 bool IsLatestVersion ();
00517
00523 std::string GetLatestVersion ();
00525
00526 }
00527
00528 #endif // __UTILS_HEADER__