From 8c6c8ad3c0c8ff652f8c20893219d826f80698d9 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 22 Apr 2022 11:58:44 +0200 Subject: [PATCH] exntend version class to allow add info --- src/fsfw/version.cpp | 24 ++++++++++++++++++------ src/fsfw/version.h | 16 ++++++++-------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/fsfw/version.cpp b/src/fsfw/version.cpp index 926e465ff..a7f1d6a15 100644 --- a/src/fsfw/version.cpp +++ b/src/fsfw/version.cpp @@ -11,12 +11,24 @@ #undef minor #endif -const fsfw::Version fsfw::FSFW_VERSION = {FSFW_VERSION_MAJOR, FSFW_VERSION_MINOR, - FSFW_VERSION_REVISION}; +const Version fsfw::FSFW_VERSION = {FSFW_VERSION_MAJOR, FSFW_VERSION_MINOR, FSFW_VERSION_REVISION}; -fsfw::Version::Version(uint32_t major, uint32_t minor, uint32_t revision) - : major(major), minor(minor), revision(revision) {} +Version::Version(uint32_t major, uint32_t minor, uint32_t revision, const char* addInfo) + : major(major), minor(minor), revision(revision), addInfo(addInfo) {} -void fsfw::Version::getVersion(char* str, size_t maxLen) const { - snprintf(str, maxLen, "%d.%d.%d", major, minor, revision); +void Version::getVersion(char* str, size_t maxLen) const { + size_t len = snprintf(str, maxLen, "%d.%d.%d", major, minor, revision); + if (addInfo != nullptr) { + snprintf(str + len, maxLen - len, "-%s", addInfo); + } } + +#if FSFW_CPP_OSTREAM_ENABLED == 1 +std::ostream& operator<<(std::ostream& os, const Version& v) { + os << v.major << "." << v.minor << "." << v.revision; + if (v.addInfo != nullptr) { + os << "-" << v.addInfo; + } + return os; +} +#endif diff --git a/src/fsfw/version.h b/src/fsfw/version.h index 7cddf1938..d8497703c 100644 --- a/src/fsfw/version.h +++ b/src/fsfw/version.h @@ -8,15 +8,16 @@ #endif #include -namespace fsfw { - class Version { public: - Version(uint32_t major, uint32_t minor, uint32_t revision); + Version(uint32_t major, uint32_t minor, uint32_t revision, const char* addInfo = nullptr); uint32_t major = 0; uint32_t minor = 0; uint32_t revision = 0; + // Additional information, e.g. a git SHA hash + const char* addInfo = nullptr; + friend bool operator==(const Version& v1, const Version& v2) { return (v1.major == v2.major and v1.minor == v2.minor and v1.revision == v2.revision); } @@ -43,10 +44,7 @@ class Version { * @param v * @return */ - friend std::ostream& operator<<(std::ostream& os, const Version& v) { - os << v.major << "." << v.minor << "." << v.revision; - return os; - } + friend std::ostream& operator<<(std::ostream& os, const Version& v); #endif /** @@ -57,7 +55,9 @@ class Version { void getVersion(char* str, size_t maxLen) const; }; -extern const fsfw::Version FSFW_VERSION; +namespace fsfw { + +extern const Version FSFW_VERSION; } // namespace fsfw