Version.cpp 2.6 KB

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