From f894c309042abdd2a9d762f7fe8ea1a8758a883e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Neum=C3=BCller?= Date: Thu, 27 Jun 2013 15:16:40 +0200 Subject: [PATCH] Use C++11 scoped enums. --- src/Logfile.cpp | 27 ++++++++++++++------------ src/Logfile.hpp | 26 ++++++++++++------------- src/MapInfo.cpp | 8 ++++---- src/MapInfo.hpp | 4 ++-- src/Tilemap.cpp | 7 ++++--- src/Tilemap.hpp | 4 ++-- src/collision/Collisions.hpp | 9 +++++---- src/collision/RectCollideableGroup.hpp | 3 ++- src/compsys/Entity.cpp | 18 ++++++++--------- src/compsys/Entity.hpp | 2 +- src/luaexport/EntitySystem.cpp | 6 +++--- src/luaexport/Logfile.cpp | 2 +- src/luaexport/Tilemap.cpp | 8 ++++---- 13 files changed, 65 insertions(+), 59 deletions(-) diff --git a/src/Logfile.cpp b/src/Logfile.cpp index c161b4a..27aaa41 100644 --- a/src/Logfile.cpp +++ b/src/Logfile.cpp @@ -72,7 +72,7 @@ static const std::string filterHtml(std::string const& s) return result; } -Logfile::Logfile(const std::string& filename, loglevel::T min_level_, logstyle::T style_): +Logfile::Logfile(const std::string& filename, loglevel min_level_, logstyle style_): m_min_level(min_level_), m_style(style_) { @@ -105,7 +105,7 @@ void Logfile::init() sf::err().rdbuf(m_sferr.rdbuf()); } -void Logfile::open(const std::string& filename, logstyle::T style) +void Logfile::open(const std::string& filename, logstyle style) { m_file.clear(); m_file.open(enc::utf8ToFstreamArg(filename)); @@ -139,20 +139,23 @@ void Logfile::open(const std::string& filename, logstyle::T style) LOG_I("Start logging at " + full_time()); } -void Logfile::write(const boost::format& msg, loglevel::T level, char const* location) +void Logfile::write(const boost::format& msg, loglevel level, char const* location) { write(msg.str(), level, location); } -void Logfile::write(std::string const& msg, loglevel::T level, char const* location) +void Logfile::write(std::string const& msg, loglevel level_, char const* location) { - if (level < m_min_level) + if (level_ < m_min_level) return; - static_assert(loglevel::max == 5, "Please correct levelnames below!"); - static const std::array levelnames = + auto level = static_cast(level_); + + static auto const maxloglevel = static_cast(loglevel::max); + static_assert(maxloglevel == 5, "Please correct levelnames below!"); + static const std::array levelnames = { "debug", "info", "warning", "error", "fatal" }; - static const std::array levelpreambles = + static const std::array levelpreambles = { " ", " _", " + ", " * ", "!>>>" }; std::string location_; @@ -208,23 +211,23 @@ void Logfile::write(std::string const& msg, loglevel::T level, char const* locat } -loglevel::T Logfile::minLevel() const +loglevel Logfile::minLevel() const { return m_min_level; } -void Logfile::setMinLevel(loglevel::T level) +void Logfile::setMinLevel(loglevel level) { m_min_level = level; } -void Logfile::logThrow(const std::exception& ex, loglevel::T level, char const* location) +void Logfile::logThrow(const std::exception& ex, loglevel level, char const* location) { logEx(ex, level, location); throw ex; } -void Logfile::logEx(const std::exception& ex, loglevel::T level, char const* location) +void Logfile::logEx(const std::exception& ex, loglevel level, char const* location) { write(boost::diagnostic_information(ex), level, location); } diff --git a/src/Logfile.hpp b/src/Logfile.hpp index 774a6bb..963ecfb 100644 --- a/src/Logfile.hpp +++ b/src/Logfile.hpp @@ -12,36 +12,36 @@ #include -namespace loglevel { enum T { debug, info, warning, error, fatal, max }; } -namespace logstyle { enum T { html, plain, like_extension, max }; } +enum class loglevel { debug, info, warning, error, fatal, max }; +enum class logstyle { html, plain, like_extension, max }; class Logfile { public: explicit Logfile(const std::string& filename, - loglevel::T min_level = loglevel::debug, - logstyle::T style = logstyle::like_extension); + loglevel min_level = loglevel::debug, + logstyle style = logstyle::like_extension); Logfile(); ~Logfile(); void write( const std::string& msg, - loglevel::T level = loglevel::info, + loglevel level = loglevel::info, char const* location = nullptr); void write( const boost::format& msg, - loglevel::T level = loglevel::info, + loglevel level = loglevel::info, char const* location = nullptr); - void open(const std::string& filename, logstyle::T style = logstyle::like_extension); - loglevel::T minLevel() const; - void setMinLevel(loglevel::T level); + void open(const std::string& filename, logstyle style = logstyle::like_extension); + loglevel minLevel() const; + void setMinLevel(loglevel level); void logThrow( const std::exception& ex, - loglevel::T level = loglevel::error, + loglevel level = loglevel::error, char const* location = nullptr); void logEx( const std::exception& ex, - loglevel::T level = loglevel::error, + loglevel level = loglevel::error, char const* location = nullptr); class Error; @@ -52,8 +52,8 @@ class Logfile void init(); const std::string printable_time() const; - loglevel::T m_min_level; - logstyle::T m_style; + loglevel m_min_level; + logstyle m_style; std::ofstream m_file; boost::iostreams::filtering_ostream m_sferr; sf::Clock m_timer; diff --git a/src/MapInfo.cpp b/src/MapInfo.cpp index ff16d55..9b7e0bf 100644 --- a/src/MapInfo.cpp +++ b/src/MapInfo.cpp @@ -236,16 +236,16 @@ MapInfo loadTilemap(jd::Tilemap& tm, std::string const& vfilename) o.properties = loadPropertiesOptional(obj.second); o.tileId = obj.second.get(".gid", 0u); if (o.tileId) { - o.objectType = MapObject::tile; + o.objectType = MapObject::T::tile; o.position.y -= ts.size().y; // assuming map orientation is orthogonal. } else if (auto const line = obj.second.get_child_optional("polyline")) { - o.objectType = MapObject::line; + o.objectType = MapObject::T::line; o.relativePoints = loadPointsOptional(*line); } else if (auto const line = obj.second.get_child_optional("polygon")) { - o.objectType = MapObject::poly; + o.objectType = MapObject::T::poly; o.relativePoints = loadPointsOptional(*line); } else { - o.objectType = MapObject::rect; // WARN could also be invalid + o.objectType = MapObject::T::rect; // WARN could also be invalid } group.objects.push_back(std::move(o)); } else if (obj.first != "") { diff --git a/src/MapInfo.hpp b/src/MapInfo.hpp index 747287a..0a31a15 100644 --- a/src/MapInfo.hpp +++ b/src/MapInfo.hpp @@ -17,8 +17,8 @@ typedef std::unordered_map PropertyMap; struct MapObject { - enum T { rect, tile, line, poly }; - int objectType; + enum class T { rect, tile, line, poly }; + T objectType; PropertyMap properties; std::string name; std::string type; diff --git a/src/Tilemap.cpp b/src/Tilemap.cpp index 99cbf33..2eac667 100644 --- a/src/Tilemap.cpp +++ b/src/Tilemap.cpp @@ -18,7 +18,7 @@ namespace jd { Tileset::Tileset(): m_texture(nullptr), - m_ordering(linewiseOrdered) + m_ordering(TileOrdering::linewiseOrdered) { } @@ -32,7 +32,8 @@ Tileset::Tileset( m_ordering(ordering), m_tileCount(tx->getSize().x / size.x, tx->getSize().y / size.y) { - assert(ordering == linewiseOrdered || ordering == columnwiseOrdered); + assert(ordering == TileOrdering::linewiseOrdered || + ordering == TileOrdering::columnwiseOrdered); assert(size.x <= m_texture->getSize().x); assert(size.y <= m_texture->getSize().y); } @@ -40,7 +41,7 @@ Tileset::Tileset( sf::Vector2u Tileset::position(unsigned index) const { sf::Vector2u result; - if (m_ordering == linewiseOrdered) { + if (m_ordering == TileOrdering::linewiseOrdered) { result.x = m_size.x * (index % m_tileCount.x); result.y = m_size.y * (index / m_tileCount.x); } else { diff --git a/src/Tilemap.hpp b/src/Tilemap.hpp index 9a56d48..4bbe618 100644 --- a/src/Tilemap.hpp +++ b/src/Tilemap.hpp @@ -26,13 +26,13 @@ typedef sf::Vector3 Vector3u; /* const */ class Tileset { public: - enum TileOrdering { linewiseOrdered, columnwiseOrdered }; + enum class TileOrdering { linewiseOrdered, columnwiseOrdered }; Tileset(); Tileset( sf::Vector2u size, ResourceTraits::Ptr texture, - TileOrdering ordering = linewiseOrdered); + TileOrdering ordering = TileOrdering::linewiseOrdered); sf::Vector2u position(unsigned index) const; ResourceTraits::Ptr texture() const { return m_texture; } diff --git a/src/collision/Collisions.hpp b/src/collision/Collisions.hpp index 90a9093..f03fda6 100644 --- a/src/collision/Collisions.hpp +++ b/src/collision/Collisions.hpp @@ -36,9 +36,9 @@ class CollideableGroup: public EnableWeakRefFromThis { // If a CollideableGroup delegates the collision check to the other one // it should call collideWith with delegated = isDelegated, to detect/avoid // endless recursion, if the other CollideableGroup also wants to delegate. - enum DelegateState { notDelegated, isDelegated, reDelegated }; + enum class DelegateState { notDelegated, isDelegated, reDelegated }; virtual void collideWith( - CollideableGroup& other, DelegateState delegated = notDelegated) + CollideableGroup& other, DelegateState delegated = DelegateState::notDelegated) { other.collideWith(*this, nextDelegateState(delegated)); } virtual void collide() { }; // Check for internal collisions @@ -49,7 +49,7 @@ class CollideableGroup: public EnableWeakRefFromThis { static DelegateState nextDelegateState(DelegateState s) { int const result = static_cast(s) + 1; - if (s > reDelegated) + if (s > DelegateState::reDelegated) throw std::logic_error("cannot delegate a redelegated collideWith() call"); return static_cast(result); } @@ -71,7 +71,8 @@ class CollideableGroupGroup: public CollideableGroup { sf::Vector2f lineStart, sf::Vector2f lineEnd) override; virtual void collideWith( - CollideableGroup& other, DelegateState delegated = notDelegated) override; + CollideableGroup& other, + DelegateState delegated = DelegateState::notDelegated) override; virtual void collide() override; // Check for internal collisions diff --git a/src/collision/RectCollideableGroup.hpp b/src/collision/RectCollideableGroup.hpp index b2206e0..2b80f9b 100644 --- a/src/collision/RectCollideableGroup.hpp +++ b/src/collision/RectCollideableGroup.hpp @@ -22,7 +22,8 @@ class RectCollideableGroup: public CollideableGroup { virtual std::vector colliding( sf::FloatRect const&, Entity* e = nullptr) override; virtual void collideWith( - CollideableGroup& other, DelegateState delegated = notDelegated) override; + CollideableGroup& other, + DelegateState delegated = DelegateState::notDelegated) override; virtual std::vector colliding( sf::Vector2f lineStart, sf::Vector2f lineEnd) override; diff --git a/src/compsys/Entity.cpp b/src/compsys/Entity.cpp index bc50af0..00e80bd 100644 --- a/src/compsys/Entity.cpp +++ b/src/compsys/Entity.cpp @@ -8,13 +8,13 @@ Entity::Entity(): - m_state(created) + m_state(EntityState::created) { } Entity::~Entity() { - if (m_state == finished) { + if (m_state == EntityState::finished) { try { kill(); } catch (std::exception const& e) { LOG_E("Exception in Entity destructor: kill() threw:"); @@ -31,7 +31,7 @@ void Entity::add(Component& c) { if (c.m_parent) throw std::logic_error("cannot add component: it already has a parent"); - if (m_state != created) + if (m_state != EntityState::created) throw std::logic_error("attempt to add a component to an Entity in a wrong state"); if ((*this)[c.metaComponent()]) throw std::logic_error("only one component per type per Entity allowed"); @@ -42,11 +42,11 @@ void Entity::add(Component& c) void Entity::finish() { - if (m_state == finished) + if (m_state == EntityState::finished) return; - if (m_state != created) + if (m_state != EntityState::created) throw std::logic_error("attempt to finish an Entity in a wrong state"); - m_state = finished; + m_state = EntityState::finished; for (Component& c : m_components) c.initComponent(); } @@ -62,11 +62,11 @@ Component* Entity::operator[](MetaComponent const& meta) void Entity::kill() { - if (m_state == killed) + if (m_state == EntityState::killed) return; - if (m_state != finished) + if (m_state != EntityState::finished) throw std::logic_error("attempt to kill an Entity in a wrong state"); - m_state = killed; + m_state = EntityState::killed; for (Component& c : m_components) c.cleanupComponent(); } diff --git a/src/compsys/Entity.hpp b/src/compsys/Entity.hpp index 08be0e4..b2aa8f8 100644 --- a/src/compsys/Entity.hpp +++ b/src/compsys/Entity.hpp @@ -19,7 +19,7 @@ class MetaComponent; class Entity: public EnableWeakRefFromThis, private boost::noncopyable { public: - enum EntityState { created, finished, killed }; + enum class EntityState { created, finished, killed }; Entity(); ~Entity(); diff --git a/src/luaexport/EntitySystem.cpp b/src/luaexport/EntitySystem.cpp index 32e29ed..4d071c3 100644 --- a/src/luaexport/EntitySystem.cpp +++ b/src/luaexport/EntitySystem.cpp @@ -86,9 +86,9 @@ void init(LuaVm& vm) .LHPROPG(state) .LHISREFVALID .enum_("state") [ - value("CREATED", Entity::created), - value("FINISHED", Entity::finished), - value("KILLED", Entity::killed) + value("CREATED", Entity::EntityState::created), + value("FINISHED", Entity::EntityState::finished), + value("KILLED", Entity::EntityState::killed) ] .def("components", &getComponents, dependency(_1, result)) .def("component", &getComponent) diff --git a/src/luaexport/Logfile.cpp b/src/luaexport/Logfile.cpp index 1391218..1050378 100644 --- a/src/luaexport/Logfile.cpp +++ b/src/luaexport/Logfile.cpp @@ -8,7 +8,7 @@ char const libname[] = "Logfile"; #include "ExportThis.hpp" -static void writelog(lua_State* L, std::string const& msg, loglevel::T lv) +static void writelog(lua_State* L, std::string const& msg, loglevel lv) { // modified from luaL_where lua_Debug ar; diff --git a/src/luaexport/Tilemap.cpp b/src/luaexport/Tilemap.cpp index 8a60216..36dfdf4 100644 --- a/src/luaexport/Tilemap.cpp +++ b/src/luaexport/Tilemap.cpp @@ -75,10 +75,10 @@ static void init(LuaVm& vm) .LHPROPRW(relativePoints) .LHPROPG(absolutePoints) .enum_("t")[ - value("RECT", MapObject::rect), - value("TILE", MapObject::tile), - value("LINE", MapObject::line), - value("POLY", MapObject::poly) + value("RECT", MapObject::T::rect), + value("TILE", MapObject::T::tile), + value("LINE", MapObject::T::line), + value("POLY", MapObject::T::poly) ] .LHPROPRW(properties), # undef LHCURCLASS