IHandlerBase.h 4.1 KB

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