IdentifierStorage.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * IdentifierStorage.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 that stores all object identifiers strings and maps them to numeric ID's
  14. /// if possible, objects ID's should be in format <type>.<name>, camelCase e.g. "creature.grandElf"
  15. class DLL_LINKAGE CIdentifierStorage
  16. {
  17. enum class ELoadingState
  18. {
  19. LOADING,
  20. FINALIZING,
  21. FINISHED
  22. };
  23. struct ObjectCallback // entry created on ID request
  24. {
  25. std::string localScope; /// scope from which this ID was requested
  26. std::string remoteScope; /// scope in which this object must be found
  27. std::string type; /// type, e.g. creature, faction, hero, etc
  28. std::string name; /// string ID
  29. std::function<void(si32)> callback;
  30. bool optional;
  31. /// Builds callback from identifier in form "targetMod:type.name"
  32. static ObjectCallback fromNameWithType(const std::string & scope, const std::string & fullName, const std::function<void(si32)> & callback, bool optional);
  33. /// Builds callback from identifier in form "targetMod:name"
  34. static ObjectCallback fromNameAndType(const std::string & scope, const std::string & type, const std::string & fullName, const std::function<void(si32)> & callback, bool optional);
  35. private:
  36. ObjectCallback() = default;
  37. };
  38. struct ObjectData // entry created on ID registration
  39. {
  40. si32 id;
  41. std::string scope; /// scope in which this ID located
  42. bool operator==(const ObjectData & other) const
  43. {
  44. return id == other.id && scope == other.scope;
  45. }
  46. template <typename Handler> void serialize(Handler &h, const int version)
  47. {
  48. h & id;
  49. h & scope;
  50. }
  51. };
  52. std::multimap<std::string, ObjectData> registeredObjects;
  53. mutable std::vector<ObjectCallback> scheduledRequests;
  54. ELoadingState state = ELoadingState::LOADING;
  55. /// Check if identifier can be valid (camelCase, point as separator)
  56. static void checkIdentifier(std::string & ID);
  57. void requestIdentifier(ObjectCallback callback) const;
  58. bool resolveIdentifier(const ObjectCallback & callback) const;
  59. std::vector<ObjectData> getPossibleIdentifiers(const ObjectCallback & callback) const;
  60. public:
  61. CIdentifierStorage() = default;
  62. virtual ~CIdentifierStorage() = default;
  63. /// request identifier for specific object name.
  64. /// Function callback will be called during ID resolution phase of loading
  65. void requestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback) const;
  66. ///fullName = [remoteScope:]type.name
  67. void requestIdentifier(const std::string & scope, const std::string & fullName, const std::function<void(si32)> & callback) const;
  68. void requestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const;
  69. void requestIdentifier(const JsonNode & name, const std::function<void(si32)> & callback) const;
  70. /// try to request ID. If ID with such name won't be loaded, callback function will not be called
  71. void tryRequestIdentifier(const std::string & scope, const std::string & type, const std::string & name, const std::function<void(si32)> & callback) const;
  72. void tryRequestIdentifier(const std::string & type, const JsonNode & name, const std::function<void(si32)> & callback) const;
  73. /// get identifier immediately. If identifier is not know and not silent call will result in error message
  74. std::optional<si32> getIdentifier(const std::string & scope, const std::string & type, const std::string & name, bool silent = false) const;
  75. std::optional<si32> getIdentifier(const std::string & type, const JsonNode & name, bool silent = false) const;
  76. std::optional<si32> getIdentifier(const JsonNode & name, bool silent = false) const;
  77. std::optional<si32> getIdentifier(const std::string & scope, const std::string & fullName, bool silent = false) const;
  78. /// registers new object
  79. void registerObject(const std::string & scope, const std::string & type, const std::string & name, si32 identifier);
  80. /// called at the very end of loading to check for any missing ID's
  81. void finalize();
  82. template <typename Handler> void serialize(Handler &h, const int version)
  83. {
  84. h & registeredObjects;
  85. h & state;
  86. }
  87. };
  88. VCMI_LIB_NAMESPACE_END