From 84af64ce6b66ee8697e923b56a190304937c0b60 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Sat, 8 Apr 2023 21:29:04 +0300 Subject: [PATCH] Removed magical initialization of registry via static variables --- client/LobbyClientNetPackVisitors.h | 4 +++- lib/spells/effects/Catapult.cpp | 4 ---- lib/spells/effects/Clone.cpp | 4 ---- lib/spells/effects/Damage.cpp | 4 ---- lib/spells/effects/DemonSummon.cpp | 4 ---- lib/spells/effects/Dispel.cpp | 4 ---- lib/spells/effects/Effect.h | 3 --- lib/spells/effects/Heal.cpp | 5 ---- lib/spells/effects/Obstacle.cpp | 4 ---- lib/spells/effects/Registry.cpp | 33 +++++++++++++++++++++++++++ lib/spells/effects/Registry.h | 18 --------------- lib/spells/effects/RemoveObstacle.cpp | 4 ---- lib/spells/effects/Sacrifice.cpp | 5 ---- lib/spells/effects/Summon.cpp | 5 ---- lib/spells/effects/Teleport.cpp | 6 ----- lib/spells/effects/Timed.cpp | 4 ---- 16 files changed, 36 insertions(+), 75 deletions(-) diff --git a/client/LobbyClientNetPackVisitors.h b/client/LobbyClientNetPackVisitors.h index 9290c8f0e..7ea105b02 100644 --- a/client/LobbyClientNetPackVisitors.h +++ b/client/LobbyClientNetPackVisitors.h @@ -12,7 +12,9 @@ #include "../lib/NetPackVisitor.h" class CClient; +VCMI_LIB_NAMESPACE_BEGIN class CGameState; +VCMI_LIB_NAMESPACE_END class ApplyOnLobbyHandlerNetPackVisitor : public VCMI_LIB_WRAP_NAMESPACE(ICPackVisitor) { @@ -53,4 +55,4 @@ public: virtual void visitLobbyStartGame(LobbyStartGame & pack) override; virtual void visitLobbyUpdateState(LobbyUpdateState & pack) override; virtual void visitLobbyShowMessage(LobbyShowMessage & pack) override; -}; \ No newline at end of file +}; diff --git a/lib/spells/effects/Catapult.cpp b/lib/spells/effects/Catapult.cpp index 2d0c8d24d..e6a3c0782 100644 --- a/lib/spells/effects/Catapult.cpp +++ b/lib/spells/effects/Catapult.cpp @@ -23,15 +23,11 @@ VCMI_LIB_NAMESPACE_BEGIN -static const std::string EFFECT_NAME = "core:catapult"; - namespace spells { namespace effects { -VCMI_REGISTER_SPELL_EFFECT(Catapult, EFFECT_NAME); - bool Catapult::applicable(Problem & problem, const Mechanics * m) const { const auto *town = m->battle()->battleGetDefendedTown(); diff --git a/lib/spells/effects/Clone.cpp b/lib/spells/effects/Clone.cpp index 7dbe418e5..dadfb4d04 100644 --- a/lib/spells/effects/Clone.cpp +++ b/lib/spells/effects/Clone.cpp @@ -19,15 +19,11 @@ VCMI_LIB_NAMESPACE_BEGIN -static const std::string EFFECT_NAME = "core:clone"; - namespace spells { namespace effects { -VCMI_REGISTER_SPELL_EFFECT(Clone, EFFECT_NAME); - void Clone::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const { for(const Destination & dest : target) diff --git a/lib/spells/effects/Damage.cpp b/lib/spells/effects/Damage.cpp index 0879d16bb..ccc727ec7 100644 --- a/lib/spells/effects/Damage.cpp +++ b/lib/spells/effects/Damage.cpp @@ -22,15 +22,11 @@ VCMI_LIB_NAMESPACE_BEGIN -static const std::string EFFECT_NAME = "core:damage"; - namespace spells { namespace effects { -VCMI_REGISTER_SPELL_EFFECT(Damage, EFFECT_NAME); - void Damage::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const { StacksInjured stacksInjured; diff --git a/lib/spells/effects/DemonSummon.cpp b/lib/spells/effects/DemonSummon.cpp index 274b4be70..40ec769cc 100644 --- a/lib/spells/effects/DemonSummon.cpp +++ b/lib/spells/effects/DemonSummon.cpp @@ -19,15 +19,11 @@ VCMI_LIB_NAMESPACE_BEGIN -static const std::string EFFECT_NAME = "core:demonSummon"; - namespace spells { namespace effects { -VCMI_REGISTER_SPELL_EFFECT(DemonSummon, EFFECT_NAME); - void DemonSummon::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const { BattleUnitsChanged pack; diff --git a/lib/spells/effects/Dispel.cpp b/lib/spells/effects/Dispel.cpp index 94836900f..88a437d12 100644 --- a/lib/spells/effects/Dispel.cpp +++ b/lib/spells/effects/Dispel.cpp @@ -23,15 +23,11 @@ VCMI_LIB_NAMESPACE_BEGIN -static const std::string EFFECT_NAME = "core:dispel"; - namespace spells { namespace effects { -VCMI_REGISTER_SPELL_EFFECT(Dispel, EFFECT_NAME); - void Dispel::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const { const bool describe = server->describeChanges(); diff --git a/lib/spells/effects/Effect.h b/lib/spells/effects/Effect.h index 8e35945df..d70b6ec7a 100644 --- a/lib/spells/effects/Effect.h +++ b/lib/spells/effects/Effect.h @@ -35,9 +35,6 @@ class Effects; class Effect; class Registry; -template -class RegisterEffect; - using TargetType = spells::AimType; class DLL_LINKAGE Effect diff --git a/lib/spells/effects/Heal.cpp b/lib/spells/effects/Heal.cpp index 1354e8b0d..894b3f592 100644 --- a/lib/spells/effects/Heal.cpp +++ b/lib/spells/effects/Heal.cpp @@ -22,16 +22,11 @@ VCMI_LIB_NAMESPACE_BEGIN - -static const std::string EFFECT_NAME = "core:heal"; - namespace spells { namespace effects { -VCMI_REGISTER_SPELL_EFFECT(Heal, EFFECT_NAME); - void Heal::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const { apply(m->getEffectValue(), server, m, target); diff --git a/lib/spells/effects/Obstacle.cpp b/lib/spells/effects/Obstacle.cpp index 36b60d908..1a8c7b7a2 100644 --- a/lib/spells/effects/Obstacle.cpp +++ b/lib/spells/effects/Obstacle.cpp @@ -21,15 +21,11 @@ VCMI_LIB_NAMESPACE_BEGIN -static const std::string EFFECT_NAME = "core:obstacle"; - namespace spells { namespace effects { -VCMI_REGISTER_SPELL_EFFECT(Obstacle, EFFECT_NAME); - using RelativeShape = std::vector>; static void serializeRelativeShape(JsonSerializeFormat & handler, const std::string & fieldName, RelativeShape & value) diff --git a/lib/spells/effects/Registry.cpp b/lib/spells/effects/Registry.cpp index 0377c55c7..82e3b1c48 100644 --- a/lib/spells/effects/Registry.cpp +++ b/lib/spells/effects/Registry.cpp @@ -11,6 +11,23 @@ #include "Registry.h" +#include "Catapult.h" +#include "Clone.h" +#include "Damage.h" +#include "DemonSummon.h" +#include "Dispel.h" +#include "Effect.h" +#include "Effects.h" +#include "Heal.h" +#include "LocationEffect.h" +#include "Obstacle.h" +#include "RemoveObstacle.h" +#include "Sacrifice.h" +#include "Summon.h" +#include "Teleport.h" +#include "Timed.h" +#include "UnitEffect.h" + VCMI_LIB_NAMESPACE_BEGIN namespace spells @@ -23,6 +40,22 @@ namespace detail class RegistryImpl : public Registry { public: + RegistryImpl() + { + add("core:catapult", std::make_shared>()); + add("core:clone", std::make_shared>()); + add("core:damage", std::make_shared>()); + add("core:demonSummon", std::make_shared>()); + add("core:dispel", std::make_shared>()); + add("core:heal", std::make_shared>()); + add("core:obstacle", std::make_shared>()); + add("core:removeObstacle", std::make_shared>()); + add("core:sacrifice", std::make_shared>()); + add("core:summon", std::make_shared>()); + add("core:teleport", std::make_shared>()); + add("core:timed", std::make_shared>()); + } + const IEffectFactory * find(const std::string & name) const override { auto iter = data.find(name); diff --git a/lib/spells/effects/Registry.h b/lib/spells/effects/Registry.h index 912cced73..dc56eeb4b 100644 --- a/lib/spells/effects/Registry.h +++ b/lib/spells/effects/Registry.h @@ -12,13 +12,6 @@ #include "Effect.h" -#define VCMI_REGISTER_SPELL_EFFECT(Type, Name) \ -namespace\ -{\ -RegisterEffect register ## Type(Name);\ -}\ -\ - VCMI_LIB_NAMESPACE_BEGIN namespace spells @@ -60,17 +53,6 @@ public: } }; -template -class RegisterEffect -{ -public: - RegisterEffect(const std::string & name) - { - auto f = std::make_shared>(); - GlobalRegistry::get()->add(name, f); - } -}; - } } diff --git a/lib/spells/effects/RemoveObstacle.cpp b/lib/spells/effects/RemoveObstacle.cpp index c268fd8c8..68732ea8d 100644 --- a/lib/spells/effects/RemoveObstacle.cpp +++ b/lib/spells/effects/RemoveObstacle.cpp @@ -22,15 +22,11 @@ VCMI_LIB_NAMESPACE_BEGIN -static const std::string EFFECT_NAME = "core:removeObstacle"; - namespace spells { namespace effects { -VCMI_REGISTER_SPELL_EFFECT(RemoveObstacle, EFFECT_NAME); - bool RemoveObstacle::applicable(Problem & problem, const Mechanics * m) const { if (getTargets(m, EffectTarget(), true).empty()) diff --git a/lib/spells/effects/Sacrifice.cpp b/lib/spells/effects/Sacrifice.cpp index 4ca9ca73a..704b93576 100644 --- a/lib/spells/effects/Sacrifice.cpp +++ b/lib/spells/effects/Sacrifice.cpp @@ -21,16 +21,11 @@ VCMI_LIB_NAMESPACE_BEGIN - -static const std::string EFFECT_NAME = "core:sacrifice"; - namespace spells { namespace effects { -VCMI_REGISTER_SPELL_EFFECT(Sacrifice, EFFECT_NAME); - void Sacrifice::adjustTargetTypes(std::vector & types) const { if(!types.empty()) diff --git a/lib/spells/effects/Summon.cpp b/lib/spells/effects/Summon.cpp index 0b18fcc0e..18e700ff0 100644 --- a/lib/spells/effects/Summon.cpp +++ b/lib/spells/effects/Summon.cpp @@ -24,16 +24,11 @@ VCMI_LIB_NAMESPACE_BEGIN - -static const std::string EFFECT_NAME = "core:summon"; - namespace spells { namespace effects { -VCMI_REGISTER_SPELL_EFFECT(Summon, EFFECT_NAME); - void Summon::adjustAffectedHexes(std::set & hexes, const Mechanics * m, const Target & spellTarget) const { //no hexes affected diff --git a/lib/spells/effects/Teleport.cpp b/lib/spells/effects/Teleport.cpp index eb4a5e9e0..af7607460 100644 --- a/lib/spells/effects/Teleport.cpp +++ b/lib/spells/effects/Teleport.cpp @@ -18,16 +18,10 @@ VCMI_LIB_NAMESPACE_BEGIN -//TODO: Teleport effect - -static const std::string EFFECT_NAME = "core:teleport"; - namespace spells { namespace effects { -VCMI_REGISTER_SPELL_EFFECT(Teleport, EFFECT_NAME); - void Teleport::adjustTargetTypes(std::vector & types) const { diff --git a/lib/spells/effects/Timed.cpp b/lib/spells/effects/Timed.cpp index 83a795315..b00f4a2dd 100644 --- a/lib/spells/effects/Timed.cpp +++ b/lib/spells/effects/Timed.cpp @@ -20,15 +20,11 @@ VCMI_LIB_NAMESPACE_BEGIN -static const std::string EFFECT_NAME = "core:timed"; - namespace spells { namespace effects { -VCMI_REGISTER_SPELL_EFFECT(Timed, EFFECT_NAME); - static void describeEffect(std::vector & log, const Mechanics * m, const std::vector & bonuses, const battle::Unit * target) { auto addLogLine = [&](const int32_t baseTextID, const boost::logic::tribool & plural)