Version.cpp
Go to the documentation of this file.
2 
3 // std
4 #include <cstdio>
5 #include <cstdlib>
6 #include <stdexcept>
7 
8 namespace dai {
9 
10 Version::Version(const std::string& v) : versionMajor(0), versionMinor(0), versionPatch(0), buildInfo{""} {
11  // Parse string
12  char buffer[256]{0};
13  if(std::sscanf(v.c_str(), "%u.%u.%u+%255s", &versionMajor, &versionMinor, &versionPatch, buffer) != 4) {
14  if(std::sscanf(v.c_str(), "%u.%u.%u", &versionMajor, &versionMinor, &versionPatch) != 3) {
15  throw std::runtime_error("Cannot parse version: " + v);
16  }
17  } else {
18  buildInfo = std::string{buffer};
19  }
20 }
21 
22 Version::Version(unsigned vmajor, unsigned vminor, unsigned vpatch) : versionMajor(vmajor), versionMinor(vminor), versionPatch(vpatch), buildInfo{""} {}
23 
24 Version::Version(unsigned vmajor, unsigned vminor, unsigned vpatch, std::string buildInfo)
25  : versionMajor(vmajor), versionMinor(vminor), versionPatch(vpatch), buildInfo(buildInfo) {}
26 
27 bool Version::operator==(const Version& other) const {
28  if(versionMajor == other.versionMajor && versionMinor == other.versionMinor && versionPatch == other.versionPatch && buildInfo == other.buildInfo) {
29  return true;
30  }
31  return false;
32 }
33 
34 bool Version::operator<(const Version& other) const {
35  if(versionMajor < other.versionMajor) {
36  return true;
37  } else if(versionMajor == other.versionMajor) {
38  if(versionMinor < other.versionMinor) {
39  return true;
40  } else if(versionMinor == other.versionMinor) {
41  if(versionPatch < other.versionPatch) {
42  return true;
43  } else if(versionPatch == other.versionPatch) {
44  if(!buildInfo.empty() && other.buildInfo.empty()) {
45  return true;
46  }
47  }
48  }
49  }
50  return false;
51 }
52 
53 std::string Version::toString() const {
54  std::string version = std::to_string(versionMajor) + "." + std::to_string(versionMinor) + "." + std::to_string(versionPatch);
55  if(!buildInfo.empty()) {
56  version += "+" + buildInfo;
57  }
58  return version;
59 }
60 
61 std::string Version::toStringSemver() const {
62  std::string version = std::to_string(versionMajor) + "." + std::to_string(versionMinor) + "." + std::to_string(versionPatch);
63  return version;
64 }
65 
66 std::string Version::getBuildInfo() const {
67  return buildInfo;
68 }
69 
72 }
73 
74 } // namespace dai
dai::Version::getBuildInfo
std::string getBuildInfo() const
Get build info.
Definition: Version.cpp:66
dai::Version::operator==
bool operator==(const Version &other) const
Definition: Version.cpp:27
dai::Version
Version structure.
Definition: Version.hpp:8
dai::Version::Version
Version(const std::string &v)
Construct Version from string.
Definition: Version.cpp:10
dai::Version::toStringSemver
std::string toStringSemver() const
Convert Version to semver (no build information) string.
Definition: Version.cpp:61
nanorpc::core::type::buffer
std::vector< std::uint8_t > buffer
Definition: type.h:28
Version.hpp
dai::Version::getSemver
Version getSemver() const
Retrieves semver version (no build information)
Definition: Version.cpp:70
nanorpc::core::exception::to_string
std::string to_string(std::exception const &e)
Definition: exception.h:46
dai::Version::operator<
bool operator<(const Version &other) const
Definition: Version.cpp:34
dai::Version::versionPatch
unsigned versionPatch
Definition: Version.hpp:39
dai::Version::toString
std::string toString() const
Convert Version to string.
Definition: Version.cpp:53
dai::Version::versionMinor
unsigned versionMinor
Definition: Version.hpp:39
dai::Version::versionMajor
unsigned versionMajor
Definition: Version.hpp:39
dai
Definition: CameraExposureOffset.hpp:6
dai::Version::buildInfo
std::string buildInfo
Definition: Version.hpp:40


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19