Registry.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Registry.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "Effect.h"
  12. #define VCMI_REGISTER_SPELL_EFFECT(Type, Name) \
  13. namespace\
  14. {\
  15. RegisterEffect<Type> register ## Type(Name);\
  16. }\
  17. \
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. namespace spells
  20. {
  21. namespace effects
  22. {
  23. class DLL_LINKAGE IEffectFactory
  24. {
  25. public:
  26. IEffectFactory() = default;
  27. virtual ~IEffectFactory() = default;
  28. virtual Effect * create() const = 0;
  29. };
  30. class DLL_LINKAGE Registry
  31. {
  32. public:
  33. using FactoryPtr = std::shared_ptr<IEffectFactory>;
  34. Registry();
  35. virtual ~Registry();
  36. virtual const IEffectFactory * find(const std::string & name) const = 0;
  37. virtual void add(const std::string & name, FactoryPtr item) = 0;
  38. };
  39. class DLL_LINKAGE GlobalRegistry
  40. {
  41. GlobalRegistry() = default;
  42. public:
  43. static Registry * get();
  44. };
  45. template<typename E>
  46. class EffectFactory : public IEffectFactory
  47. {
  48. public:
  49. EffectFactory() = default;
  50. virtual ~EffectFactory() = default;
  51. Effect * create() const override
  52. {
  53. return new E();
  54. }
  55. };
  56. template<typename E>
  57. class RegisterEffect
  58. {
  59. public:
  60. RegisterEffect(const std::string & name)
  61. {
  62. auto f = std::make_shared<EffectFactory<E>>();
  63. GlobalRegistry::get()->add(name, f);
  64. }
  65. };
  66. }
  67. }
  68. VCMI_LIB_NAMESPACE_END