Registry.h 1.3 KB

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