enum_type.hpp

Go to the documentation of this file.
00001 /* $Id: enum_type.hpp 13731 2008-07-18 16:40:29Z rubidium $ */
00002 
00005 #ifndef ENUM_TYPE_HPP
00006 #define ENUM_TYPE_HPP
00007 
00009 #define DECLARE_POSTFIX_INCREMENT(type) \
00010   FORCEINLINE type operator ++(type& e, int) \
00011   { \
00012     type e_org = e; \
00013     e = (type)((int)e + 1); \
00014     return e_org; \
00015   } \
00016   FORCEINLINE type operator --(type& e, int) \
00017   { \
00018     type e_org = e; \
00019     e = (type)((int)e - 1); \
00020     return e_org; \
00021   }
00022 
00023 
00024 
00026 # define DECLARE_ENUM_AS_BIT_SET(mask_t) \
00027   FORCEINLINE mask_t operator | (mask_t m1, mask_t m2) {return (mask_t)((int)m1 | m2);} \
00028   FORCEINLINE mask_t operator & (mask_t m1, mask_t m2) {return (mask_t)((int)m1 & m2);} \
00029   FORCEINLINE mask_t operator ^ (mask_t m1, mask_t m2) {return (mask_t)((int)m1 ^ m2);} \
00030   FORCEINLINE mask_t& operator |= (mask_t& m1, mask_t m2) {m1 = m1 | m2; return m1;} \
00031   FORCEINLINE mask_t& operator &= (mask_t& m1, mask_t m2) {m1 = m1 & m2; return m1;} \
00032   FORCEINLINE mask_t& operator ^= (mask_t& m1, mask_t m2) {m1 = m1 ^ m2; return m1;} \
00033   FORCEINLINE mask_t operator ~(mask_t m) {return (mask_t)(~(int)m);}
00034 
00035 
00044 template <typename Tenum_t> struct EnumPropsT;
00045 
00055 template <typename Tenum_t, typename Tstorage_t, Tenum_t Tbegin, Tenum_t Tend, Tenum_t Tinvalid>
00056 struct MakeEnumPropsT {
00057   typedef Tenum_t type;                     
00058   typedef Tstorage_t storage;               
00059   static const Tenum_t begin = Tbegin;      
00060   static const Tenum_t end = Tend;          
00061   static const Tenum_t invalid = Tinvalid;  
00062 };
00063 
00064 
00065 
00073 template <typename Tenum_t> struct TinyEnumT;
00074 
00076 template <typename Tenum_t>
00077 struct TinyEnumT
00078 {
00079   typedef Tenum_t enum_type;                      
00080   typedef EnumPropsT<Tenum_t> Props;              
00081   typedef typename Props::storage storage_type;   
00082   static const enum_type begin = Props::begin;    
00083   static const enum_type end = Props::end;        
00084   static const enum_type invalid = Props::invalid;
00085 
00086   storage_type m_val;  
00087 
00089   FORCEINLINE operator enum_type () const
00090   {
00091     return (enum_type)m_val;
00092   }
00093 
00095   FORCEINLINE TinyEnumT& operator = (enum_type e)
00096   {
00097     m_val = (storage_type)e;
00098     return *this;
00099   }
00100 
00102   FORCEINLINE TinyEnumT& operator = (uint u)
00103   {
00104     m_val = (storage_type)u;
00105     return *this;
00106   }
00107 
00109   FORCEINLINE TinyEnumT operator ++ (int)
00110   {
00111     TinyEnumT org = *this;
00112     if (++m_val >= end) m_val -= (storage_type)(end - begin);
00113     return org;
00114   }
00115 
00117   FORCEINLINE TinyEnumT& operator ++ ()
00118   {
00119     if (++m_val >= end) m_val -= (storage_type)(end - begin);
00120     return *this;
00121   }
00122 };
00123 
00124 #endif /* HELPERS_HPP */

Generated on Mon Jan 5 19:01:36 2009 for openttd by  doxygen 1.5.6