Libkleo

multivalidator.cpp
1/* -*- mode: c++; c-basic-offset:4 -*-
2 utils/multivalidator.cpp
3
4 This file is part of Kleopatra, the KDE keymanager
5 SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB
6 SPDX-FileCopyrightText: 2022 g10 Code GmbH
7 SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
8
9 SPDX-License-Identifier: GPL-2.0-or-later
10*/
11
12#include "multivalidator_p.h"
13
14#include <algorithm>
15#include <iterator>
16
17using namespace Kleo;
18
19MultiValidator::MultiValidator(const std::vector<std::shared_ptr<QValidator>> &validators)
20 : QValidator{}
21 , m_validators{validators}
22{
23}
24
25// static
26std::shared_ptr<QValidator> Kleo::MultiValidator::create(const std::vector<std::shared_ptr<QValidator>> &validators)
27{
28 Q_ASSERT(std::all_of(std::begin(validators), std::end(validators), [](const auto &v) {
29 return v && !v->parent();
30 }));
31
32 return std::shared_ptr<MultiValidator>{new MultiValidator{validators}};
33}
34
35MultiValidator::~MultiValidator() = default;
36
37void MultiValidator::fixup(QString &str) const
38{
39 std::for_each(std::cbegin(m_validators), std::cend(m_validators), [&str](const auto &val) {
40 val->fixup(str);
41 });
42}
43
44QValidator::State MultiValidator::validate(QString &str, int &pos) const
45{
46 std::vector<State> states;
47 states.reserve(m_validators.size());
48 std::transform(std::cbegin(m_validators), std::cend(m_validators), std::back_inserter(states), [&str, &pos](const auto &val) {
49 return val->validate(str, pos);
50 });
51
52 if (std::any_of(std::cbegin(states), std::cend(states), [](State state) {
53 return state == Invalid;
54 })) {
55 return Invalid;
56 }
57
58 if (std::all_of(std::cbegin(states), std::cend(states), [](State state) {
59 return state == Acceptable;
60 })) {
61 return Acceptable;
62 }
63
64 return Intermediate;
65}
66
67#include "moc_multivalidator_p.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:00:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.