00001 #ifndef EnumHelper_H 00002 #define EnumHelper_H 00003 00004 /* 00005 * Safely add an integer to an enum. 00006 * 00007 * This is illegal: 00008 * 00009 * ((int&)val) += iAmt; 00010 * 00011 * It breaks aliasing rules; the compiler is allowed to assume that "val" doesn't 00012 * change (unless it's declared volatile), and in some cases, you'll end up getting 00013 * old values for "val" following the add. (What's probably really happening is 00014 * that the memory location is being added to, but the value is stored in a register, 00015 * and breaking aliasing rules means the compiler doesn't know that the register 00016 * value is invalid.) 00017 * 00018 * Always do these conversions through a union. 00019 */ 00020 template<typename T> 00021 static inline void enum_add( T &val, int iAmt ) 00022 { 00023 union conv 00024 { 00025 T value; 00026 int i; 00027 }; 00028 00029 conv c; 00030 c.value = val; 00031 c.i += iAmt; 00032 val = c.value; 00033 } 00034 00035 #define FOREACH_ENUM( e, max, var ) for( e var=(e)0; var<max; enum_add<e>( var, +1 ) ) 00036 00037 00038 static const CString EMPTY_STRING; 00039 00040 #define XToString(X) \ 00041 const CString& X##ToString( X x ) \ 00042 { \ 00043 if( x == ARRAYSIZE(X##Names)+1 ) \ 00044 return EMPTY_STRING; \ 00045 ASSERT(unsigned(x) < ARRAYSIZE(X##Names)); \ 00046 return X##Names[x]; \ 00047 } 00048 00049 #define XToThemedString(X) \ 00050 CString X##ToThemedString( X x ) \ 00051 { \ 00052 return THEME->GetMetric( #X, X##ToString(x) ); \ 00053 } 00054 00055 #define StringToX(X) \ 00056 X StringTo##X( const CString& s ) \ 00057 { \ 00058 CString s2 = s; \ 00059 s2.MakeLower(); \ 00060 unsigned i; \ 00061 for( i = 0; i < ARRAYSIZE(X##Names); ++i ) \ 00062 if( !s2.CompareNoCase(X##Names[i]) ) \ 00063 return (X)i; \ 00064 return (X)(i+1); /*invalid*/ \ 00065 } 00066 00067 #endif 00068 00069 /* 00070 * (c) 2004 Chris Danford 00071 * All rights reserved. 00072 * 00073 * Permission is hereby granted, free of charge, to any person obtaining a 00074 * copy of this software and associated documentation files (the 00075 * "Software"), to deal in the Software without restriction, including 00076 * without limitation the rights to use, copy, modify, merge, publish, 00077 * distribute, and/or sell copies of the Software, and to permit persons to 00078 * whom the Software is furnished to do so, provided that the above 00079 * copyright notice(s) and this permission notice appear in all copies of 00080 * the Software and that both the above copyright notice(s) and this 00081 * permission notice appear in supporting documentation. 00082 * 00083 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00084 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00085 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF 00086 * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS 00087 * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT 00088 * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 00089 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 00090 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00091 * PERFORMANCE OF THIS SOFTWARE. 00092 */