CModVersion.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * CModVersion.cpp, 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. #include "StdInc.h"
  11. #include "CModVersion.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. CModVersion CModVersion::GameVersion()
  14. {
  15. return CModVersion(VCMI_VERSION_MAJOR, VCMI_VERSION_MINOR, VCMI_VERSION_PATCH);
  16. }
  17. CModVersion CModVersion::fromString(std::string from)
  18. {
  19. int major = Any;
  20. int minor = Any;
  21. int patch = Any;
  22. try
  23. {
  24. auto pointPos = from.find('.');
  25. major = std::stoi(from.substr(0, pointPos));
  26. if(pointPos != std::string::npos)
  27. {
  28. from = from.substr(pointPos + 1);
  29. pointPos = from.find('.');
  30. minor = std::stoi(from.substr(0, pointPos));
  31. if(pointPos != std::string::npos)
  32. patch = std::stoi(from.substr(pointPos + 1));
  33. }
  34. }
  35. catch(const std::invalid_argument &)
  36. {
  37. return CModVersion();
  38. }
  39. return CModVersion(major, minor, patch);
  40. }
  41. std::string CModVersion::toString() const
  42. {
  43. std::string res;
  44. if(major != Any)
  45. {
  46. res += std::to_string(major);
  47. if(minor != Any)
  48. {
  49. res += '.' + std::to_string(minor);
  50. if(patch != Any)
  51. res += '.' + std::to_string(patch);
  52. }
  53. }
  54. return res;
  55. }
  56. bool CModVersion::operator ==(const CModVersion & other) const
  57. {
  58. return major == other.major && minor == other.minor && patch == other.patch;
  59. }
  60. bool CModVersion::operator !=(const CModVersion & other) const
  61. {
  62. return major != other.major || minor != other.minor || patch != other.patch;
  63. }
  64. bool CModVersion::compatible(const CModVersion & other, bool checkMinor, bool checkPatch) const
  65. {
  66. bool doCheckMinor = checkMinor && minor != Any && other.minor != Any;
  67. bool doCheckPatch = checkPatch && patch != Any && other.patch != Any;
  68. assert(!doCheckPatch || (doCheckPatch && doCheckMinor));
  69. return (major == other.major &&
  70. (!doCheckMinor || minor >= other.minor) &&
  71. (!doCheckPatch || minor > other.minor || (minor == other.minor && patch >= other.patch)));
  72. }
  73. bool CModVersion::isNull() const
  74. {
  75. return major == Any;
  76. }
  77. bool operator < (const CModVersion & lesser, const CModVersion & greater)
  78. {
  79. //specific is "greater" than non-specific, that's why do not check for Any value
  80. if(lesser.major == greater.major)
  81. {
  82. if(lesser.minor == greater.minor)
  83. return lesser.patch < greater.patch;
  84. return lesser.minor < greater.minor;
  85. }
  86. return lesser.major < greater.major;
  87. }
  88. VCMI_LIB_NAMESPACE_END