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 <unordered_set>
19#include <vector>
20
21namespace Mantid {
22namespace Kernel {
30template <typename TYPE> class ListValidator : public TypedValidator<TYPE> {
31public:
33 ListValidator() : TypedValidator<TYPE>(), m_allowMultiSelection(false) {}
34
40 template <typename T>
41 explicit ListValidator(const T &values,
42 const std::map<std::string, std::string> &aliases = std::map<std::string, std::string>(),
43 const bool allowMultiSelection = false)
44 : TypedValidator<TYPE>(), m_allowedValues(values.begin(), values.end()),
45 m_aliases(aliases.begin(), aliases.end()), m_allowMultiSelection(allowMultiSelection) {
47 throw Kernel::Exception::NotImplementedError("The List Validator does not support multi selection yet");
48 }
49 for (auto aliasIt = m_aliases.begin(); aliasIt != m_aliases.end(); ++aliasIt) {
50 if (values.end() == std::find(values.begin(), values.end(), boost::lexical_cast<TYPE>(aliasIt->second))) {
51 throw std::invalid_argument("Alias " + aliasIt->first + " refers to invalid value " + aliasIt->second);
52 }
53 }
54 }
55
57 IValidator_sptr clone() const override { return std::make_shared<ListValidator<TYPE>>(*this); }
62 std::vector<std::string> allowedValues() const override {
64 std::vector<std::string> allowedStrings;
65 allowedStrings.reserve(m_allowedValues.size());
66 auto cend = m_allowedValues.end();
67 for (auto cit = m_allowedValues.begin(); cit != cend; ++cit) {
68 allowedStrings.emplace_back(boost::lexical_cast<std::string>(*cit));
69 }
70 return allowedStrings;
71 }
72
77 void addAllowedValue(const TYPE &value) {
78 // add only new values
79 if (std::find(m_allowedValues.begin(), m_allowedValues.end(), value) == m_allowedValues.end()) {
80 m_allowedValues.emplace_back(value);
81 }
82 }
83
89 std::string getValueForAlias(const std::string &alias) const override {
90 auto aliasIt = m_aliases.find(alias);
91 if (aliasIt == m_aliases.end()) {
92 throw std::invalid_argument("Unknown alias found " + alias);
93 }
94 return aliasIt->second;
95 }
96
98
99 void setMultipleSelectionAllowed(const bool isMultiSelectionAllowed) {
100 if (isMultiSelectionAllowed) {
101 throw Kernel::Exception::NotImplementedError("The List Validator does not support Multi selection yet");
102 }
103 m_allowMultiSelection = isMultiSelectionAllowed;
104 }
105
106protected:
112 std::string checkValidity(const TYPE &value) const override {
113 if (m_allowedValues.end() != std::find(m_allowedValues.begin(), m_allowedValues.end(), value)) {
114 return "";
115 } else {
116 // For a generic type, isEmpty always returns false, but if TYPE is std::string,
117 // then the string version of isEmpty will be used, which could be true or false.
118 if (isEmpty(value))
119 return "Select a value";
120 if (isAlias(value))
121 return "_alias";
122 std::ostringstream os;
123 os << "The value \"" << value << "\" is not in the list of allowed values";
124 return os.str();
125 }
126 }
127
133 template <typename T> bool isEmpty(const T &value) const { UNUSED_ARG(value) return false; }
139 bool isEmpty(const std::string &value) const { return value.empty(); }
140
146 template <typename T> bool isAlias(const T &value) const {
147 std::string strValue = boost::lexical_cast<std::string>(value);
148 return m_aliases.find(strValue) != m_aliases.end();
149 }
150
156 bool isAlias(const std::string &value) const { return m_aliases.find(value) != m_aliases.end(); }
157
159 std::vector<TYPE> m_allowedValues;
161 std::map<std::string, std::string> m_aliases;
162
165};
166
169
170} // namespace Kernel
171} // 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:48
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.