##

EnumIface.hh
Go to the documentation of this file.
/*
* Copyright (C) 2015 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef _GAZEBO_ENUMITERATOR_HH_
#define _GAZEBO_ENUMITERATOR_HH_
#include <string>
#include <vector>
#include <algorithm>
#include "gazebo/util/system.hh"
#include "gazebo/common/Assert.hh"
namespace gazebo
{
namespace common
{
#define GZ_ENUM(enumType, begin, end, ...) \
template<> GZ_COMMON_VISIBLE enumType \
common::EnumIface<enumType>::range[] = {begin, end}; \
template<> GZ_COMMON_VISIBLE \
std::vector<std::string> common::EnumIface<enumType>::names = {__VA_ARGS__};
#ifndef _MSC_VER
#define GZ_ENUM_VISIBILITY(visibility, enumType, begin, end, ...) \
template<> visibility enumType \
common::EnumIface<enumType>::range[] = {begin, end}; \
template<> visibility std::vector<std::string> \
common::EnumIface<enumType>::names = {__VA_ARGS__};
#else
#define GZ_ENUM_VISIBILITY(visibility, enumType, begin, end, ...) \
template<> enumType \
common::EnumIface<enumType>::range[] = {begin, end}; \
template<> std::vector<std::string> \
common::EnumIface<enumType>::names = {__VA_ARGS__}; \
template class visibility common::EnumIface<enumType>;
#endif
#ifndef _MSC_VER
#define GZ_ENUM_DECLARE(visibility, enumType)
#else
#define GZ_ENUM_DECLARE(visibility, enumType) \
template class visibility common::EnumIface<enumType>;
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundefined-var-template"
#endif
template<typename T>
class EnumIface
{
public: static T Begin()
{
return range[0];
}
public: static T End()
{
return range[1];
}
static std::string Str(T const &_e)
{
if (static_cast<unsigned int>(_e) < names.size())
return names[static_cast<unsigned int>(_e)];
else
return "";
}
static void Set(T &_e, const std::string &_str)
{
static auto begin = std::begin(names);
static auto end = std::end(names);
auto find = std::find(begin, end, _str);
if (find != end)
{
_e = static_cast<T>(std::distance(begin, find));
}
}
public: static T range[2];
public: static std::vector<std::string> names;
};
#ifdef __clang__
#pragma clang diagnostic pop
#endif
template<typename Enum>
class EnumIterator
: std::iterator<std::bidirectional_iterator_tag, Enum>
{
public: EnumIterator() : c(this->End())
{
}
// cppcheck-suppress noExplicitConstructor
public: EnumIterator(const Enum _c) : c(_c)
{
GZ_ASSERT(this->c >= this->Begin() && this->c <= this->End(),
"Invalid enum value in EnumIterator constructor");
}
public: EnumIterator &operator=(const Enum _c)
{
GZ_ASSERT(_c >= this->Begin() && _c <= this->End(),
"Invalid operator= value in EnumIterator");
this->c = _c;
return *this;
}
public: static Enum Begin()
{
return EnumIface<Enum>::Begin();
}
public: static Enum End()
{
return EnumIface<Enum>::End();
}
public: EnumIterator &operator++()
{
GZ_ASSERT(this->c != this->End(), "Incrementing past end of enum");
this->c = static_cast<Enum>(static_cast<int>(this->c) + 1);
return *this;
}
public: EnumIterator operator++(const int)
{
GZ_ASSERT(this->c != this->End(), "Incrementing past end of enum");
EnumIterator cpy(*this);
++*this;
return cpy;
}
public: EnumIterator &operator--()
{
GZ_ASSERT(this->c != this->Begin(), "decrementing beyond begin?");
this->c = static_cast<Enum>(static_cast<int>(this->c) - 1);
return *this;
}
public: EnumIterator operator--(const int)
{
GZ_ASSERT(this->c != this->Begin(), "Decrementing beyond beginning.");
EnumIterator cpy(*this);
--*this;
return cpy;
}
public: Enum operator*() const
{
GZ_ASSERT(this->c != this->End(), "Cannot dereference end iterator");
return c;
}
public: Enum Value() const
{
return this->c;
}
private: Enum c;
};
template<typename Enum>
bool operator==(EnumIterator<Enum> _e1, EnumIterator<Enum> _e2)
{
return _e1.Value() == _e2.Value();
}
template<typename Enum>
bool operator!=(EnumIterator<Enum> _e1, EnumIterator<Enum> _e2)
{
return !(_e1 == _e2);
}
}
}
#endif
gazebo
Forward declarations for the common classes.
Definition: Animation.hh:26
common
common
Definition: FuelModelDatabase.hh:37
GZ_ASSERT
#define GZ_ASSERT(_expr, _msg)
This macro define the standard way of launching an exception inside gazebo.
Definition: Assert.hh:24
system.hh
gazebo::msgs::Set
GAZEBO_VISIBLE void Set(common::Image &_img, const msgs::Image &_msg)
Convert a msgs::Image to a common::Image.
Assert.hh