User Manual, Developers Guide and API Documentation

Enum.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 5, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #ifndef WNS_ENUM_HPP
00029 #define WNS_ENUM_HPP
00030 
00031 #include <WNS/TypeInfo.hpp>
00032 #include <WNS/Assure.hpp>
00033 
00034 #include <iostream>
00035 #include <string>
00036 #include <map>
00037 #include <stdint.h>
00038 
00039 // Start your enum section with this macro
00040 #define ENUM_BEGIN(NAMESPACE) namespace NAMESPACE {\
00041 template <int T, typename dummy> class EnumItem {};\
00042 class _Enums {\
00043 public:\
00044     typedef std::map<std::string, int> S2I;\
00045     typedef std::map<int, std::string> I2S;\
00046 \
00047     static S2I& getS2I() {\
00048         static S2I s2i;\
00049         return s2i;\
00050     }\
00051 \
00052     static I2S& getI2S() {\
00053         static I2S i2s;\
00054         return i2s;\
00055     }\
00056 };\
00057 \
00058 struct EnumRegister : private _Enums {\
00059      EnumRegister(const std::string& s, int i) {\
00060          _Enums::getS2I()[s]=i;\
00061          _Enums::getI2S()[i]=s;\
00062      }\
00063 };\
00064 inline int fromString(const std::string& s) {\
00065         _Enums::S2I& s2i = _Enums::getS2I();\
00066         _Enums::S2I::iterator itr = s2i.find(s);\
00067     assure(itr != s2i.end(), "No Enum with this name (\""<<s<<"\")!");  \
00068         return itr->second;\
00069 }\
00070 inline int fromString(const char* c) {\
00071         return fromString(std::string(c));\
00072 }\
00073 \
00074 inline std::string toString(int i) {\
00075         _Enums::I2S& i2s = _Enums::getI2S();\
00076         _Enums::I2S::iterator itr = i2s.find(i);\
00077     assure(itr != i2s.end(), "No Enum with this integer value ("<<i<<")!"); \
00078         return itr->second;\
00079 } \
00080 class ThisClassDoesNotExist /* to allow semicolon */
00081 
00082 // End your enum section with this macro
00083 #define ENUM_END() } \
00084 class ThisClassDoesNotExist /* to allow semicolon */
00085 
00086 // Define your enums with this macro
00087 #define ENUM(NAME, NUMBER)\
00088 /* The dummy template parameter type allows definition of the static member in
00089    the header-file*/\
00090 template <typename dummy> class EnumItem<NUMBER, dummy> : public _Enums \
00091 {\
00092 public:\
00093     operator int() {return NUMBER;}\
00094 private:\
00095     static EnumRegister reg;\
00096 };\
00097 typedef EnumItem<NUMBER, wns::NullType> NAME;\
00098 /* explicit template instantiation to call the EnumRegister c'tor*/\
00099 template <typename dummy> EnumRegister EnumItem<NUMBER, dummy>::reg = EnumRegister(#NAME, NUMBER);\
00100 template class EnumItem<NUMBER, wns::NullType>
00101 
00102 
00103 #endif //_ENUM_HPP
00104 

Generated on Wed May 23 03:31:34 2012 for openWNS by  doxygen 1.5.5