Cpp: Add string comparators for keys
authorAndre Heinecke <aheinecke@intevation.de>
Mon, 11 Apr 2016 15:02:04 +0000 (17:02 +0200)
committerAndre Heinecke <aheinecke@intevation.de>
Mon, 11 Apr 2016 15:06:14 +0000 (17:06 +0200)
* lang/cpp/src/global.h (GPGMEPP_MAKE_STRCMP): New.
  (_gpgmepp_strcmp): NULL save wrapper around std::strcmp.
* lang/cpp/src/key.h: Add comparators for various attributes.

--
This was taken from libkleo predicates.h. Appears generally useful.

lang/cpp/src/global.h
lang/cpp/src/key.h

index b48ec07..9be5202 100644 (file)
@@ -28,6 +28,7 @@
 #include "gpgmepp_export.h"
 
 #include <iosfwd>
+#include <cstring>
 
 namespace GpgME
 {
@@ -152,4 +153,54 @@ GPGMEPP_EXPORT bool hasFeature(unsigned long feature, unsigned long feature2);
     operator unspecified_bool_type() const { return ( Cond ) ? &__safe_bool_dummy__::nonnull : 0 ; }
 # endif
 
+inline int _gpgmepp_strcmp(const char *s1, const char *s2)
+{
+    return s1 ? s2 ? std::strcmp(s1, s2) : 1 : s2 ? -1 : 0;
+}
+
+#define _GPGMEPP_MAKE_STRCMP( Name, expr, cmp )                     \
+    template <template <typename U> class Op>                           \
+    struct Name {                                                       \
+        typedef bool result_type;                                       \
+        \
+        bool operator()( const char * lhs, const char * rhs ) const {   \
+            return Op<int>()( cmp, 0 );                                 \
+        }                                                               \
+        \
+        bool operator()( const std::string & lhs, const std::string & rhs ) const { \
+            return operator()( lhs.c_str(), rhs.c_str() );              \
+        }                                                               \
+        bool operator()( const char * lhs, const std::string & rhs ) const { \
+            return operator()( lhs, rhs.c_str() );                      \
+        }                                                               \
+        bool operator()( const std::string & lhs, const char * rhs ) const { \
+            return operator()( lhs.c_str(), rhs );                      \
+        }                                                               \
+        \
+        template <typename T>                                           \
+        bool operator()( const T & lhs, const T & rhs ) const {         \
+            return operator()( (lhs expr), (rhs expr) );                \
+        }                                                               \
+        template <typename T>                                           \
+        bool operator()( const T & lhs, const char * rhs ) const {      \
+            return operator()( (lhs expr), rhs );                       \
+        }                                                               \
+        template <typename T>                                           \
+        bool operator()( const char * lhs, const T & rhs ) const {      \
+            return operator()( lhs, (rhs expr) );                       \
+        }                                                               \
+        template <typename T>                                           \
+        bool operator()( const T & lhs, const std::string & rhs ) const { \
+            return operator()( (lhs expr), rhs );                       \
+        }                                                               \
+        template <typename T>                                           \
+        bool operator()( const std::string & lhs, const T & rhs ) const {    \
+            return operator()( lhs, (rhs expr) );                       \
+        }                                                               \
+    }
+
+#define GPGMEPP_MAKE_STRCMP( Name, expr )                          \
+    _GPGMEPP_MAKE_STRCMP( Name, expr, _gpgmepp_strcmp( lhs, rhs ) )
+
+
 #endif // __GPGMEPP_GLOBAL_H__
index a87a35c..30badea 100644 (file)
@@ -353,4 +353,9 @@ GPGMEPP_MAKE_STD_SWAP_SPECIALIZATION(Subkey)
 GPGMEPP_MAKE_STD_SWAP_SPECIALIZATION(UserID)
 GPGMEPP_MAKE_STD_SWAP_SPECIALIZATION(UserID::Signature)
 
+GPGMEPP_MAKE_STRCMP(ByFingerprint, .primaryFingerprint());
+GPGMEPP_MAKE_STRCMP(ByKeyID, .keyID());
+GPGMEPP_MAKE_STRCMP(ByShortKeyID, .shortKeyID());
+GPGMEPP_MAKE_STRCMP(ByChainID, .chainID());
+
 #endif // __GPGMEPP_KEY_H__