Registry.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. namespace spells
  19. {
  20. namespace effects
  21. {
  22. class IEffectFactory
  23. {
  24. public:
  25. IEffectFactory() = default;
  26. virtual ~IEffectFactory() = default;
  27. virtual Effect * create() const = 0;
  28. };
  29. class Registry
  30. {
  31. public:
  32. virtual ~Registry();
  33. virtual const IEffectFactory * find(const std::string & name) const = 0;
  34. virtual void add(const std::string & name, IEffectFactory * item) = 0;
  35. static Registry * get();
  36. protected:
  37. Registry();
  38. };
  39. template<typename E>
  40. class EffectFactory : public IEffectFactory
  41. {
  42. public:
  43. EffectFactory() = default;
  44. virtual ~EffectFactory() = default;
  45. Effect * create() const override
  46. {
  47. return new E();
  48. }
  49. };
  50. template<typename E>
  51. class RegisterEffect
  52. {
  53. public:
  54. RegisterEffect(const std::string & name)
  55. {
  56. IEffectFactory * f = new EffectFactory<E>();
  57. Registry::get()->add(name, f);
  58. }
  59. };
  60. }
  61. }