enum_type.hpp

Go to the documentation of this file.
00001 /* $Id: enum_type.hpp 20286 2010-08-01 19:44:49Z frosch $ */
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 
00052 template <typename Tenum_t> struct EnumPropsT;
00053 
00065 template <typename Tenum_t, typename Tstorage_t, Tenum_t Tbegin, Tenum_t Tend, Tenum_t Tinvalid, uint Tnum_bits = 8 * sizeof(Tstorage_t)>
00066 struct MakeEnumPropsT {
00067   typedef Tenum_t type;                     
00068   typedef Tstorage_t storage;               
00069   static const Tenum_t begin = Tbegin;      
00070   static const Tenum_t end = Tend;          
00071   static const Tenum_t invalid = Tinvalid;  
00072   static const uint num_bits = Tnum_bits;   
00073 };
00074 
00075 
00076 
00086 template <typename Tenum_t> struct TinyEnumT;
00087 
00089 template <typename Tenum_t>
00090 struct TinyEnumT {
00091   typedef Tenum_t enum_type;                      
00092   typedef EnumPropsT<Tenum_t> Props;              
00093   typedef typename Props::storage storage_type;   
00094   static const enum_type begin = Props::begin;    
00095   static const enum_type end = Props::end;        
00096   static const enum_type invalid = Props::invalid;
00097 
00098   storage_type m_val;  
00099 
00101   FORCEINLINE operator enum_type () const
00102   {
00103     return (enum_type)m_val;
00104   }
00105 
00107   FORCEINLINE TinyEnumT& operator = (enum_type e)
00108   {
00109     m_val = (storage_type)e;
00110     return *this;
00111   }
00112 
00114   FORCEINLINE TinyEnumT& operator = (uint u)
00115   {
00116     m_val = (storage_type)u;
00117     return *this;
00118   }
00119 
00121   FORCEINLINE TinyEnumT operator ++ (int)
00122   {
00123     TinyEnumT org = *this;
00124     if (++m_val >= end) m_val -= (storage_type)(end - begin);
00125     return org;
00126   }
00127 
00129   FORCEINLINE TinyEnumT& operator ++ ()
00130   {
00131     if (++m_val >= end) m_val -= (storage_type)(end - begin);
00132     return *this;
00133   }
00134 };
00135 
00136 
00138 template <typename enum_type, typename storage_type>
00139 struct SimpleTinyEnumT {
00140   storage_type m_val;  
00141 
00143   FORCEINLINE operator enum_type () const
00144   {
00145     return (enum_type)this->m_val;
00146   }
00147 
00149   FORCEINLINE SimpleTinyEnumT &operator = (enum_type e)
00150   {
00151     this->m_val = (storage_type)e;
00152     return *this;
00153   }
00154 
00156   FORCEINLINE SimpleTinyEnumT &operator = (uint u)
00157   {
00158     this->m_val = (storage_type)u;
00159     return *this;
00160   }
00161 
00163   FORCEINLINE SimpleTinyEnumT &operator |= (enum_type e)
00164   {
00165     this->m_val = (storage_type)((enum_type)this->m_val | e);
00166     return *this;
00167   }
00168 
00170   FORCEINLINE SimpleTinyEnumT &operator &= (enum_type e)
00171   {
00172     this->m_val = (storage_type)((enum_type)this->m_val & e);
00173     return *this;
00174   }
00175 };
00176 
00177 #endif /* ENUM_TYPE_HPP */

Generated on Thu Jan 20 22:57:33 2011 for OpenTTD by  doxygen 1.6.1