Cpp: Add EngineInfo::Version class
authorAndre Heinecke <aheinecke@intevation.de>
Thu, 14 Jul 2016 15:01:33 +0000 (17:01 +0200)
committerAndre Heinecke <aheinecke@intevation.de>
Thu, 14 Jul 2016 15:02:38 +0000 (17:02 +0200)
* lang/cpp/src/engineinfo.cpp (EngineInfo::engineVersion): New.
* lang/cpp/src/engineinfo.h (EngineInfo::engineVersion): Declare.
(EngineInfo::Version): Small helper to work with versions.

lang/cpp/src/engineinfo.cpp
lang/cpp/src/engineinfo.h

index d260090..c3b3e04 100644 (file)
@@ -72,6 +72,11 @@ const char *GpgME::EngineInfo::version() const
     return isNull() ? 0 : d->info->version;
 }
 
+GpgME::EngineInfo::Version GpgME::EngineInfo::engineVersion() const
+{
+    return Version(version());
+}
+
 const char *GpgME::EngineInfo::requiredVersion() const
 {
     return isNull() ? 0 : d->info->req_version;
index 4de9884..72e125c 100644 (file)
@@ -28,6 +28,8 @@
 #include <memory>
 
 #include <algorithm>
+#include <string>
+#include <iostream>
 
 namespace GpgME
 {
@@ -35,6 +37,59 @@ namespace GpgME
 class GPGMEPP_EXPORT EngineInfo
 {
 public:
+    struct Version
+    {
+        int major, minor, patch;
+
+        Version(const std::string& version)
+        {
+            if (version.empty() ||
+                std::sscanf(version.c_str(), "%d.%d.%d", &major, &minor, &patch) != 3) {
+                major = 0;
+                minor = 0;
+                patch = 0;
+            }
+        }
+
+        bool operator < (const Version& other)
+        {
+            if (major < other.major)
+                return true;
+            if (minor < other.minor)
+                return true;
+            if (patch < other.patch)
+                return true;
+            return false;
+        }
+
+        bool operator < (const char* other)
+        {
+            return operator<(Version(other));
+        }
+
+        bool operator == (const Version& other)
+        {
+            return major == other.major
+                && minor == other.minor
+                && patch == other.patch;
+        }
+
+        bool operator == (const char* other)
+        {
+            return operator==(Version(other));
+        }
+
+        friend std::ostream& operator << (std::ostream& stream, const Version& ver)
+        {
+            stream << ver.major;
+            stream << '.';
+            stream << ver.minor;
+            stream << '.';
+            stream << ver.patch;
+            return stream;
+        }
+    };
+
     EngineInfo();
     explicit EngineInfo(gpgme_engine_info_t engine);
 
@@ -55,6 +110,7 @@ public:
     Protocol protocol() const;
     const char *fileName() const;
     const char *version() const;
+    Version engineVersion() const;
     const char *requiredVersion() const;
     const char *homeDirectory() const;