iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Enum.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 #ifndef INCLUDE_NRT_CORE_TYPING_ENUM_H
36 #define INCLUDE_NRT_CORE_TYPING_ENUM_H
37 
39 #include <string>
40 #include <vector>
41 
42 /*! \defgroup enumclass Enum class convenience macro
43  \ingroup typing */
44 
45 
46 /*! \def NRT_DEFINE_ENUM_CLASS(name, SEQ)
47  \hideinitializer
48 
49  Helper macro to define new enum class types. The macro NRT_DEFINE_ENUM_CLASS(name, SEQ) helps you define a new
50  enum class. SEQ should be a BOOST_PP_SEQ. You will get:
51 
52  - an enum class declared and defined with the name and values supplied
53  - a static const std::vector of your enum values, with all the values that the enum can take, in name_Values
54  - a static const std::vector of std::string, with string names of all the values the enum can take, in name_Strings
55  - operator<< and operator>> for your enum.
56 
57  You can in particular use name_Values when declaring a ParameterDef<name> to list all the valid values, and the
58  streaming operators will allow you to set the Parameter value by string, etc.
59 
60  For example:
61 
62  @code
63  NRT_DEFINE_ENUM_CLASS(myEnum, (One) (Two) (Three));
64  @endcode
65 
66  expands to:
67 
68  - first, the actual definition of the enum class, with the values you specified in the sequence:
69  @code
70  enum class myEnum { One, Two, Three };
71  @endcode
72 
73  - second, a static const vector that contains all the enum values, listed in the order you specified:
74  @code
75  static std::vector<myEnum> const myEnum_Values { myEnum::One, myEnum::Two, myEnum::Three };
76  @endcode
77 
78  - third, a static const vector of strings that contains all the enum values in string form, listed in the order you
79  specified:
80  @code
81  static std::vector<std::string> const myEnum_Strings { "One", "Two", "Three" };
82  @endcode
83 
84  - finally, two stream operators that allow you to convert between your enum values and string:
85  @code
86  inline std::ostream & operator<<(std::ostream & os, myEnum const & v) {
87  std::vector<myEnum>::const_iterator vitr = myEnum_Values.begin(), vend = myEnum_Values.end();
88  std::vector<std::string>::const_iterator sitr = myEnum_Strings.begin();
89  while (vitr != vend) if (v == *vitr) { os << *sitr; return os; } else { ++vitr; ++sitr; }
90  return os;
91  }
92 
93  inline std::istream & operator>>(std::istream & is, myEnum & v) {
94  std::string s; is >> s;
95  std::vector<myEnum>::const_iterator vitr = myEnum_Values.begin(), vend = myEnum_Values.end();
96  std::vector<std::string>::const_iterator sitr = myEnum_Strings.begin();
97  while (vitr != vend) if (s == *sitr) { v = *vitr; return is; } else { ++vitr; ++sitr; }
98  throw nrt::exception::BadEnumStringException("myEnum", s.c_str());
99  };
100  @endcode
101 
102  Note how the \c operator>> will throw nrt::exception::BadEnumStringException if the given string does not match the
103  string representation of one of the enum's values.
104 
105  \ingroup enumclass */
106 
107 // exception:
108 namespace nrt
109 {
110  namespace exception
111  {
112  //! Exception thrown when attempting to stream a bad string into an enum value
113  /*! \relates NRT_DEFINE_ENUM_CLASS(name, SEQ) */
115  {
116  public:
117  BadEnumStringException(char const * classname, char const * s) throw();
118 
119  virtual ~BadEnumStringException() throw();
120 
121  private:
122  std::string const whatstr;
123  };
124  }
125 }
126 
127 
128 
129 //! Include details of no interest to the user
131 
132 #endif // INCLUDE_NRT_CORE_TYPING_ENUM_H