utils.h

Go to the documentation of this file.
00001 
00002 //            Copyright (C) 2004-2007 by The Allacrost Project
00003 //                         All Rights Reserved
00004 //
00005 // This code is licensed under the GNU GPL version 2. It is free software
00006 // and you may modify it and/or redistribute it under the terms of this license.
00007 // See http://www.gnu.org/copyleft/gpl.html for details.
00009 
00050 #ifndef __UTILS_HEADER__
00051 #define __UTILS_HEADER__
00052 
00053 #include <stdlib.h>
00054 #include <cstdlib>
00055 #include <cmath>
00056 #include <string.h> // For C string manipulation functions like strcmp
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   // Even though Allacrost is platform independent, OpenGL on Windows requires windows.h to be included
00070   #include <windows.h>
00071   // Case-insensitive string compare is called stricmp in Windows and strcasecmp everywhere else
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     // We assume that there is always a null terminating character, hence the -1 subtracted from the size
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 }; // class ustring
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 }; // template<typename T> class Singleton
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  * int GaussianValue(int mean, int range, bool positive_value):
00404  *
00405  *  This function computes a random number based on a Gaussian Distribution Curve. This number will be between
00406  *   mean - range and mean + range if range is greater than zero, otherwise it will return a true, unbounded
00407  *   guassian random value. If positive_value is set to true, this function will only return a number that is
00408  *   zero or positive.
00409  *
00410  *  Mean is (obviously) the mean, and the range represents the value for 3 standard deviations from the mean.
00411  *   That means that 99.7% of the random values chosen will lay between mean - range and mean + range, if
00412  *   range is a greater than or equal to zero.
00413  ******************************************************************************/
00420 int32 GaussianRandomValue(int32 mean, float std_dev = 10.0f, bool positive_value = true);
00421 
00422 /******************************************************************************
00423  * bool Probability(uint32 chance);
00424  *
00425  *  This function calculates a random number on a given chance and returns true if the chance occurs.
00426  *  For example, if calling Probability(25), there is a probability of 25% on returning `true`.
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 } // void InsertionSort(std::vector<T>& swap_vec)
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 } // namespace hoa_utils
00527 
00528 #endif // __UTILS_HEADER__

Generated on Fri Jul 6 23:11:26 2007 for Hero of Allacrost by  doxygen 1.5.1