MetaIdentifier.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * MetaIdentifier.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 "IdentifierBase.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. /// This class represents field that may contain value of multiple different identifer types
  14. class DLL_LINKAGE MetaIdentifier
  15. {
  16. std::string entityType;
  17. std::string stringForm;
  18. int32_t integerForm;
  19. void onDeserialized();
  20. public:
  21. static const MetaIdentifier NONE;
  22. MetaIdentifier();
  23. MetaIdentifier(const std::string & entityType, const std::string & identifier);
  24. MetaIdentifier(const std::string & entityType, const std::string & identifier, int32_t value);
  25. template<typename IdentifierType>
  26. explicit MetaIdentifier(const IdentifierType & identifier )
  27. : entityType(IdentifierType::entityType())
  28. , integerForm(identifier.getNum())
  29. , stringForm(IdentifierType::encode(identifier.getNum()))
  30. {
  31. static_assert(std::is_base_of<IdentifierBase, IdentifierType>::value, "MetaIdentifier can only be constructed from Identifer class");
  32. }
  33. int32_t getNum() const;
  34. std::string toString() const;
  35. template<typename IdentifierType>
  36. IdentifierType as() const
  37. {
  38. static_assert(std::is_base_of<IdentifierBase, IdentifierType>::value, "MetaIdentifier can only be converted to Identifer class");
  39. IdentifierType result(integerForm);
  40. return result;
  41. }
  42. template <typename Handler> void serialize(Handler &h, const int version)
  43. {
  44. h & stringForm;
  45. if (!h.saving)
  46. onDeserialized();
  47. }
  48. bool operator == (const MetaIdentifier & other) const;
  49. bool operator != (const MetaIdentifier & other) const;
  50. bool operator < (const MetaIdentifier & other) const;
  51. };
  52. VCMI_LIB_NAMESPACE_END