Skip to content

Commit

Permalink
Use C++11 scoped enums.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oberon00 committed Jun 27, 2013
1 parent e97160a commit f894c30
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 59 deletions.
27 changes: 15 additions & 12 deletions src/Logfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_)
{
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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<const char* const, loglevel::max> levelnames =
auto level = static_cast<int>(level_);

static auto const maxloglevel = static_cast<int>(loglevel::max);
static_assert(maxloglevel == 5, "Please correct levelnames below!");
static const std::array<const char* const, maxloglevel> levelnames =
{ "debug", "info", "warning", "error", "fatal" };
static const std::array<const char* const, loglevel::max> levelpreambles =
static const std::array<const char* const, maxloglevel> levelpreambles =
{ " ", " _", " + ", " * ", "!>>>" };

std::string location_;
Expand Down Expand Up @@ -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);
}
26 changes: 13 additions & 13 deletions src/Logfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,36 @@
#include <fstream>


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;
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/MapInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,16 @@ MapInfo loadTilemap(jd::Tilemap& tm, std::string const& vfilename)
o.properties = loadPropertiesOptional(obj.second);
o.tileId = obj.second.get("<xmlattr>.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 != "<xmlattr>") {
Expand Down
4 changes: 2 additions & 2 deletions src/MapInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
typedef std::unordered_map<std::string, std::string> 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;
Expand Down
7 changes: 4 additions & 3 deletions src/Tilemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace jd {

Tileset::Tileset():
m_texture(nullptr),
m_ordering(linewiseOrdered)
m_ordering(TileOrdering::linewiseOrdered)
{
}

Expand All @@ -32,15 +32,16 @@ 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);
}

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 {
Expand Down
4 changes: 2 additions & 2 deletions src/Tilemap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ typedef sf::Vector3<unsigned> Vector3u;
/* const */ class Tileset
{
public:
enum TileOrdering { linewiseOrdered, columnwiseOrdered };
enum class TileOrdering { linewiseOrdered, columnwiseOrdered };

Tileset();
Tileset(
sf::Vector2u size,
ResourceTraits<sf::Texture>::Ptr texture,
TileOrdering ordering = linewiseOrdered);
TileOrdering ordering = TileOrdering::linewiseOrdered);

sf::Vector2u position(unsigned index) const;
ResourceTraits<sf::Texture>::Ptr texture() const { return m_texture; }
Expand Down
9 changes: 5 additions & 4 deletions src/collision/Collisions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class CollideableGroup: public EnableWeakRefFromThis<CollideableGroup> {
// 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
Expand All @@ -49,7 +49,7 @@ class CollideableGroup: public EnableWeakRefFromThis<CollideableGroup> {
static DelegateState nextDelegateState(DelegateState s)
{
int const result = static_cast<int>(s) + 1;
if (s > reDelegated)
if (s > DelegateState::reDelegated)
throw std::logic_error("cannot delegate a redelegated collideWith() call");
return static_cast<DelegateState>(result);
}
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/collision/RectCollideableGroup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class RectCollideableGroup: public CollideableGroup {
virtual std::vector<Collision> 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<Collision> colliding(
sf::Vector2f lineStart, sf::Vector2f lineEnd) override;
Expand Down
18 changes: 9 additions & 9 deletions src/compsys/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:");
Expand All @@ -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");
Expand All @@ -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();
}
Expand All @@ -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();
}
2 changes: 1 addition & 1 deletion src/compsys/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MetaComponent;

class Entity: public EnableWeakRefFromThis<Entity>, private boost::noncopyable {
public:
enum EntityState { created, finished, killed };
enum class EntityState { created, finished, killed };

Entity();
~Entity();
Expand Down
6 changes: 3 additions & 3 deletions src/luaexport/EntitySystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/luaexport/Logfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/luaexport/Tilemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f894c30

Please sign in to comment.