enum_type.hpp

Go to the documentation of this file.
00001 /* $Id: enum_type.hpp 17248 2009-08-21 20:21:05Z 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 
00062 template <typename Tenum_t, typename Tstorage_t, Tenum_t Tbegin, Tenum_t Tend, Tenum_t Tinvalid>
00063 struct MakeEnumPropsT {
00064   typedef Tenum_t type;                     
00065   typedef Tstorage_t storage;               
00066   static const Tenum_t begin = Tbegin;      
00067   static const Tenum_t end = Tend;          
00068   static const Tenum_t invalid = Tinvalid;  
00069 };
00070 
00071 
00072 
00080 template <typename Tenum_t> struct TinyEnumT;
00081 
00083 template <typename Tenum_t>
00084 struct TinyEnumT {
00085   typedef Tenum_t enum_type;                      
00086   typedef EnumPropsT<Tenum_t> Props;              
00087   typedef typename Props::storage storage_type;   
00088   static const enum_type begin = Props::begin;    
00089   static const enum_type end = Props::end;        
00090   static const enum_type invalid = Props::invalid;
00091 
00092   storage_type m_val;  
00093 
00095   FORCEINLINE operator enum_type () const
00096   {
00097     return (enum_type)m_val;
00098   }
00099 
00101   FORCEINLINE TinyEnumT& operator = (enum_type e)
00102   {
00103     m_val = (storage_type)e;
00104     return *this;
00105   }
00106 
00108   FORCEINLINE TinyEnumT& operator = (uint u)
00109   {
00110     m_val = (storage_type)u;
00111     return *this;
00112   }
00113 
00115   FORCEINLINE TinyEnumT operator ++ (int)
00116   {
00117     TinyEnumT org = *this;
00118     if (++m_val >= end) m_val -= (storage_type)(end - begin);
00119     return org;
00120   }
00121 
00123   FORCEINLINE TinyEnumT& operator ++ ()
00124   {
00125     if (++m_val >= end) m_val -= (storage_type)(end - begin);
00126     return *this;
00127   }
00128 };
00129 
00130 
00132 template <typename enum_type, typename storage_type>
00133 struct SimpleTinyEnumT {
00134   storage_type m_val;  
00135 
00137   FORCEINLINE operator enum_type () const
00138   {
00139     return (enum_type)this->m_val;
00140   }
00141 
00143   FORCEINLINE SimpleTinyEnumT &operator = (enum_type e)
00144   {
00145     this->m_val = (storage_type)e;
00146     return *this;
00147   }
00148 
00150   FORCEINLINE SimpleTinyEnumT &operator = (uint u)
00151   {
00152     this->m_val = (storage_type)u;
00153     return *this;
00154   }
00155 
00157   FORCEINLINE SimpleTinyEnumT &operator |= (enum_type e)
00158   {
00159     this->m_val = (storage_type)((enum_type)this->m_val | e);
00160     return *this;
00161   }
00162 
00164   FORCEINLINE SimpleTinyEnumT &operator &= (enum_type e)
00165   {
00166     this->m_val = (storage_type)((enum_type)this->m_val & e);
00167     return *this;
00168   }
00169 };
00170 
00171 #endif /* ENUM_TYPE_HPP */

Generated on Tue Jan 5 21:02:53 2010 for OpenTTD by  doxygen 1.5.6