Cpp: Add string comparators for keys

* 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.
This commit is contained in:
Andre Heinecke 2016-04-11 17:02:04 +02:00
parent 691950e18c
commit 1bb162a54b
2 changed files with 56 additions and 0 deletions

View 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__

View 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__