Version.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-28
  6. * Changed: 2020-11-26
  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. std::string subSequence {};
  28. size_t position;
  29. if(ls_std::Version::_isValid(_data)) {
  30. position = field.find('.');
  31. subSequence = field.substr(0, position);
  32. this->majorVersion = std::stoi(subSequence);
  33. field.erase(0, position + 1);
  34. position = field.find('.');
  35. subSequence = field.substr(0, position);
  36. this->minorVersion = std::stoi(subSequence);
  37. field.erase(0, position + 1);
  38. this->patchVersion = std::stoi(field);
  39. }
  40. }
  41. ls_std::version_type ls_std::Version::getMajorVersion() const
  42. {
  43. return this->majorVersion;
  44. }
  45. ls_std::version_type ls_std::Version::getMinorVersion() const
  46. {
  47. return this->minorVersion;
  48. }
  49. ls_std::version_type ls_std::Version::getPatchVersion() const
  50. {
  51. return this->patchVersion;
  52. }
  53. bool ls_std::Version::isValid(const std::string &_versionString)
  54. {
  55. return ls_std::Version::_isValid(_versionString);
  56. }
  57. void ls_std::Version::setMajorVersion(ls_std::version_type _major)
  58. {
  59. this->majorVersion = _major;
  60. }
  61. void ls_std::Version::setMinorVersion(ls_std::version_type _minor)
  62. {
  63. this->minorVersion = _minor;
  64. }
  65. void ls_std::Version::setPatchVersion(ls_std::version_type _patch)
  66. {
  67. this->patchVersion = _patch;
  68. }
  69. bool ls_std::Version::_isValid(const std::string &_versionString)
  70. {
  71. static std::regex versionRegex {R"(\d+[.]\d+[.]\d+)"};
  72. return std::regex_match(_versionString.begin(), _versionString.end(), versionRegex);
  73. }