Gazebo Common

API Reference

3.17.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 #include <gz/common/Util.hh>
24 #include <gz/common/Export.hh>
25 
26 namespace ignition
27 {
28  namespace common
29  {
40  #define IGN_ENUM(name, enumType, begin, end, ...) \
41  static ignition::common::EnumIface<enumType> name( \
42  begin, end, {__VA_ARGS__});
43 
46  template<typename T>
47  class EnumIface
48  {
53  public: EnumIface(T _start, T _end,
54  const std::vector<std::string> &_names)
55  : names(_names)
56  {
57  this->range[0] = _start;
58  this->range[1] = _end;
59  }
60 
63  public: T Begin()
64  {
65  return range[0];
66  }
67 
70  public: T End()
71  {
72  return range[1];
73  }
74 
80  public: std::string Str(T const &_e)
81  {
82  if (static_cast<unsigned int>(_e) < names.size())
83  return names[static_cast<unsigned int>(_e)];
84  else
85  return "";
86  }
87 
93  public: void Set(T &_e, const std::string &_str)
94  {
95  auto begin = std::begin(names);
96  auto end = std::end(names);
97 
98  auto find = std::find(begin, end, _str);
99  if (find != end)
100  {
101  _e = static_cast<T>(std::distance(begin, find));
102  }
103  }
104 
107  public: T range[2];
108 
112  public: std::vector<std::string> names;
113  };
114 
143 #if defined __APPLE__ && defined __clang__
144  _Pragma("clang diagnostic push")
145  _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
146 #endif
147  template<typename Enum>
148  class EnumIterator
149  : std::iterator<std::bidirectional_iterator_tag, Enum>
150  {
152  public: EnumIterator()
153  {
154  }
155 
158  // cppcheck-suppress noExplicitConstructor
159  public: EnumIterator(const Enum &_c) : c(_c)
160  {
161  }
162 
165  public: EnumIterator &operator=(const Enum &_c)
166  {
167  this->c = _c;
168  return *this;
169  }
170 
173  public: EnumIterator &operator++()
174  {
175  this->c = static_cast<Enum>(static_cast<int>(this->c) + 1);
176  return *this;
177  }
178 
181  public: EnumIterator operator++(const int)
182  {
183  EnumIterator cpy(*this);
184  ++*this;
185  return cpy;
186  }
187 
190  public: EnumIterator &operator--()
191  {
192  this->c = static_cast<Enum>(static_cast<int>(this->c) - 1);
193  return *this;
194  }
195 
198  public: EnumIterator operator--(const int)
199  {
200  EnumIterator cpy(*this);
201  --*this;
202  return cpy;
203  }
204 
207  public: Enum operator*() const
208  {
209  return c;
210  }
211 
214  public: Enum Value() const
215  {
216  return this->c;
217  }
218 
222  private: Enum c;
223  };
224 #if defined __APPLE__ && defined __clang__
225  _Pragma("clang diagnostic pop")
226 #endif
227 
232  template<typename Enum>
233  bool operator==(EnumIterator<Enum> _e1, EnumIterator<Enum> _e2)
234  {
235  return _e1.Value() == _e2.Value();
236  }
237 
242  template<typename Enum>
243  bool operator!=(EnumIterator<Enum> _e1, EnumIterator<Enum> _e2)
244  {
245  return !(_e1 == _e2);
246  }
247  }
248 }
249 #endif
Forward declarations for the common classes.
STL class.
T operator!=(T... args)
STL class.
T find(T... args)
T distance(T... args)
T begin(T... args)
T end(T... args)