MercuryUtil.h

Go to the documentation of this file.
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 /*  These two functions are very different. nextPow2 will go to the NEXT power of 2
00026     even if x is already a power of 2. makePow2 will make sure the number is a power
00027     of 2 at least equak to x. */
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 //Returns -1 if failure, otherwise # of bytes read.
00032 //Will initialize and malloc data.  Will ignore whatever was in it before.
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 //Return if file exists
00047 bool    FileExists( const MString & filename );
00048 
00049 //plain text processing,
00050 //strin = string to process, slen = length
00051 //termin = tokens to check, termlen = length
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 //Special crashing code
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 //I know this looks a little cookey but it's gotta get included here, otherwise it won't have access to what it needs.
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 //This counter is used with singletons to
00094 //ensure proper destruction order of the
00095 //singleton
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         //Comparative
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         //Assignment
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  * Copyright (c) 2005-2006, Charles Lohr, Joshua Allen
00197  * All rights reserved.
00198  *
00199  * Redistribution and use in source and binary forms, with or
00200  * without modification, are permitted provided that the following
00201  * conditions are met:
00202  *  -   Redistributions of source code must retain the above
00203  *      copyright notice, this list of conditions and the following disclaimer.
00204  *  -   Redistributions in binary form must reproduce the above copyright
00205  *      notice, this list of conditions and the following disclaimer in
00206  *      the documentation and/or other materials provided with the distribution.
00207  *  -   Neither the name of the Mercury Engine nor the names of its
00208  *      contributors may be used to endorse or promote products derived from
00209  *      this software without specific prior written permission.
00210  *
00211  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00212  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00213  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00214  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00215  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00216  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00217  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00218  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00219  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00220  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00221  */

Hosted by SourceForge.net Logo