IHandlerBase.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * IHandlerBase.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. VCMI_LIB_NAMESPACE_BEGIN
  12. class JsonNode;
  13. class Entity;
  14. /// base class for all handlers that can be accessed from mod system
  15. class DLL_LINKAGE IHandlerBase : boost::noncopyable
  16. {
  17. protected:
  18. static std::string getScopeBuiltin();
  19. /// Calls modhandler. Mostly needed to avoid large number of includes in headers
  20. void registerObject(const std::string & scope, const std::string & typeName, const std::string & name, const JsonNode & data, si32 index);
  21. void registerObject(const std::string & scope, const std::vector<std::string> & typeNames, const std::string & name, const JsonNode & data, si32 index);
  22. public:
  23. /// loads all original game data in vector of json nodes
  24. /// dataSize - is number of items that must be loaded (normally - constant from GameConstants)
  25. virtual std::vector<JsonNode> loadLegacyData() = 0;
  26. /// loads single object into game. Scope is namespace of this object, same as name of source mod
  27. virtual void loadObject(std::string scope, std::string name, const JsonNode & data) = 0;
  28. virtual void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) = 0;
  29. /// allows handlers to alter object configuration before validation and actual load
  30. virtual void beforeValidate(JsonNode & object){};
  31. /// allows handler to load some custom internal data before identifier finalization
  32. virtual void loadCustom(){};
  33. /// allows handler to do post-loading step for validation or integration of loaded data
  34. virtual void afterLoadFinalization(){};
  35. virtual ~IHandlerBase() = default;
  36. };
  37. template <class _ObjectID, class _ObjectBase, class _Object, class _ServiceBase>
  38. class CHandlerBase : public _ServiceBase, public IHandlerBase
  39. {
  40. const _Object * getObjectImpl(const int32_t index) const
  41. {
  42. try
  43. {
  44. return objects.at(index).get();
  45. }
  46. catch (const std::out_of_range&)
  47. {
  48. logMod->error("%s id %d is invalid", getTypeNames()[0], index);
  49. throw std::runtime_error("Attempt to access invalid index " + std::to_string(index) + " of type " + getTypeNames().front());
  50. }
  51. }
  52. public:
  53. using ObjectPtr = std::shared_ptr<_Object>;
  54. const Entity * getBaseByIndex(const int32_t index) const override
  55. {
  56. return getObjectImpl(index);
  57. }
  58. const _ObjectBase * getById(const _ObjectID & id) const override
  59. {
  60. return getObjectImpl(id.getNum());
  61. }
  62. const _ObjectBase * getByIndex(const int32_t index) const override
  63. {
  64. return getObjectImpl(index);
  65. }
  66. void forEachBase(const std::function<void(const Entity * entity, bool & stop)> & cb) const override
  67. {
  68. forEachT(cb);
  69. }
  70. void forEach(const std::function<void(const _ObjectBase * entity, bool & stop)> & cb) const override
  71. {
  72. forEachT(cb);
  73. }
  74. void loadObject(std::string scope, std::string name, const JsonNode & data) override
  75. {
  76. objects.push_back(loadFromJson(scope, data, name, objects.size()));
  77. registerObject(scope, getTypeNames(), name, data, objects.back()->getIndex());
  78. }
  79. void loadObject(std::string scope, std::string name, const JsonNode & data, size_t index) override
  80. {
  81. assert(objects[index] == nullptr); // ensure that this id was not loaded before
  82. objects[index] = loadFromJson(scope, data, name, index);
  83. registerObject(scope, getTypeNames(), name, data, index);
  84. }
  85. const _Object * operator[] (const _ObjectID id) const
  86. {
  87. return getObjectImpl(id.getNum());
  88. }
  89. const _Object * operator[] (int32_t index) const
  90. {
  91. return getObjectImpl(index);
  92. }
  93. size_t size() const
  94. {
  95. return objects.size();
  96. }
  97. protected:
  98. virtual ObjectPtr loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index) = 0;
  99. virtual const std::vector<std::string> & getTypeNames() const = 0;
  100. template<typename ItemType>
  101. void forEachT(const std::function<void(const ItemType *, bool &)> & cb) const
  102. {
  103. bool stop = false;
  104. for(auto & object : objects)
  105. {
  106. cb(object.get(), stop);
  107. if(stop)
  108. break;
  109. }
  110. }
  111. public: //todo: make private
  112. std::vector<ObjectPtr> objects;
  113. };
  114. VCMI_LIB_NAMESPACE_END