Gazebo Common

API Reference

5.6.0
gz/common/EnumIface.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GZ_COMMON_ENUMITERATOR_HH_
18 #define GZ_COMMON_ENUMITERATOR_HH_
19 
20 #include <string>
21 #include <vector>
22 #include <algorithm>
23 
24 #include <gz/common/Export.hh>
25 #include <gz/common/Util.hh>
26 
27 namespace gz
28 {
29  namespace common
30  {
41  #define GZ_ENUM(name, enumType, begin, end, ...) \
42  static gz::common::EnumIface<enumType> name( \
43  begin, end, {__VA_ARGS__});
44 
47  template<typename T>
48  class EnumIface
49  {
54  public: EnumIface(T _start, T _end,
55  const std::vector<std::string> &_names)
56  : names(_names)
57  {
58  this->range[0] = _start;
59  this->range[1] = _end;
60  }
61 
64  public: T Begin()
65  {
66  return range[0];
67  }
68 
71  public: T End()
72  {
73  return range[1];
74  }
75 
81  public: std::string Str(T const &_e)
82  {
83  if (static_cast<unsigned int>(_e) < names.size())
84  return names[static_cast<unsigned int>(_e)];
85  else
86  return "";
87  }
88 
94  public: void Set(T &_e, const std::string &_str)
95  {
96  auto begin = std::begin(names);
97  auto end = std::end(names);
98 
99  auto find = std::find(begin, end, _str);
100  if (find != end)
101  {
102  _e = static_cast<T>(std::distance(begin, find));
103  }
104  }
105 
108  public: T range[2];
109 
113  public: std::vector<std::string> names;
114  };
115 
144  GZ_UTILS_WARN_IGNORE__DEPRECATED_DECLARATION
145  template<typename Enum>
146  class EnumIterator
147  : std::iterator<std::bidirectional_iterator_tag, Enum>
148  {
150  public: EnumIterator()
151  {
152  }
153 
156  // cppcheck-suppress noExplicitConstructor
157  public: EnumIterator(const Enum &_c) : c(_c)
158  {
159  }
160 
163  public: EnumIterator &operator=(const Enum &_c)
164  {
165  this->c = _c;
166  return *this;
167  }
168 
171  public: EnumIterator &operator++()
172  {
173  this->c = static_cast<Enum>(static_cast<int>(this->c) + 1);
174  return *this;
175  }
176 
179  public: EnumIterator operator++(const int)
180  {
181  EnumIterator cpy(*this);
182  ++*this;
183  return cpy;
184  }
185 
188  public: EnumIterator &operator--()
189  {
190  this->c = static_cast<Enum>(static_cast<int>(this->c) - 1);
191  return *this;
192  }
193 
196  public: EnumIterator operator--(const int)
197  {
198  EnumIterator cpy(*this);
199  --*this;
200  return cpy;
201  }
202 
205  public: Enum operator*() const
206  {
207  return c;
208  }
209 
212  public: Enum Value() const
213  {
214  return this->c;
215  }
216 
220  private: Enum c;
221  };
222  GZ_UTILS_WARN_RESUME__DEPRECATED_DECLARATION
223 
228  template<typename Enum>
229  bool operator==(EnumIterator<Enum> _e1, EnumIterator<Enum> _e2)
230  {
231  return _e1.Value() == _e2.Value();
232  }
233 
238  template<typename Enum>
239  bool operator!=(EnumIterator<Enum> _e1, EnumIterator<Enum> _e2)
240  {
241  return !(_e1 == _e2);
242  }
243  }
244 }
245 #endif
ragment -->