Ignition Common

API Reference

3.6.1
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 #include <ignition/common/Util.hh>
24 #include <ignition/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  template<typename Enum>
144  class EnumIterator
145  : std::iterator<std::bidirectional_iterator_tag, Enum>
146  {
148  public: EnumIterator()
149  {
150  }
151 
154  // cppcheck-suppress noExplicitConstructor
155  public: EnumIterator(const Enum &_c) : c(_c)
156  {
157  }
158 
161  public: EnumIterator &operator=(const Enum &_c)
162  {
163  this->c = _c;
164  return *this;
165  }
166 
169  public: EnumIterator &operator++()
170  {
171  this->c = static_cast<Enum>(static_cast<int>(this->c) + 1);
172  return *this;
173  }
174 
177  public: EnumIterator operator++(const int)
178  {
179  EnumIterator cpy(*this);
180  ++*this;
181  return cpy;
182  }
183 
186  public: EnumIterator &operator--()
187  {
188  this->c = static_cast<Enum>(static_cast<int>(this->c) - 1);
189  return *this;
190  }
191 
194  public: EnumIterator operator--(const int)
195  {
196  EnumIterator cpy(*this);
197  --*this;
198  return cpy;
199  }
200 
203  public: Enum operator*() const
204  {
205  return c;
206  }
207 
210  public: Enum Value() const
211  {
212  return this->c;
213  }
214 
218  private: Enum c;
219  };
220 
225  template<typename Enum>
226  bool operator==(EnumIterator<Enum> _e1, EnumIterator<Enum> _e2)
227  {
228  return _e1.Value() == _e2.Value();
229  }
230 
235  template<typename Enum>
236  bool operator!=(EnumIterator<Enum> _e1, EnumIterator<Enum> _e2)
237  {
238  return !(_e1 == _e2);
239  }
240  }
241 }
242 #endif
T distance(T... args)
T operator!=(T... args)
T end(T... args)
STL class.
T find(T... args)
STL class.
T begin(T... args)
Forward declarations for the common classes.