EnumIface.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 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 _GAZEBO_ENUMITERATOR_HH_
18 #define _GAZEBO_ENUMITERATOR_HH_
19 
20 #include <string>
21 #include <vector>
22 #include <algorithm>
23 #include "gazebo/util/system.hh"
24 #include "gazebo/common/Assert.hh"
25 
26 namespace gazebo
27 {
28  namespace common
29  {
39  #define GZ_ENUM(enumType, begin, end, ...) \
40  template<> GZ_COMMON_VISIBLE enumType \
41  common::EnumIface<enumType>::range[] = {begin, end}; \
42  template<> GZ_COMMON_VISIBLE \
43  std::vector<std::string> common::EnumIface<enumType>::names = {__VA_ARGS__};
44 
45 #ifdef __clang__
46 #pragma clang diagnostic push
47 #pragma clang diagnostic ignored "-Wundefined-var-template"
48 #endif
49  template<typename T>
52  class EnumIface
53  {
56  public: static T Begin()
57  {
58  return range[0];
59  }
60 
63  public: static T End()
64  {
65  return range[1];
66  }
67 
73  static std::string Str(T const &_e)
74  {
75  if (static_cast<unsigned int>(_e) < names.size())
76  return names[static_cast<unsigned int>(_e)];
77  else
78  return "";
79  }
80 
86  static void Set(T &_e, const std::string &_str)
87  {
88  static auto begin = std::begin(names);
89  static auto end = std::end(names);
90 
91  auto find = std::find(begin, end, _str);
92  if (find != end)
93  {
94  _e = static_cast<T>(std::distance(begin, find));
95  }
96  }
97 
100  public: static T range[2];
101 
105  public: static std::vector<std::string> names;
106  };
107 #ifdef __clang__
108 #pragma clang diagnostic pop
109 #endif
110 
139  template<typename Enum>
140  class EnumIterator
141  : std::iterator<std::bidirectional_iterator_tag, Enum>
142  {
144  public: EnumIterator() : c(this->End())
145  {
146  }
147 
150  // cppcheck-suppress noExplicitConstructor
151  public: EnumIterator(const Enum _c) : c(_c)
152  {
153  GZ_ASSERT(this->c >= this->Begin() && this->c <= this->End(),
154  "Invalid enum value in EnumIterator constructor");
155  }
156 
159  public: EnumIterator &operator=(const Enum _c)
160  {
161  GZ_ASSERT(_c >= this->Begin() && _c <= this->End(),
162  "Invalid operator= value in EnumIterator");
163  this->c = _c;
164  return *this;
165  }
166 
169  public: static Enum Begin()
170  {
171  return EnumIface<Enum>::Begin();
172  }
173 
176  public: static Enum End()
177  {
178  return EnumIface<Enum>::End();
179  }
180 
183  public: EnumIterator &operator++()
184  {
185  GZ_ASSERT(this->c != this->End(), "Incrementing past end of enum");
186  this->c = static_cast<Enum>(static_cast<int>(this->c) + 1);
187  return *this;
188  }
189 
192  public: EnumIterator operator++(const int)
193  {
194  GZ_ASSERT(this->c != this->End(), "Incrementing past end of enum");
195  EnumIterator cpy(*this);
196  ++*this;
197  return cpy;
198  }
199 
202  public: EnumIterator &operator--()
203  {
204  GZ_ASSERT(this->c != this->Begin(), "decrementing beyond begin?");
205  this->c = static_cast<Enum>(static_cast<int>(this->c) - 1);
206  return *this;
207  }
208 
211  public: EnumIterator operator--(const int)
212  {
213  GZ_ASSERT(this->c != this->Begin(), "Decrementing beyond beginning.");
214  EnumIterator cpy(*this);
215  --*this;
216  return cpy;
217  }
218 
221  public: Enum operator*() const
222  {
223  GZ_ASSERT(this->c != this->End(), "Cannot dereference end iterator");
224  return c;
225  }
226 
229  public: Enum Value() const
230  {
231  return this->c;
232  }
233 
237  private: Enum c;
238  };
239 
244  template<typename Enum>
245  bool operator==(EnumIterator<Enum> _e1, EnumIterator<Enum> _e2)
246  {
247  return _e1.Value() == _e2.Value();
248  }
249 
254  template<typename Enum>
255  bool operator!=(EnumIterator<Enum> _e1, EnumIterator<Enum> _e2)
256  {
257  return !(_e1 == _e2);
258  }
259  }
260 }
261 #endif
#define GZ_ASSERT(_expr, _msg)
This macro define the standard way of launching an exception inside gazebo.
Definition: Assert.hh:24
Forward declarations for the common classes.
Definition: Animation.hh:26
GAZEBO_VISIBLE void Set(common::Image &_img, const msgs::Image &_msg)
Convert a msgs::Image to a common::Image.