Version.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-28
  6. * Changed: 2021-04-24
  7. *
  8. * */
  9. #include <regex>
  10. #include <ls_std/base/Version.hpp>
  11. ls_std::Version::Version(ls_std::version_type _majorVersion, ls_std::version_type _minorVersion, ls_std::version_type _patchVersion)
  12. : majorVersion(_majorVersion),
  13. minorVersion(_minorVersion),
  14. patchVersion(_patchVersion)
  15. {}
  16. ls_std::byte_field ls_std::Version::marshal()
  17. {
  18. ls_std::byte_field data{};
  19. data += std::to_string(this->majorVersion) + ".";
  20. data += std::to_string(this->minorVersion) + ".";
  21. data += std::to_string(this->patchVersion);
  22. return data;
  23. }
  24. void ls_std::Version::unmarshal(const ls_std::byte_field &_data)
  25. {
  26. std::string field = _data;
  27. if (ls_std::Version::_isValid(_data))
  28. {
  29. size_t position = field.find('.');
  30. std::string subSequence = field.substr(0, position);
  31. this->majorVersion = std::stoi(subSequence);
  32. field.erase(0, position + 1);
  33. position = field.find('.');
  34. subSequence = field.substr(0, position);
  35. this->minorVersion = std::stoi(subSequence);
  36. field.erase(0, position + 1);
  37. this->patchVersion = std::stoi(field);
  38. }
  39. }
  40. ls_std::version_type ls_std::Version::getMajorVersion() const
  41. {
  42. return this->majorVersion;
  43. }
  44. ls_std::version_type ls_std::Version::getMinorVersion() const
  45. {
  46. return this->minorVersion;
  47. }
  48. ls_std::version_type ls_std::Version::getPatchVersion() const
  49. {
  50. return this->patchVersion;
  51. }
  52. bool ls_std::Version::isValid(const std::string &_versionString)
  53. {
  54. return ls_std::Version::_isValid(_versionString);
  55. }
  56. void ls_std::Version::setMajorVersion(ls_std::version_type _major)
  57. {
  58. this->majorVersion = _major;
  59. }
  60. void ls_std::Version::setMinorVersion(ls_std::version_type _minor)
  61. {
  62. this->minorVersion = _minor;
  63. }
  64. void ls_std::Version::setPatchVersion(ls_std::version_type _patch)
  65. {
  66. this->patchVersion = _patch;
  67. }
  68. bool ls_std::Version::_isValid(const std::string &_versionString)
  69. {
  70. bool isValidVersionString{};
  71. static std::regex versionRegex{R"(\d+[.]\d+[.]\d+)"};
  72. if (!_versionString.empty())
  73. {
  74. isValidVersionString = std::regex_match(_versionString.begin(), _versionString.end(), versionRegex);
  75. }
  76. return isValidVersionString;
  77. }