Registry.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. virtual ~Registry() = default; //Required for child classes
  35. virtual const IEffectFactory * find(const std::string & name) const = 0;
  36. virtual void add(const std::string & name, FactoryPtr item) = 0;
  37. };
  38. class DLL_LINKAGE GlobalRegistry
  39. {
  40. GlobalRegistry() = default;
  41. public:
  42. static Registry * get();
  43. };
  44. template<typename E>
  45. class EffectFactory : public IEffectFactory
  46. {
  47. public:
  48. EffectFactory() = default;
  49. virtual ~EffectFactory() = default;
  50. Effect * create() const override
  51. {
  52. return new E();
  53. }
  54. };
  55. template<typename E>
  56. class RegisterEffect
  57. {
  58. public:
  59. RegisterEffect(const std::string & name)
  60. {
  61. auto f = std::make_shared<EffectFactory<E>>();
  62. GlobalRegistry::get()->add(name, f);
  63. }
  64. };
  65. }
  66. }
  67. VCMI_LIB_NAMESPACE_END