JeVois  1.5
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
EnumImpl.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 // This file has been modified / imported from the Neuromorphic Robotics Toolkit (NRT). Original copyright is:
19 
20 /* author Laurent Itti
21  // ////////////////////////////////////////////////////////////////////////
22  // The iLab Neuromorphic Robotics Toolkit (NRT) //
23  // Copyright 2010-2012 by the University of Southern California (USC) //
24  // and the iLab at USC. //
25  // //
26  // iLab - University of Southern California //
27  // Hedco Neurociences Building, Room HNB-10 //
28  // Los Angeles, Ca 90089-2520 - USA //
29  // //
30  // See http://ilab.usc.edu for information about this project. //
31  // ////////////////////////////////////////////////////////////////////////
32  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
33  // //
34  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
35  // redistribute it and/or modify it under the terms of the GNU General //
36  // Public License as published by the Free Software Foundation, either //
37  // version 3 of the License, or (at your option) any later version. //
38  // //
39  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
40  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
41  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
42  // PURPOSE. See the GNU General Public License for more details. //
43  // //
44  // You should have received a copy of the GNU General Public License //
45  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
46  // <http://www.gnu.org/licenses/>. //
47  // ////////////////////////////////////////////////////////////////////////
48 */
49 
50 #pragma once
51 
52 #include <iostream>
53 #include <vector>
54 #include <stdexcept>
55 #include <boost/preprocessor/seq/enum.hpp>
56 #include <boost/preprocessor/seq/transform.hpp>
57 
58 #ifndef JEVOIS_DOXYGEN
59 
60 // ######################################################################
61 #define JEVOIS_MACRO_ENUMERATED_RAW_LIST(seq) BOOST_PP_SEQ_ENUM(seq)
62 
63 #define JEVOIS_MACRO_ENUMERATED_STRINGIZE(val) #val
64 
65 #define JEVOIS_MACRO_ENUMERATED_STRINGIZE_OP(s, data, elem) JEVOIS_MACRO_ENUMERATED_STRINGIZE(elem)
66 
67 #define JEVOIS_MACRO_ENUMERATED_STRINGIZE_SEQ(seq) BOOST_PP_SEQ_TRANSFORM(JEVOIS_MACRO_ENUMERATED_STRINGIZE_OP, 0, seq)
68 
69 #define JEVOIS_MACRO_ENUMERATED_QUOTED_LIST(seq) BOOST_PP_SEQ_ENUM(JEVOIS_MACRO_ENUMERATED_STRINGIZE_SEQ(seq))
70 
71 #define JEVOIS_MACRO_ENUMERATED_PREFIX_OP(s, data, elem) data::elem
72 
73 #define JEVOIS_MACRO_ENUMERATED_PREFIX_SEQ(prefix, seq) BOOST_PP_SEQ_TRANSFORM(JEVOIS_MACRO_ENUMERATED_PREFIX_OP, prefix, seq)
74 
75 #define JEVOIS_MACRO_ENUMERATED_PREFIXED_LIST(prefix, seq) BOOST_PP_SEQ_ENUM(JEVOIS_MACRO_ENUMERATED_PREFIX_SEQ(prefix, seq))
76 
77 #define JEVOIS_DEFINE_ENUM_CLASS(name, seq) \
78  enum class name { JEVOIS_MACRO_ENUMERATED_RAW_LIST(seq) }; \
79  static std::vector<name> const name##_Values { JEVOIS_MACRO_ENUMERATED_PREFIXED_LIST(name, seq) }; \
80  static std::vector<std::string> const name##_Strings { JEVOIS_MACRO_ENUMERATED_QUOTED_LIST(seq) }; \
81  \
82  inline std::ostream & operator<<(std::ostream & os, name const & v) { \
83  std::vector<name>::const_iterator vitr = name##_Values.begin(), vend = name##_Values.end(); \
84  std::vector<std::string>::const_iterator sitr = name##_Strings.begin(); \
85  while (vitr != vend) if (v == *vitr) { os << *sitr; return os; } else { ++vitr; ++sitr; } \
86  return os; \
87  } \
88  \
89  inline std::istream & operator>>(std::istream & is, name & v) { \
90  std::string s; is >> s; \
91  std::vector<name>::const_iterator vitr = name##_Values.begin(), vend = name##_Values.end(); \
92  std::vector<std::string>::const_iterator sitr = name##_Strings.begin(); \
93  while (vitr != vend) if (s == *sitr) { v = *vitr; return is; } else { ++vitr; ++sitr; } \
94  throw std::range_error("Invalid value [" + s + "] for Enum class " #name); \
95  return is; /* cannot happen but compiler complains */ \
96  }
97 
98 #endif