Mantid
Loading...
Searching...
No Matches
ListValidator.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2008 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 +
7#pragma once
8
11#ifndef Q_MOC_RUN
12#include <boost/lexical_cast.hpp>
13#include <memory>
14#endif
15#include <algorithm>
16#include <map>
17#include <set>
18#include <type_traits>
19#include <unordered_set>
20#include <vector>
21
22namespace Mantid {
23namespace Kernel {
31template <typename TYPE> class ListValidator : public TypedValidator<TYPE> {
32public:
34 ListValidator() : TypedValidator<TYPE>(), m_allowMultiSelection(false) {}
35
41 template <typename T>
42 explicit ListValidator(const T &values,
43 const std::map<std::string, std::string> &aliases = std::map<std::string, std::string>(),
44 const bool allowMultiSelection = false)
45 : TypedValidator<TYPE>(), m_allowedValues(values.begin(), values.end()),
46 m_aliases(aliases.begin(), aliases.end()), m_allowMultiSelection(allowMultiSelection) {
48 throw Kernel::Exception::NotImplementedError("The List Validator does not support multi selection yet");
49 }
50 for (auto aliasIt = m_aliases.begin(); aliasIt != m_aliases.end(); ++aliasIt) {
51 if (values.end() == std::find(values.begin(), values.end(), boost::lexical_cast<TYPE>(aliasIt->second))) {
52 throw std::invalid_argument("Alias " + aliasIt->first + " refers to invalid value " + aliasIt->second);
53 }
54 }
55 }
56
58 IValidator_sptr clone() const override { return std::make_shared<ListValidator<TYPE>>(*this); }
63 std::vector<std::string> allowedValues() const override {
65 std::vector<std::string> allowedStrings;
66 allowedStrings.reserve(m_allowedValues.size());
67 auto cend = m_allowedValues.end();
68 for (auto cit = m_allowedValues.begin(); cit != cend; ++cit) {
69 allowedStrings.emplace_back(boost::lexical_cast<std::string>(*cit));
70 }
71 return allowedStrings;
72 }
73
78 void addAllowedValue(const TYPE &value) {
79 // add only new values
80 if (std::find(m_allowedValues.begin(), m_allowedValues.end(), value) == m_allowedValues.end()) {
81 m_allowedValues.emplace_back(value);
82 }
83 }
84
90 std::string getValueForAlias(const std::string &alias) const override {
91 auto aliasIt = m_aliases.find(alias);
92 if (aliasIt == m_aliases.end()) {
93 throw std::invalid_argument("Unknown alias found " + alias);
94 }
95 return aliasIt->second;
96 }
97
99
100 void setMultipleSelectionAllowed(const bool isMultiSelectionAllowed) {
101 if (isMultiSelectionAllowed) {
102 throw Kernel::Exception::NotImplementedError("The List Validator does not support Multi selection yet");
103 }
104 m_allowMultiSelection = isMultiSelectionAllowed;
105 }
106
107protected:
113 std::string checkValidity(const TYPE &value) const override {
114 if (m_allowedValues.end() != std::find(m_allowedValues.begin(), m_allowedValues.end(), value)) {
115 return "";
116 }
117 if (isAlias(value))
118 return "_alias";
119 std::ostringstream os;
120 os << "The value \"" << value << "\" is not in the list of allowed values";
121 return os.str();
122 }
123
129 template <typename T> bool isEmpty(const T &value) const { UNUSED_ARG(value) return false; }
135 bool isEmpty(const std::string &value) const { return value.empty(); }
136
142 template <typename T> bool isAlias(const T &value) const {
143 std::string strValue = boost::lexical_cast<std::string>(value);
144 return m_aliases.find(strValue) != m_aliases.end();
145 }
146
152 bool isAlias(const std::string &value) const { return m_aliases.find(value) != m_aliases.end(); }
153
155 std::vector<TYPE> m_allowedValues;
157 std::map<std::string, std::string> m_aliases;
158
161};
162
165
166template <> inline std::string ListValidator<std::string>::checkValidity(const std::string &value) const {
167 if (m_allowedValues.end() != std::find(m_allowedValues.begin(), m_allowedValues.end(), value)) {
168 return "";
169 }
170 if (value.empty())
171 return "Select a value";
172 if (isAlias(value))
173 return "_alias";
174 std::ostringstream os;
175 os << "The value \"" << value << "\" is not in the list of allowed values";
176 return os.str();
177}
178
179} // namespace Kernel
180} // namespace Mantid
double value
The value of the point.
Definition FitMW.cpp:51
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition System.h:44
Marks code as not implemented yet.
Definition Exception.h:138
ListValidator is a validator that requires the value of a property to be one of a defined list of pos...
bool isMultipleSelectionAllowed() override
bool isEmpty(const T &value) const
Is the value considered empty.
bool isAlias(const std::string &value) const
Test if a value is an alias of an alowed value.
IValidator_sptr clone() const override
Clone the validator.
std::string getValueForAlias(const std::string &alias) const override
Return an allowed value (as a string) given an alias.
bool isEmpty(const std::string &value) const
Is the value considered empty.
std::string checkValidity(const TYPE &value) const override
Checks if the string passed is in the list.
bool m_allowMultiSelection
if the validator should allow multiple selection
std::map< std::string, std::string > m_aliases
The optional aliases for the allowed values.
bool isAlias(const T &value) const
Test if a value is an alias of an alowed value.
ListValidator()
Default constructor. Sets up an empty list of valid values.
void addAllowedValue(const TYPE &value)
Add value to the list of allowable values if it's not already there.
void setMultipleSelectionAllowed(const bool isMultiSelectionAllowed)
std::vector< std::string > allowedValues() const override
Returns the set of allowed values currently defined.
std::vector< TYPE > m_allowedValues
The set of valid values.
ListValidator(const T &values, const std::map< std::string, std::string > &aliases=std::map< std::string, std::string >(), const bool allowMultiSelection=false)
Constructor.
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
Definition IValidator.h:26
Helper class which provides the Collimation Length for SANS instruments.