From c25141f02a5b3dcddcc93ca5d2bd2fcdad5efb31 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 6 May 2015 18:37:06 +0300 Subject: Add strong typed bool type Enable. --- src/being/actorsprite.cpp | 30 ++++++++++++++++++------------ src/being/actorsprite.h | 6 ++++-- src/enums/simpletypes.h | 1 + src/gui/widgets/inttextfield.cpp | 4 ++-- src/gui/widgets/inttextfield.h | 4 +++- src/gui/widgets/setupitem.cpp | 2 +- src/net/eathena/beinghandler.cpp | 8 +++++--- src/net/tmwa/beinghandler.cpp | 3 ++- src/statuseffect.cpp | 11 +++++++---- src/statuseffect.h | 6 ++++-- 10 files changed, 47 insertions(+), 28 deletions(-) diff --git a/src/being/actorsprite.cpp b/src/being/actorsprite.cpp index 88418be1a..b305b35a7 100644 --- a/src/being/actorsprite.cpp +++ b/src/being/actorsprite.cpp @@ -120,9 +120,9 @@ void ActorSprite::logic() FOR_EACH (std::set::const_iterator, it, mStatusEffects) { const StatusEffect *const effect - = StatusEffect::getStatusEffect(*it, true); + = StatusEffect::getStatusEffect(*it, Enable_true); if (effect && effect->particleEffectIsPersistent()) - updateStatusEffect(*it, true); + updateStatusEffect(*it, Enable_true); } } @@ -174,14 +174,15 @@ struct EffectDescription final std::string mSFXEffect; }; -void ActorSprite::setStatusEffect(const int index, const bool active) +void ActorSprite::setStatusEffect(const int index, const Enable active) { - const bool wasActive = mStatusEffects.find(index) != mStatusEffects.end(); + const Enable wasActive = fromBool( + mStatusEffects.find(index) != mStatusEffects.end(), Enable); if (active != wasActive) { updateStatusEffect(index, active); - if (active) + if (active == Enable_true) mStatusEffects.insert(index); else mStatusEffects.erase(index); @@ -197,28 +198,33 @@ void ActorSprite::setStatusEffectBlock(const int offset, offset + i); if (index != -1) - setStatusEffect(index, (newEffects & (1 << i)) > 0); + { + setStatusEffect(index, + fromBool((newEffects & (1 << i)) > 0, Enable)); + } } } void ActorSprite::updateStunMode(const int oldMode, const int newMode) { - handleStatusEffect(StatusEffect::getStatusEffect(oldMode, false), -1); - handleStatusEffect(StatusEffect::getStatusEffect(newMode, true), -1); + handleStatusEffect(StatusEffect::getStatusEffect( + oldMode, Enable_false), -1); + handleStatusEffect(StatusEffect::getStatusEffect( + newMode, Enable_true), -1); } -void ActorSprite::updateStatusEffect(const int index, const bool newStatus) +void ActorSprite::updateStatusEffect(const int index, const Enable newStatus) { StatusEffect *const effect = StatusEffect::getStatusEffect( index, newStatus); if (!effect) return; if (effect->isPoison() && getType() == ActorType::Player) - setPoison(newStatus); + setPoison(newStatus == Enable_true); else if (effect->isCart() && localPlayer == this) - setHaveCart(newStatus); + setHaveCart(newStatus == Enable_true); else if (effect->isRiding()) - setRiding(newStatus); + setRiding(newStatus == Enable_true); handleStatusEffect(effect, index); } diff --git a/src/being/actorsprite.h b/src/being/actorsprite.h index 7b5da3aa7..f5d4f0214 100644 --- a/src/being/actorsprite.h +++ b/src/being/actorsprite.h @@ -22,6 +22,8 @@ #ifndef BEING_ACTORSPRITE_H #define BEING_ACTORSPRITE_H +#include "enums/simpletypes.h" + #include "resources/map/blocktype.h" #include "resources/map/mapconsts.h" @@ -118,7 +120,7 @@ class ActorSprite notfinal : public CompoundSprite, public Actor mStunMode = stunMode; } - void setStatusEffect(const int index, const bool active); + void setStatusEffect(const int index, const Enable active); /** * A status effect block is a 16 bit mask of status effects. We assign @@ -186,7 +188,7 @@ class ActorSprite notfinal : public CompoundSprite, public Actor * Notify self that a status effect has flipped. * The new flag is passed. */ - virtual void updateStatusEffect(const int index, const bool newStatus); + virtual void updateStatusEffect(const int index, const Enable newStatus); /** * Handle an update to a status or stun effect diff --git a/src/enums/simpletypes.h b/src/enums/simpletypes.h index c6b3d26a6..698e5a531 100644 --- a/src/enums/simpletypes.h +++ b/src/enums/simpletypes.h @@ -62,5 +62,6 @@ defBoolEnum(Modifiable); defBoolEnum(AllPlayers); defBoolEnum(AllowSort); defBoolEnum(NpcNames); +defBoolEnum(Enable); #endif // ENUMS_SIMPLETYPES_H diff --git a/src/gui/widgets/inttextfield.cpp b/src/gui/widgets/inttextfield.cpp index d198fa9b8..094f2e50c 100644 --- a/src/gui/widgets/inttextfield.cpp +++ b/src/gui/widgets/inttextfield.cpp @@ -35,7 +35,7 @@ IntTextField::IntTextField(const Widget2 *const widget, const int def, const int min, const int max, - const bool enabled, + const Enable enabled, const int width) : TextField(widget, toString(def)), mMin(0), @@ -46,7 +46,7 @@ IntTextField::IntTextField(const Widget2 *const widget, if (min != 0 || max != 0) setRange(min, max); - setEnabled(enabled); + setEnabled(enabled == Enable_true); if (width != 0) setWidth(width); } diff --git a/src/gui/widgets/inttextfield.h b/src/gui/widgets/inttextfield.h index 979f3a886..04f34b393 100644 --- a/src/gui/widgets/inttextfield.h +++ b/src/gui/widgets/inttextfield.h @@ -23,6 +23,8 @@ #ifndef GUI_WIDGETS_INTTEXTFIELD_H #define GUI_WIDGETS_INTTEXTFIELD_H +#include "enums/simpletypes.h" + #include "gui/widgets/textfield.h" /** @@ -38,7 +40,7 @@ class IntTextField final : public TextField const int def = 0, const int min = 0, const int max = 0, - const bool enabled = true, + const Enable enabled = Enable_true, const int width = 0); A_DELETE_COPY(IntTextField) diff --git a/src/gui/widgets/setupitem.cpp b/src/gui/widgets/setupitem.cpp index 6cb807ab5..b02f2b9ba 100644 --- a/src/gui/widgets/setupitem.cpp +++ b/src/gui/widgets/setupitem.cpp @@ -496,7 +496,7 @@ void SetupItemIntTextField::createControls() mLabel = new Label(this, mText); mLabel->setToolTip(mDescription); mTextField = new IntTextField(this, atoi(mValue.c_str()), - mMin, mMax, true, 30); + mMin, mMax, Enable_true, 30); mTextField->setActionEventId(mEventName); mTextField->addActionListener(mParent); diff --git a/src/net/eathena/beinghandler.cpp b/src/net/eathena/beinghandler.cpp index 1c913a691..93733d2fa 100644 --- a/src/net/eathena/beinghandler.cpp +++ b/src/net/eathena/beinghandler.cpp @@ -1176,7 +1176,8 @@ void BeingHandler::processBeingStatusChange(Net::MessageIn &msg) // Status change const uint16_t status = msg.readInt16("status"); const int id = msg.readInt32("being id"); - const bool flag = msg.readUInt8("flag: 0: stop, 1: start"); + const Enable flag = fromBool( + msg.readUInt8("flag: 0: stop, 1: start"), Enable); msg.readInt32("total"); msg.readInt32("left"); msg.readInt32("val1"); @@ -1201,7 +1202,8 @@ void BeingHandler::processBeingStatusChange2(Net::MessageIn &msg) // Status change const uint16_t status = msg.readInt16("status"); const int id = msg.readInt32("being id"); - const bool flag = msg.readUInt8("flag: 0: stop, 1: start"); + const Enable flag = fromBool( + msg.readUInt8("flag: 0: stop, 1: start"), Enable); msg.readInt32("left"); msg.readInt32("val1"); msg.readInt32("val2"); @@ -1613,7 +1615,7 @@ void BeingHandler::processPlaterStatusChangeNoTick(Net::MessageIn &msg) { const uint16_t status = msg.readInt16("index"); const int id = msg.readInt32("account id"); - const bool flag = msg.readUInt8("state") ? 1 : 0; + const Enable flag = fromBool(msg.readUInt8("state") ? true : false, Enable); Being *const dstBeing = actorManager->findBeing(id); if (!dstBeing) diff --git a/src/net/tmwa/beinghandler.cpp b/src/net/tmwa/beinghandler.cpp index 580b4b3f2..8277e4805 100644 --- a/src/net/tmwa/beinghandler.cpp +++ b/src/net/tmwa/beinghandler.cpp @@ -1329,7 +1329,8 @@ void BeingHandler::processBeingStatusChange(Net::MessageIn &msg) // Status change const uint16_t status = msg.readInt16("status"); const int id = msg.readInt32("being id"); - const bool flag = msg.readUInt8("flag: 0: stop, 1: start"); + const Enable flag = fromBool( + msg.readUInt8("flag: 0: stop, 1: start"), Enable); Being *const dstBeing = actorManager->findBeing(id); if (dstBeing) diff --git a/src/statuseffect.cpp b/src/statuseffect.cpp index d4083eac4..16936ec2b 100644 --- a/src/statuseffect.cpp +++ b/src/statuseffect.cpp @@ -115,9 +115,10 @@ int StatusEffect::blockEffectIndexToEffectIndex(const int blockIndex) } StatusEffect *StatusEffect::getStatusEffect(const int index, - const bool enabling) + const Enable enabling) { - std::map &effects = statusEffects[enabling]; + std::map &effects + = statusEffects[enabling == Enable_true]; const std::map::iterator it = effects.find(index); if (it != effects.end()) return (*it).second; @@ -125,9 +126,11 @@ StatusEffect *StatusEffect::getStatusEffect(const int index, return nullptr; } -StatusEffect *StatusEffect::getStunEffect(const int index, const bool enabling) +StatusEffect *StatusEffect::getStunEffect(const int index, + const Enable enabling) { - std::map &effects = stunEffects[enabling]; + std::map &effects + = stunEffects[enabling == Enable_true]; const std::map::iterator it = effects.find(index); if (it != effects.end()) return (*it).second; diff --git a/src/statuseffect.h b/src/statuseffect.h index ce99fa5d8..bd7da5f8d 100644 --- a/src/statuseffect.h +++ b/src/statuseffect.h @@ -23,6 +23,8 @@ #ifndef STATUSEFFECT_H #define STATUSEFFECT_H +#include "enums/simpletypes.h" + #include #include "localconsts.h" @@ -91,7 +93,7 @@ class StatusEffect final * the deactivating effect (false). */ static StatusEffect *getStatusEffect(const int index, - const bool enabling) + const Enable enabling) A_WARN_UNUSED; /** @@ -102,7 +104,7 @@ class StatusEffect final * the deactivating effect (false). */ static StatusEffect *getStunEffect(const int index, - const bool enabling) A_WARN_UNUSED; + const Enable enabling) A_WARN_UNUSED; /** * Maps a block effect index to its corresponding effect index. Block -- cgit v1.2.3-70-g09d2