00001 #ifndef _MERCURY_UTIL_H
00002 #define _MERCURY_UTIL_H
00003
00004 #include "global.h"
00005
00007 MString ConvertToCFormat( const MString & ncf );
00008
00010 MString ConvertToUnformatted( const MString & cf );
00011
00012 #if defined(WIN32)
00013 #define PROPERRETURN "\r\n"
00014 #else
00015 #define PROPERRETURN "\n"
00016 #endif
00017
00018 void Sleep(float msec);
00019
00020 #define CLASS_RTTI( CLASSNAME, PARENTCLASS ) \
00021 virtual MString GetType() { return #CLASSNAME; } \
00022 virtual void GetAllTypes( MVector<MString> & vOut ) { vOut.push_back( #CLASSNAME ); PARENTCLASS::GetAllTypes( vOut ); } \
00023 virtual bool IsTypeOf( const MString & sType ) { if( sType == #CLASSNAME ) return true; return PARENTCLASS::IsTypeOf( sType ); }
00024
00025
00026
00027
00028 inline int nextPow2(int x) { int num = 1; while(num <= x) num <<= 1; return num; }
00029 inline int makePow2(int x) { int num = 1; while(num < x) num <<= 1; return num; }
00030
00031
00032
00033
00035 long DumpFromFile( const MString & filename, char * & data );
00036
00038 bool DumpToFile( const MString & filename, const char * data, long bytes );
00039
00041 void FileToMString( const MString & filename, MString & data );
00042
00044 void MStringToFile( const MString & filename, const MString & data );
00045
00046
00047 bool FileExists( const MString & filename );
00048
00049
00050
00051
00052
00054 long BytesUntil( const char* strin, const char * termin, long start, long slen, long termlen );
00055
00057 long BytesNUntil( const char* strin, const char * termin, long start, long slen, long termlen );
00058
00060 template<typename T>
00061 inline T Clamp(const T& in, const T& min, const T& max) { if (in<min) return min; if ( in > max ) return max; return in; }
00062
00063
00064
00066 void mercury_crash( const char *reason = "Internal error" );
00067 #define ASSERT_M(COND, MESSAGE) { if(!(COND)) { mercury_crash(MESSAGE); } }
00068 #define ASSERT(COND) ASSERT_M((COND), "Assertion \"" + MString( #COND ) + "\" failed in: " + ssprintf( "%s:%d", __FILE__, __LINE__ ) )
00069 #define FAIL(MESSAGE) mercury_crash( MESSAGE )
00070
00071
00072 #include "MercuryVector.h"
00073
00075 void SplitStrings( const MString & in, MVector < MString > & out, const char * termin, const char * whitespace, long termlen, long wslen );
00076
00077 inline unsigned int Swap32( unsigned int n )
00078 {
00079 return (n >> 24) |
00080 ((n >> 8) & 0x0000FF00) |
00081 ((n << 8) & 0x00FF0000) |
00082 (n << 24);
00083 }
00084
00085 inline unsigned short Swap16( unsigned short n )
00086 {
00087 return (n >> 8) | (n << 8);
00088 }
00089
00090 template<typename T>
00091 void Destructor(T* t) { t->~T(); }
00092
00093
00094
00095
00096 template<typename T>
00097 class InstanceCounter
00098 {
00099 public:
00100 InstanceCounter()
00101 {
00102 if (m_count == 0)
00103 T::GetInstance();
00104 ++m_count;
00105 }
00106 ~InstanceCounter()
00107 {
00108 --m_count;
00109 if (m_count == 0)
00110 {
00111 T* i = &T::GetInstance();
00112 SAFE_DELETE(i);
00113 }
00114 }
00115 private:
00116 static unsigned int m_count;
00117 };
00118
00119 template<typename T>
00120 unsigned int InstanceCounter<T>::m_count = 0;
00121
00122 template<typename T>
00123 class MAutoPtr
00124 {
00125 public:
00126 MAutoPtr(T* ptr)
00127 {
00128 m_ptr = ptr;
00129 m_count = new unsigned int;
00130 *m_count = 1;
00131 }
00132
00133 MAutoPtr() : m_ptr(NULL), m_count(new unsigned int)
00134 {
00135 *m_count = 0;
00136 }
00137
00138 MAutoPtr(const MAutoPtr<T>& autoPtr)
00139 {
00140 m_ptr = autoPtr.m_ptr;
00141 m_count = autoPtr.m_count;
00142 ++(*m_count);
00143 }
00144
00145 inline ~MAutoPtr() { TryDelete(); }
00146
00147
00148 inline bool operator<(const MAutoPtr& rhs) const { return m_ptr < rhs.m_ptr; }
00149 inline bool operator>(const MAutoPtr& rhs) const { return m_ptr > rhs.m_ptr; }
00150 inline bool operator<=(const MAutoPtr& rhs) const { return m_ptr <= rhs.m_ptr; }
00151 inline bool operator>=(const MAutoPtr& rhs) const { return m_ptr >= rhs.m_ptr; }
00152 inline bool operator==(const MAutoPtr& rhs) const { return m_ptr == rhs.m_ptr; }
00153 inline bool operator!=(const MAutoPtr& rhs) const { return m_ptr != rhs.m_ptr; }
00154
00155
00156 MAutoPtr& operator=(const MAutoPtr& rhs)
00157 {
00158 TryDelete();
00159
00160 m_ptr = rhs.m_ptr;
00161 m_count = rhs.m_count;
00162 ++(*m_count);
00163
00164 return *this;
00165 }
00166
00167 inline const T& operator->*(MAutoPtr<T>& autoPtr) const { return *m_ptr; }
00168 inline T& operator->*(MAutoPtr<T>& autoPtr) { return *m_ptr; }
00169
00170 inline operator T*() { return m_ptr; }
00171 inline operator const T*() const { return m_ptr; }
00172
00173 private:
00174 void TryDelete()
00175 {
00176 if (*m_count > 0)
00177 --(*m_count);
00178 if (*m_count == 0)
00179 {
00180 SAFE_DELETE(m_ptr);
00181 SAFE_DELETE(m_count);
00182 }
00183 }
00184
00185 T* m_ptr;
00186 unsigned int* m_count;
00187 };
00188
00191 int GetAPrime( int ith );
00192
00193 #endif
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221