Mantid
Loading...
Searching...
No Matches
StartsWithValidator.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9#include <algorithm>
10#ifndef Q_MOC_RUN
11#include <memory>
12#endif
13#include <sstream>
14
15namespace Mantid::Kernel {
16
17template <> std::string ListValidator<std::string>::checkValidity(const std::string &value) const {
18 if (m_allowedValues.end() != std::find(m_allowedValues.begin(), m_allowedValues.end(), value)) {
19 return "";
20 }
21 if (value.empty())
22 return "Select a value";
23 if (isAlias(value))
24 return "_alias";
25 std::ostringstream os;
26 os << "The value \"" << value << "\" is not in the list of allowed values";
27 return os.str();
28}
29
31GNU_DIAG_OFF("attributes")
32template class MANTID_KERNEL_DLL ListValidator<std::string>;
33template class MANTID_KERNEL_DLL TypedValidator<std::string>;
34GNU_DIAG_ON("attributes")
36
41StartsWithValidator::StartsWithValidator(const std::vector<std::string> &values)
42 : Kernel::StringListValidator(values) {}
47StartsWithValidator::StartsWithValidator(const std::set<std::string> &values) : Kernel::StringListValidator(values) {}
48
50IValidator_sptr StartsWithValidator::clone() const { return std::make_shared<StartsWithValidator>(*this); }
51
57std::string StartsWithValidator::checkValidity(const std::string &value) const {
58 if (std::any_of(m_allowedValues.cbegin(), m_allowedValues.cend(),
59 [&value](const auto &val) { return value.substr(0, val.size()) == val; })) {
60 return "";
61 }
62 if (isEmpty(value))
63 return "Select a value";
64 std::ostringstream os;
65 os << "The value \"" << value << "\" does not start with any of the allowed values";
66 return os.str();
67}
68
69} // namespace Mantid::Kernel
double value
The value of the point.
Definition FitMW.cpp:51
#define GNU_DIAG_ON(x)
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
ListValidator is a validator that requires the value of a property to be one of a defined list of pos...
bool isEmpty(const T &value) const
Is the value considered empty.
std::string checkValidity(const TYPE &value) const override
Checks if the string passed is in the list.
std::vector< TYPE > m_allowedValues
The set of valid values.
StartsWithValidator is a validator that requires the value of a property to start with one of the str...
std::string checkValidity(const std::string &value) const override
Checks if the string passed starts with one from the list.
IValidator_sptr clone() const override
Clone the validator.
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
STL namespace.