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 <map>
16#include <set>
17#include <unordered_set>
18#include <vector>
19
20namespace Mantid {
21namespace Kernel {
29template <typename TYPE> class ListValidator : public TypedValidator<TYPE> {
30public:
32 ListValidator() : TypedValidator<TYPE>(), m_allowMultiSelection(false) {}
33
39 template <typename T>
40 explicit ListValidator(const T &values,
41 const std::map<std::string, std::string> &aliases = std::map<std::string, std::string>(),
42 const bool allowMultiSelection = false)
43 : TypedValidator<TYPE>(), m_allowedValues(values.begin(), values.end()),
44 m_aliases(aliases.begin(), aliases.end()), m_allowMultiSelection(allowMultiSelection) {
46 throw Kernel::Exception::NotImplementedError("The List Validator does not support multi selection yet");
47 }
48 for (auto aliasIt = m_aliases.begin(); aliasIt != m_aliases.end(); ++aliasIt) {
49 if (values.end() == std::find(values.begin(), values.end(), boost::lexical_cast<TYPE>(aliasIt->second))) {
50 throw std::invalid_argument("Alias " + aliasIt->first + " refers to invalid value " + aliasIt->second);
51 }
52 }
53 }
54
56 IValidator_sptr clone() const override { return std::make_shared<ListValidator<TYPE>>(*this); }
61 std::vector<std::string> allowedValues() const override {
63 std::vector<std::string> allowedStrings;
64 allowedStrings.reserve(m_allowedValues.size());
65 auto cend = m_allowedValues.end();
66 for (auto cit = m_allowedValues.begin(); cit != cend; ++cit) {
67 allowedStrings.emplace_back(boost::lexical_cast<std::string>(*cit));
68 }
69 return allowedStrings;
70 }
71
76 void addAllowedValue(const TYPE &value) {
77 // add only new values
78 if (std::find(m_allowedValues.begin(), m_allowedValues.end(), value) == m_allowedValues.end()) {
79 m_allowedValues.emplace_back(value);
80 }
81 }
82
88 std::string getValueForAlias(const std::string &alias) const override {
89 auto aliasIt = m_aliases.find(alias);
90 if (aliasIt == m_aliases.end()) {
91 throw std::invalid_argument("Unknown alias found " + alias);
92 }
93 return aliasIt->second;
94 }
95
97
98 void setMultipleSelectionAllowed(const bool isMultiSelectionAllowed) {
99 if (isMultiSelectionAllowed) {
100 throw Kernel::Exception::NotImplementedError("The List Validator does not support Multi selection yet");
101 }
102 m_allowMultiSelection = isMultiSelectionAllowed;
103 }
104
105protected:
111 std::string checkValidity(const TYPE &value) const override {
112 if (m_allowedValues.end() != std::find(m_allowedValues.begin(), m_allowedValues.end(), value)) {
113 return "";
114 } else {
115 if (isEmpty(value))
116 return "Select a value";
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 }
124
130 template <typename T> bool isEmpty(const T &value) const { UNUSED_ARG(value) return false; }
136 bool isEmpty(const std::string &value) const { return value.empty(); }
137
143 template <typename T> bool isAlias(const T &value) const {
144 std::string strValue = boost::lexical_cast<std::string>(value);
145 return m_aliases.find(strValue) != m_aliases.end();
146 }
147
153 bool isAlias(const std::string &value) const { return m_aliases.find(value) != m_aliases.end(); }
154
156 std::vector<TYPE> m_allowedValues;
158 std::map<std::string, std::string> m_aliases;
159
162};
163
166
167} // namespace Kernel
168} // 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:64
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...
Definition: ListValidator.h:29
bool isMultipleSelectionAllowed() override
Definition: ListValidator.h:96
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.
Definition: ListValidator.h:56
std::string getValueForAlias(const std::string &alias) const override
Return an allowed value (as a string) given an alias.
Definition: ListValidator.h:88
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.
Definition: ListValidator.h:32
void addAllowedValue(const TYPE &value)
Add value to the list of allowable values if it's not already there.
Definition: ListValidator.h:76
void setMultipleSelectionAllowed(const bool isMultiSelectionAllowed)
Definition: ListValidator.h:98
std::vector< std::string > allowedValues() const override
Returns the set of allowed values currently defined.
Definition: ListValidator.h:61
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.
Definition: ListValidator.h:40
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.