Mantid
Loading...
Searching...
No Matches
BoundedValidator.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 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
10#include <memory>
11#include <sstream>
12#include <string>
13
14namespace Mantid {
15namespace Kernel {
27namespace {
28constexpr int LOWER_BOUND = -1;
29constexpr int UPPER_BOUND = 1;
30} // namespace
31
32template <class TYPE> class DLLExport BoundedValidator final : public TypedValidator<TYPE> {
33public:
36 : TypedValidator<TYPE>(), m_hasLowerBound(false), m_hasUpperBound(false), m_lowerExclusive(false),
37 m_upperExclusive(false), m_lowerBound(TYPE()), m_upperBound(TYPE()), m_hasError(false) {}
38
44 BoundedValidator(const TYPE &lowerBound, const TYPE &upperBound, bool exclusive = false) noexcept
45 : TypedValidator<TYPE>(), m_hasLowerBound(true), m_hasUpperBound(true), m_lowerExclusive(exclusive),
46 m_upperExclusive(exclusive), m_lowerBound(lowerBound), m_upperBound(upperBound), m_hasError(false) {}
47
49 bool hasLower() const noexcept { return m_hasLowerBound; }
51 bool hasUpper() const noexcept { return m_hasUpperBound; }
53 TYPE lower() const noexcept { return m_lowerBound; }
55 TYPE upper() const noexcept { return m_upperBound; }
57 bool isLowerExclusive() const noexcept { return m_lowerExclusive; }
59 bool isUpperExclusive() const noexcept { return m_upperExclusive; }
61 void setLowerExclusive(const bool exclusive) noexcept { m_lowerExclusive = exclusive; }
63 void setUpperExclusive(const bool exclusive) noexcept { m_upperExclusive = exclusive; }
64
66 void setExclusive(const bool exclusive) noexcept {
67 setLowerExclusive(exclusive);
68 setUpperExclusive(exclusive);
69 }
70
72 void setLower(const TYPE &value) noexcept {
73 m_hasLowerBound = true;
74 m_lowerBound = value;
75 }
76
78 void setUpper(const TYPE &value) noexcept {
79 m_hasUpperBound = true;
80 m_upperBound = value;
81 }
82
84 void clearLower() noexcept {
85 m_hasLowerBound = false;
86 m_lowerBound = TYPE();
87 }
89 void clearUpper() noexcept {
90 m_hasUpperBound = false;
91 m_upperBound = TYPE();
92 }
93
95 void setBounds(const TYPE &lower, const TYPE &upper) noexcept {
96 setLower(lower);
97 setUpper(upper);
98 }
99
101 void clearBounds() noexcept {
102 clearLower();
103 clearUpper();
104 }
105
107 void setError(const TYPE &value) {
108 m_error = value;
109 m_hasError = true;
110 }
111
113 IValidator_sptr clone() const override { return std::make_shared<BoundedValidator>(*this); }
114
115private:
116 // Data and Function Members for This Class Implementation.
117
134
141 std::string checkValidity(const TYPE &value) const override {
142 // declare a class that can do conversions to string
143 std::ostringstream error;
144 // load in the "no error" condition
145 error << "";
146 // it is allowed not to have a lower bound, if not then you don't need to
147 // check
148 if (m_hasLowerBound && (value < (errorAdjustment(m_lowerBound, LOWER_BOUND)) ||
149 (value == (errorAdjustment(m_lowerBound, LOWER_BOUND)) && m_lowerExclusive))) {
150 error << "Selected value " << value << " is ";
151 (m_lowerExclusive) ? error << "<=" : error << "<";
152 error << " the lower bound (" << m_lowerBound;
153 (error.str() != "" && m_hasError) ? error << " +/- " << m_error << ")" : error << ")";
154 }
155
156 if (m_hasUpperBound && (value > (errorAdjustment(m_upperBound, UPPER_BOUND)) ||
157 (value == (errorAdjustment(m_upperBound, UPPER_BOUND)) && m_upperExclusive))) {
158 error << "Selected value " << value << " is ";
159 (m_upperExclusive) ? error << ">=" : error << ">";
160 error << " the upper bound (" << m_upperBound;
161 (error.str() != "" && m_hasError) ? error << " +/- " << m_error << ")" : error << ")";
162 }
163 return error.str();
164 }
165
166 TYPE errorAdjustment(const TYPE &boundingValue, const int boundID) const {
167 (void)boundID; // avoid unused variable warning.
168 return boundingValue;
169 }
170};
171
172template <> inline void BoundedValidator<std::string>::setError(const std::string &value) {
173 (void)value; // avoid unused variable warning.
174 throw std::invalid_argument("BoundedValidator<std::string> does not support error.");
175}
176template <>
177inline double BoundedValidator<double>::errorAdjustment(const double &boundingValue, const int boundID) const {
178 double adjValue;
179 m_hasError ? adjValue = boundingValue + m_error *boundID : adjValue = boundingValue;
180 return adjValue;
181}
182template <> inline int BoundedValidator<int>::errorAdjustment(const int &boundingValue, const int boundID) const {
183 int adjValue;
184 m_hasError ? adjValue = boundingValue + m_error *boundID : adjValue = boundingValue;
185 return adjValue;
186}
187
188} // namespace Kernel
189} // namespace Mantid
double value
The value of the point.
Definition: FitMW.cpp:51
double error
Definition: IndexPeaks.cpp:133
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition: System.h:53
double lower
lower and upper bounds on the multiplier, if known
double upper
BoundedValidator is a validator that requires the values to be between upper or lower bounds,...
bool isUpperExclusive() const noexcept
Check if upper bound is exclusive.
bool m_upperExclusive
Upper bound is exclusive.
void setExclusive(const bool exclusive) noexcept
Set both the upper and lower bounds to be exclusive.
bool m_hasError
Has an error set true/false.
void setLower(const TYPE &value) noexcept
Set lower bound value.
void setBounds(const TYPE &lower, const TYPE &upper) noexcept
Set both bounds (lower and upper) at the same time.
void setLowerExclusive(const bool exclusive) noexcept
Set the lower bound to be exclusive.
bool hasUpper() const noexcept
Return if it has a lower bound.
TYPE errorAdjustment(const TYPE &boundingValue, const int boundID) const
void clearLower() noexcept
Clear lower bound value.
BoundedValidator() noexcept
No-arg Constructor.
TYPE upper() const noexcept
Return the upper bound value.
void setUpper(const TYPE &value) noexcept
Set upper bound value.
bool hasLower() const noexcept
Return if it has a lower bound.
bool m_hasUpperBound
Has a upper bound set true/false.
BoundedValidator(const TYPE &lowerBound, const TYPE &upperBound, bool exclusive=false) noexcept
Constructor.
void setUpperExclusive(const bool exclusive) noexcept
Set the upper bound to be exclusive.
std::string checkValidity(const TYPE &value) const override
Checks that the value is within any upper and lower limits.
bool isLowerExclusive() const noexcept
Check if lower bound is exclusive.
bool m_lowerExclusive
Lower bound is exclusive.
void clearBounds() noexcept
Clear both bounds (lower and upper) at the same time.
void setError(const TYPE &value)
Set the allowed error.
IValidator_sptr clone() const override
Clone the current state.
bool m_hasLowerBound
Has a lower bound set true/false.
void clearUpper() noexcept
Clear upper bound value.
TYPE lower() const noexcept
Return the lower bound value.
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.