Version.cpp 2.5 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: 2022-05-12
  7. *
  8. * */
  9. #include <regex>
  10. #include <ls_std/core/Version.hpp>
  11. ls::std::core::Version::Version(ls::std::core::type::version_type _majorVersion, ls::std::core::type::version_type _minorVersion, ls::std::core::type::version_type _patchVersion)
  12. : majorVersion(_majorVersion),
  13. minorVersion(_minorVersion),
  14. patchVersion(_patchVersion)
  15. {}
  16. ls::std::core::type::byte_field ls::std::core::Version::marshal()
  17. {
  18. ls::std::core::type::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::core::Version::unmarshal(const ls::std::core::type::byte_field &_data)
  25. {
  26. ::std::string field = _data;
  27. if (ls::std::core::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::core::type::version_type ls::std::core::Version::getMajorVersion() const
  41. {
  42. return this->majorVersion;
  43. }
  44. ls::std::core::type::version_type ls::std::core::Version::getMinorVersion() const
  45. {
  46. return this->minorVersion;
  47. }
  48. ls::std::core::type::version_type ls::std::core::Version::getPatchVersion() const
  49. {
  50. return this->patchVersion;
  51. }
  52. bool ls::std::core::Version::isValid(const ::std::string &_versionString)
  53. {
  54. return ls::std::core::Version::_isValid(_versionString);
  55. }
  56. void ls::std::core::Version::setMajorVersion(ls::std::core::type::version_type _major)
  57. {
  58. this->majorVersion = _major;
  59. }
  60. void ls::std::core::Version::setMinorVersion(ls::std::core::type::version_type _minor)
  61. {
  62. this->minorVersion = _minor;
  63. }
  64. void ls::std::core::Version::setPatchVersion(ls::std::core::type::version_type _patch)
  65. {
  66. this->patchVersion = _patch;
  67. }
  68. bool ls::std::core::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. }