瀏覽代碼

Removed magical initialization of registry via static variables

Ivan Savenko 2 年之前
父節點
當前提交
84af64ce6b

+ 3 - 1
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;
-};
+};

+ 0 - 4
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();

+ 0 - 4
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)

+ 0 - 4
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;

+ 0 - 4
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;

+ 0 - 4
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();

+ 0 - 3
lib/spells/effects/Effect.h

@@ -35,9 +35,6 @@ class Effects;
 class Effect;
 class Registry;
 
-template<typename F>
-class RegisterEffect;
-
 using TargetType = spells::AimType;
 
 class DLL_LINKAGE Effect

+ 0 - 5
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);

+ 0 - 4
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<std::vector<BattleHex::EDir>>;
 
 static void serializeRelativeShape(JsonSerializeFormat & handler, const std::string & fieldName, RelativeShape & value)

+ 33 - 0
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<EffectFactory<Catapult>>());
+		add("core:clone", std::make_shared<EffectFactory<Clone>>());
+		add("core:damage", std::make_shared<EffectFactory<Damage>>());
+		add("core:demonSummon", std::make_shared<EffectFactory<DemonSummon>>());
+		add("core:dispel", std::make_shared<EffectFactory<Dispel>>());
+		add("core:heal", std::make_shared<EffectFactory<Heal>>());
+		add("core:obstacle", std::make_shared<EffectFactory<Obstacle>>());
+		add("core:removeObstacle", std::make_shared<EffectFactory<RemoveObstacle>>());
+		add("core:sacrifice", std::make_shared<EffectFactory<Sacrifice>>());
+		add("core:summon", std::make_shared<EffectFactory<Summon>>());
+		add("core:teleport", std::make_shared<EffectFactory<Teleport>>());
+		add("core:timed", std::make_shared<EffectFactory<Timed>>());
+	}
+
 	const IEffectFactory * find(const std::string & name) const override
 	{
 		auto iter = data.find(name);

+ 0 - 18
lib/spells/effects/Registry.h

@@ -12,13 +12,6 @@
 
 #include "Effect.h"
 
-#define VCMI_REGISTER_SPELL_EFFECT(Type, Name) \
-namespace\
-{\
-RegisterEffect<Type> register ## Type(Name);\
-}\
-\
-
 VCMI_LIB_NAMESPACE_BEGIN
 
 namespace spells
@@ -60,17 +53,6 @@ public:
 	}
 };
 
-template<typename E>
-class RegisterEffect
-{
-public:
-	RegisterEffect(const std::string & name)
-	{
-		auto f = std::make_shared<EffectFactory<E>>();
-		GlobalRegistry::get()->add(name, f);
-	}
-};
-
 }
 }
 

+ 0 - 4
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())

+ 0 - 5
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<TargetType> & types) const
 {
 	if(!types.empty())

+ 0 - 5
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<BattleHex> & hexes, const Mechanics * m, const Target & spellTarget) const
 {
 	//no hexes affected

+ 0 - 6
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<TargetType> & types) const
 {

+ 0 - 4
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<MetaString> & log, const Mechanics * m, const std::vector<Bonus> & bonuses, const battle::Unit * target) 
 {
 	auto addLogLine = [&](const int32_t baseTextID, const boost::logic::tribool & plural)