Enum.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /* GG is a GUI for SDL and OpenGL.
00003    Copyright (C) 2003 T. Zachary Laine
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public License
00007    as published by the Free Software Foundation; either version 2.1
00008    of the License, or (at your option) any later version.
00009    
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014     
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA
00019 
00020    If you do not wish to comply with the terms of the LGPL please
00021    contact the author as other terms are available for a fee.
00022     
00023    Zach Laine
00024    whatwasthataddress@hotmail.com */
00025    
00030 #ifndef _GG_Enum_h_
00031 #define _GG_Enum_h_
00032 
00033 #include <map>
00034 #include <string>
00035 
00036 
00037 namespace GG {
00038 
00040 struct EnumMapBase
00041 {
00042     enum {BAD_VALUE = -5000000};
00043 
00044     virtual ~EnumMapBase() {} 
00045 
00048     virtual const std::string& FromEnum(int i) const = 0;
00049 
00052     virtual int FromString (const std::string& str) const = 0;
00053 };
00054 
00057 template <class E> struct EnumMap : EnumMapBase
00058 {
00059     virtual ~EnumMap() {} 
00060     virtual const std::string& FromEnum(int) const
00061     { static std::string empty; return empty; }
00062     virtual int FromString (const std::string&) const {return 0;}
00063 };
00064 
00066 template <class E> EnumMap<E> GetEnumMap()
00067 {
00068     static EnumMap<E> enum_map;
00069     return enum_map;
00070 }
00071 
00081 #define GG_ENUM_MAP_BEGIN( name )                                       \
00082 template <> struct EnumMap< name > : EnumMapBase                        \
00083 {                                                                       \
00084     typedef name EnumType;                                              \
00085     typedef std::map<EnumType, std::string> MapType;                    \
00086     EnumMap ()                                                          \
00087     {
00088 
00090 #define GG_ENUM_MAP_INSERT( value ) m_map[ value ] = #value ;
00091 
00093 #define GG_ENUM_MAP_END                                                 \
00094     }                                                                   \
00095     virtual const std::string& FromEnum(int i) const                    \
00096     {                                                                   \
00097         static const std::string ERROR_STR;                             \
00098         std::map<EnumType, std::string>::const_iterator it =            \
00099             m_map.find(EnumType(i));                                    \
00100         return it == m_map.end() ? ERROR_STR : it->second;              \
00101     }                                                                   \
00102     int FromString (const std::string &str) const                       \
00103     {                                                                   \
00104         for (MapType::const_iterator it = m_map.begin();                \
00105              it != m_map.end();                                         \
00106              ++it) {                                                    \
00107             if (it->second == str)                                      \
00108                 return it->first;                                       \
00109         }                                                               \
00110         return BAD_VALUE;                                               \
00111     }                                                                   \
00112     MapType m_map;                                                      \
00113 };
00114 
00116 #define GG_ENUM_STREAM_IN( name )                                       \
00117     inline std::istream& operator>>(std::istream& is, name& v)          \
00118     {                                                                   \
00119         std::string str;                                                \
00120         is >> str;                                                      \
00121         v = name (GG::GetEnumMap< name >().FromString(str));            \
00122         return is;                                                      \
00123     }
00124 
00126 #define GG_ENUM_STREAM_OUT( name )                                      \
00127     inline std::ostream& operator<<(std::ostream& os, name v)           \
00128     {                                                                   \
00129         os << GG::GetEnumMap< name >().FromEnum(v);                     \
00130         return os;                                                      \
00131     }
00132 
00133 } // namespace GG
00134 
00135 #endif // _GG_Enum_h_

Generated on Wed Mar 26 14:35:41 2008 for GG by  doxygen 1.5.2