iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EnumImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #ifndef INCLUDE_NRT_CORE_TYPING_DETAILS_ENUMIMPL_H
37 #define INCLUDE_NRT_CORE_TYPING_DETAILS_ENUMIMPL_H
38 
39 #include <boost/preprocessor/seq/enum.hpp>
40 #include <boost/preprocessor/seq/transform.hpp>
41 
42 // ######################################################################
43 inline nrt::exception::BadEnumStringException::
44 BadEnumStringException(char const * classname, char const * s) throw()
45  try : nrt::exception::Exception("Error BadEnumStringException, and further error while constructing the exception"),
46  whatstr(std::string("String [")+s+"] does not match any value name in enum class ["+classname+"]")
47 {
48  whatptr = whatstr.c_str();
49 } catch (...) { }
50 
51 inline nrt::exception::BadEnumStringException::
52 ~BadEnumStringException() throw()
53 { }
54 
55 // ######################################################################
56 #define NRT_MACRO_ENUMERATED_RAW_LIST(seq) BOOST_PP_SEQ_ENUM(seq)
57 
58 #define NRT_MACRO_ENUMERATED_STRINGIZE(val) #val
59 #define NRT_MACRO_ENUMERATED_STRINGIZE_OP(s, data, elem) NRT_MACRO_ENUMERATED_STRINGIZE(elem)
60 #define NRT_MACRO_ENUMERATED_STRINGIZE_SEQ(seq) BOOST_PP_SEQ_TRANSFORM(NRT_MACRO_ENUMERATED_STRINGIZE_OP, 0, seq)
61 #define NRT_MACRO_ENUMERATED_QUOTED_LIST(seq) BOOST_PP_SEQ_ENUM(NRT_MACRO_ENUMERATED_STRINGIZE_SEQ(seq))
62 
63 
64 #define NRT_MACRO_ENUMERATED_PREFIX_OP(s, data, elem) data::elem
65 #define NRT_MACRO_ENUMERATED_PREFIX_SEQ(prefix, seq) BOOST_PP_SEQ_TRANSFORM(NRT_MACRO_ENUMERATED_PREFIX_OP, prefix, seq)
66 #define NRT_MACRO_ENUMERATED_PREFIXED_LIST(prefix, seq) BOOST_PP_SEQ_ENUM(NRT_MACRO_ENUMERATED_PREFIX_SEQ(prefix, seq))
67 
68 #define NRT_DEFINE_ENUM_CLASS(name, seq) \
69  enum class name { NRT_MACRO_ENUMERATED_RAW_LIST(seq) }; \
70  static std::vector<name> const name##_Values { NRT_MACRO_ENUMERATED_PREFIXED_LIST(name, seq) }; \
71  static std::vector<std::string> const name##_Strings { NRT_MACRO_ENUMERATED_QUOTED_LIST(seq) }; \
72  \
73  inline std::ostream & operator<<(std::ostream & os, name const & v) { \
74  std::vector<name>::const_iterator vitr = name##_Values.begin(), vend = name##_Values.end(); \
75  std::vector<std::string>::const_iterator sitr = name##_Strings.begin(); \
76  while (vitr != vend) if (v == *vitr) { os << *sitr; return os; } else { ++vitr; ++sitr; } \
77  return os; \
78  } \
79  \
80  inline std::istream & operator>>(std::istream & is, name & v) { \
81  std::string s; is >> s; \
82  std::vector<name>::const_iterator vitr = name##_Values.begin(), vend = name##_Values.end(); \
83  std::vector<std::string>::const_iterator sitr = name##_Strings.begin(); \
84  while (vitr != vend) if (s == *sitr) { v = *vitr; return is; } else { ++vitr; ++sitr; } \
85  throw nrt::exception::BadEnumStringException(#name, s.c_str()); \
86  }
87 
88 #endif // INCLUDE_NRT_CORE_TYPING_DETAILS_ENUMIMPL_H