GG
|
00001 // -*- C++ -*- 00002 /* GG is a GUI for SDL and OpenGL. 00003 Copyright (C) 2003-2008 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@gmail.com */ 00025 00030 #ifndef _GG_Enum_h_ 00031 #define _GG_Enum_h_ 00032 00033 #include <boost/config.hpp> 00034 00035 #include <map> 00036 #include <string> 00037 00038 00039 namespace GG { 00040 00042 struct EnumMapBase 00043 { 00044 BOOST_STATIC_CONSTANT(long int, BAD_VALUE = -5000000); 00045 00046 virtual ~EnumMapBase() {} 00047 00050 virtual const std::string& FromEnum(long int i) const = 0; 00051 00054 virtual long int FromString (const std::string& str) const = 0; 00055 }; 00056 00062 template <class E> struct EnumMap : EnumMapBase 00063 { 00064 virtual ~EnumMap() {} 00065 virtual const std::string& FromEnum(long int) const 00066 { static std::string empty; return empty; } 00067 virtual long int FromString (const std::string&) const {return 0;} 00068 }; 00069 00072 template <class E> EnumMap<E> GetEnumMap() 00073 { 00074 static EnumMap<E> enum_map; 00075 return enum_map; 00076 } 00077 00088 #define GG_ENUM_MAP_BEGIN( name ) \ 00089 template <> struct EnumMap< name > : EnumMapBase \ 00090 { \ 00091 typedef name EnumType; \ 00092 typedef std::map<EnumType, std::string> MapType; \ 00093 EnumMap () \ 00094 { 00095 00098 #define GG_ENUM_MAP_INSERT( value ) m_map[ value ] = #value ; 00099 00102 #define GG_ENUM_MAP_END \ 00103 } \ 00104 virtual const std::string& FromEnum(long int i) const \ 00105 { \ 00106 static const std::string ERROR_STR; \ 00107 std::map<EnumType, std::string>::const_iterator it = \ 00108 m_map.find(EnumType(i)); \ 00109 return it == m_map.end() ? ERROR_STR : it->second; \ 00110 } \ 00111 long int FromString (const std::string &str) const \ 00112 { \ 00113 for (MapType::const_iterator it = m_map.begin(); \ 00114 it != m_map.end(); \ 00115 ++it) { \ 00116 if (it->second == str) \ 00117 return it->first; \ 00118 } \ 00119 return BAD_VALUE; \ 00120 } \ 00121 MapType m_map; \ 00122 }; 00123 00126 #define GG_ENUM_STREAM_IN( name ) \ 00127 inline std::istream& operator>>(std::istream& is, name& v) \ 00128 { \ 00129 std::string str; \ 00130 is >> str; \ 00131 v = name (GG::GetEnumMap< name >().FromString(str)); \ 00132 return is; \ 00133 } 00134 00137 #define GG_ENUM_STREAM_OUT( name ) \ 00138 inline std::ostream& operator<<(std::ostream& os, name v) \ 00139 { \ 00140 os << GG::GetEnumMap< name >().FromEnum(v); \ 00141 return os; \ 00142 } 00143 00144 } // namespace GG 00145 00146 #endif // _GG_Enum_h_