Gazebo Common
API Reference
7.4.0
insert_drive_file
Tutorials
library_books
Classes
toc
Namespaces
insert_drive_file
Files
launch
Gazebo Website
Index
List
Hierarchy
Members: All
Members: Functions
Members: Variables
Members: Typedefs
Members: Enumerations
Members: Enumerator
List
Members
Functions
Typedefs
Variables
Enumerations
Enumerator
src
gz-common
include
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 <iterator>
21
#include <string>
22
#include <vector>
23
#include <algorithm>
24
25
#include <gz/common/Export.hh>
26
#include <
gz/common/Util.hh
>
27
28
namespace
gz::common
29
{
32
template
<
typename
T>
33
class
EnumIface
34
{
39
public
:
EnumIface
(T _start, T _end,
40
const
std::vector<std::string>
&_names)
41
:
names
(_names)
42
{
43
this->
range
[0] = _start;
44
this->
range
[1] = _end;
45
}
46
49
public
: T
Begin
()
50
{
51
return
range
[0];
52
}
53
56
public
: T
End
()
57
{
58
return
range
[1];
59
}
60
66
public
:
std::string
Str
(T
const
&_e)
67
{
68
std::string
ret;
69
if
(
static_cast<
unsigned
int
>
(_e) <
names
.
size
())
70
ret =
names
[
static_cast<
unsigned
int
>
(_e)];
71
return
ret;
72
}
73
79
public
:
void
Set
(T &_e,
const
std::string
&_str)
80
{
81
auto
begin =
std::begin
(
names
);
82
auto
end =
std::end
(
names
);
83
84
auto
find =
std::find
(begin, end, _str);
85
if
(find != end)
86
{
87
_e =
static_cast<
T
>
(
std::distance
(begin, find));
88
}
89
}
90
93
public
: T
range
[2];
94
98
public
:
std::vector<std::string>
names
;
99
};
100
129
template
<
typename
Enum>
130
class
EnumIterator
131
{
132
public
:
using
iterator_category
=
std::bidirectional_iterator_tag
;
133
public
:
using
value_type
= Enum;
134
public
:
using
difference_type
=
std::ptrdiff_t
;
135
public
:
using
pointer
= Enum*;
136
public
:
using
reference
= Enum&;
137
139
public
:
EnumIterator
() =
default
;
140
143
// cppcheck-suppress noExplicitConstructor
144
public
:
explicit
EnumIterator
(
const
Enum &_c) : c(_c)
145
{
146
}
147
150
public
:
EnumIterator
&
operator=
(
const
Enum &_c)
151
{
152
this->c = _c;
153
return
*
this
;
154
}
155
158
public
:
EnumIterator
&
operator++
()
159
{
160
this->c =
static_cast<
Enum
>
(
static_cast<
int
>
(this->c) + 1);
161
return
*
this
;
162
}
163
166
public
:
EnumIterator
operator++
(
const
int
)
167
{
168
EnumIterator
cpy(*
this
);
169
++*
this
;
170
return
cpy;
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
: Enum
operator*
()
const
193
{
194
return
c;
195
}
196
199
public
: Enum
Value
()
const
200
{
201
return
this->c;
202
}
203
207
private
: Enum c;
208
};
209
214
template
<
typename
Enum>
215
bool
operator==
(
EnumIterator<Enum>
_e1,
EnumIterator<Enum>
_e2)
216
{
217
return
_e1.
Value
() == _e2.
Value
();
218
}
219
224
template
<
typename
Enum>
225
bool
operator!=
(
EnumIterator<Enum>
_e1,
EnumIterator<Enum>
_e2)
226
{
227
return
!(_e1 == _e2);
228
}
229
238
template
<
typename
T>
239
static
constexpr
gz::common::EnumIface<T>
gzEnum
(
240
T _enumBegin, T _enumEnd,
const
std::vector<std::string>
&_enumVals)
241
{
242
return
gz::common::EnumIface<T>
(_enumBegin, _enumEnd, _enumVals);
243
}
244
}
// namespace gz::common
245
246
257
#define GZ_ENUM(name, enumType, begin, end, ...) \
258
static auto name = gz::common::gzEnum(begin, end, {__VA_ARGS__});
259
260
#endif
// GZ_COMMON_ENUMITERATOR_HH_