Gazebo Common

API Reference

4.7.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 IGNITION_COMMON_ENUMITERATOR_HH_
18 #define IGNITION_COMMON_ENUMITERATOR_HH_
19 
20 #include <string>
21 #include <vector>
22 #include <algorithm>
23 
24 #include <gz/common/config.hh>
25 #include <gz/common/Export.hh>
26 #include <gz/common/Util.hh>
27 
28 namespace ignition
29 {
30  namespace common
31  {
42  #define IGN_ENUM(name, enumType, begin, end, ...) \
43  static ignition::common::EnumIface<enumType> name( \
44  begin, end, {__VA_ARGS__});
45 
48  template<typename T>
49  class EnumIface
50  {
55  public: EnumIface(T _start, T _end,
56  const std::vector<std::string> &_names)
57  : names(_names)
58  {
59  this->range[0] = _start;
60  this->range[1] = _end;
61  }
62 
65  public: T Begin()
66  {
67  return range[0];
68  }
69 
72  public: T End()
73  {
74  return range[1];
75  }
76 
82  public: std::string Str(T const &_e)
83  {
84  if (static_cast<unsigned int>(_e) < names.size())
85  return names[static_cast<unsigned int>(_e)];
86  else
87  return "";
88  }
89 
95  public: void Set(T &_e, const std::string &_str)
96  {
97  auto begin = std::begin(names);
98  auto end = std::end(names);
99 
100  auto find = std::find(begin, end, _str);
101  if (find != end)
102  {
103  _e = static_cast<T>(std::distance(begin, find));
104  }
105  }
106 
109  public: T range[2];
110 
114  public: std::vector<std::string> names;
115  };
116 
145 #if defined __APPLE__ && defined __clang__
146  _Pragma("clang diagnostic push")
147  _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
148 #endif
149  template<typename Enum>
150  class EnumIterator
151  : std::iterator<std::bidirectional_iterator_tag, Enum>
152  {
154  public: EnumIterator()
155  {
156  }
157 
160  // cppcheck-suppress noExplicitConstructor
161  public: EnumIterator(const Enum &_c) : c(_c)
162  {
163  }
164 
167  public: EnumIterator &operator=(const Enum &_c)
168  {
169  this->c = _c;
170  return *this;
171  }
172 
175  public: EnumIterator &operator++()
176  {
177  this->c = static_cast<Enum>(static_cast<int>(this->c) + 1);
178  return *this;
179  }
180 
183  public: EnumIterator operator++(const int)
184  {
185  EnumIterator cpy(*this);
186  ++*this;
187  return cpy;
188  }
189 
192  public: EnumIterator &operator--()
193  {
194  this->c = static_cast<Enum>(static_cast<int>(this->c) - 1);
195  return *this;
196  }
197 
200  public: EnumIterator operator--(const int)
201  {
202  EnumIterator cpy(*this);
203  --*this;
204  return cpy;
205  }
206 
209  public: Enum operator*() const
210  {
211  return c;
212  }
213 
216  public: Enum Value() const
217  {
218  return this->c;
219  }
220 
224  private: Enum c;
225  };
226 #if defined __APPLE__ && defined __clang__
227  _Pragma("clang diagnostic pop")
228 #endif
229 
234  template<typename Enum>
235  bool operator==(EnumIterator<Enum> _e1, EnumIterator<Enum> _e2)
236  {
237  return _e1.Value() == _e2.Value();
238  }
239 
244  template<typename Enum>
245  bool operator!=(EnumIterator<Enum> _e1, EnumIterator<Enum> _e2)
246  {
247  return !(_e1 == _e2);
248  }
249  }
250 }
251 #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)