Libkleo

predicates.h
1/* -*- mode: c++; c-basic-offset:4 -*-
2 kleo/predicates.h
3
4 This file is part of Kleopatra, the KDE keymanager
5 SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9
10#pragma once
11
12#include <gpgme++/key.h>
13
14#include <algorithm>
15#include <cstring>
16#include <functional>
17#include <iterator>
18#include <string>
19
20namespace Kleo
21{
22namespace _detail
23{
24
25inline int mystrcmp(const char *s1, const char *s2)
26{
27 using namespace std;
28 return s1 ? s2 ? strcmp(s1, s2) : 1 : s2 ? -1 : 0;
29}
30
31#define make_comparator_str_impl(Name, expr, cmp) \
32 template<template<typename U> class Op> \
33 struct Name { \
34 typedef bool result_type; \
35 \
36 bool operator()(const char *lhs, const char *rhs) const \
37 { \
38 return Op<int>()(cmp, 0); \
39 } \
40 \
41 bool operator()(const std::string &lhs, const std::string &rhs) const \
42 { \
43 return operator()(lhs.c_str(), rhs.c_str()); \
44 } \
45 bool operator()(const char *lhs, const std::string &rhs) const \
46 { \
47 return operator()(lhs, rhs.c_str()); \
48 } \
49 bool operator()(const std::string &lhs, const char *rhs) const \
50 { \
51 return operator()(lhs.c_str(), rhs); \
52 } \
53 \
54 template<typename T> \
55 bool operator()(const T &lhs, const T &rhs) const \
56 { \
57 return operator()((lhs expr), (rhs expr)); \
58 } \
59 template<typename T> \
60 bool operator()(const T &lhs, const char *rhs) const \
61 { \
62 return operator()((lhs expr), rhs); \
63 } \
64 template<typename T> \
65 bool operator()(const char *lhs, const T &rhs) const \
66 { \
67 return operator()(lhs, (rhs expr)); \
68 } \
69 template<typename T> \
70 bool operator()(const T &lhs, const std::string &rhs) const \
71 { \
72 return operator()((lhs expr), rhs); \
73 } \
74 template<typename T> \
75 bool operator()(const std::string &lhs, const T &rhs) const \
76 { \
77 return operator()(lhs, (rhs expr)); \
78 } \
79 }
80
81#define make_comparator_str_fast(Name, expr) make_comparator_str_impl(Name, expr, _detail::mystrcmp(lhs, rhs))
82#define make_comparator_str(Name, expr) make_comparator_str_impl(Name, expr, qstricmp(lhs, rhs))
83
84make_comparator_str_fast(ByFingerprint, .primaryFingerprint());
85make_comparator_str_fast(ByKeyID, .keyID());
86make_comparator_str_fast(ByShortKeyID, .shortKeyID());
87make_comparator_str_fast(ByChainID, .chainID());
88make_comparator_str_fast(ByKeyGrip, .keyGrip());
89
90template<typename T>
91void sort_by_fpr(T &t)
92{
93 std::sort(t.begin(), t.end(), ByFingerprint<std::less>());
94}
95
96template<typename T>
97void remove_duplicates_by_fpr(T &t)
98{
99 t.erase(std::unique(t.begin(), t.end(), ByFingerprint<std::equal_to>()), t.end());
100}
101
102template<typename T>
103T union_by_fpr(const T &t1, const T &t2)
104{
105 T result;
106 result.reserve(t1.size() + t2.size());
107 std::set_union(t1.begin(), //
108 t1.end(),
109 t2.begin(),
110 t2.end(),
111 std::back_inserter(result),
112 ByFingerprint<std::less>());
113 return result;
114}
115}
116}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:14:12 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.