enum_type.hpp

Go to the documentation of this file.
00001 /* $Id: enum_type.hpp 19665 2010-04-17 22:27:49Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #ifndef ENUM_TYPE_HPP
00013 #define ENUM_TYPE_HPP
00014 
00016 #define DECLARE_POSTFIX_INCREMENT(type) \
00017   FORCEINLINE type operator ++(type& e, int) \
00018   { \
00019     type e_org = e; \
00020     e = (type)((int)e + 1); \
00021     return e_org; \
00022   } \
00023   FORCEINLINE type operator --(type& e, int) \
00024   { \
00025     type e_org = e; \
00026     e = (type)((int)e - 1); \
00027     return e_org; \
00028   }
00029 
00030 
00031 
00033 # define DECLARE_ENUM_AS_BIT_SET(mask_t) \
00034   FORCEINLINE mask_t operator | (mask_t m1, mask_t m2) {return (mask_t)((int)m1 | m2);} \
00035   FORCEINLINE mask_t operator & (mask_t m1, mask_t m2) {return (mask_t)((int)m1 & m2);} \
00036   FORCEINLINE mask_t operator ^ (mask_t m1, mask_t m2) {return (mask_t)((int)m1 ^ m2);} \
00037   FORCEINLINE mask_t& operator |= (mask_t& m1, mask_t m2) {m1 = m1 | m2; return m1;} \
00038   FORCEINLINE mask_t& operator &= (mask_t& m1, mask_t m2) {m1 = m1 & m2; return m1;} \
00039   FORCEINLINE mask_t& operator ^= (mask_t& m1, mask_t m2) {m1 = m1 ^ m2; return m1;} \
00040   FORCEINLINE mask_t operator ~(mask_t m) {return (mask_t)(~(int)m);}
00041 
00042 
00051 template <typename Tenum_t> struct EnumPropsT;
00052 
00063 template <typename Tenum_t, typename Tstorage_t, Tenum_t Tbegin, Tenum_t Tend, Tenum_t Tinvalid, uint Tnum_bits = 8 * sizeof(Tstorage_t)>
00064 struct MakeEnumPropsT {
00065   typedef Tenum_t type;                     
00066   typedef Tstorage_t storage;               
00067   static const Tenum_t begin = Tbegin;      
00068   static const Tenum_t end = Tend;          
00069   static const Tenum_t invalid = Tinvalid;  
00070   static const uint num_bits = Tnum_bits;   
00071 };
00072 
00073 
00074 
00082 template <typename Tenum_t> struct TinyEnumT;
00083 
00085 template <typename Tenum_t>
00086 struct TinyEnumT {
00087   typedef Tenum_t enum_type;                      
00088   typedef EnumPropsT<Tenum_t> Props;              
00089   typedef typename Props::storage storage_type;   
00090   static const enum_type begin = Props::begin;    
00091   static const enum_type end = Props::end;        
00092   static const enum_type invalid = Props::invalid;
00093 
00094   storage_type m_val;  
00095 
00097   FORCEINLINE operator enum_type () const
00098   {
00099     return (enum_type)m_val;
00100   }
00101 
00103   FORCEINLINE TinyEnumT& operator = (enum_type e)
00104   {
00105     m_val = (storage_type)e;
00106     return *this;
00107   }
00108 
00110   FORCEINLINE TinyEnumT& operator = (uint u)
00111   {
00112     m_val = (storage_type)u;
00113     return *this;
00114   }
00115 
00117   FORCEINLINE TinyEnumT operator ++ (int)
00118   {
00119     TinyEnumT org = *this;
00120     if (++m_val >= end) m_val -= (storage_type)(end - begin);
00121     return org;
00122   }
00123 
00125   FORCEINLINE TinyEnumT& operator ++ ()
00126   {
00127     if (++m_val >= end) m_val -= (storage_type)(end - begin);
00128     return *this;
00129   }
00130 };
00131 
00132 
00134 template <typename enum_type, typename storage_type>
00135 struct SimpleTinyEnumT {
00136   storage_type m_val;  
00137 
00139   FORCEINLINE operator enum_type () const
00140   {
00141     return (enum_type)this->m_val;
00142   }
00143 
00145   FORCEINLINE SimpleTinyEnumT &operator = (enum_type e)
00146   {
00147     this->m_val = (storage_type)e;
00148     return *this;
00149   }
00150 
00152   FORCEINLINE SimpleTinyEnumT &operator = (uint u)
00153   {
00154     this->m_val = (storage_type)u;
00155     return *this;
00156   }
00157 
00159   FORCEINLINE SimpleTinyEnumT &operator |= (enum_type e)
00160   {
00161     this->m_val = (storage_type)((enum_type)this->m_val | e);
00162     return *this;
00163   }
00164 
00166   FORCEINLINE SimpleTinyEnumT &operator &= (enum_type e)
00167   {
00168     this->m_val = (storage_type)((enum_type)this->m_val & e);
00169     return *this;
00170   }
00171 };
00172 
00173 #endif /* ENUM_TYPE_HPP */

Generated on Sat Nov 20 20:59:02 2010 for OpenTTD by  doxygen 1.6.1