Mantid
Loading...
Searching...
No Matches
ArrayLengthValidator.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 +
8
9#include <cstdint>
10#include <memory>
11
12using namespace Mantid::Kernel;
13
14namespace Mantid::Kernel {
15
16//----------------------------------------------------------------------------------------------
19template <typename TYPE>
21 : TypedValidator<std::vector<TYPE>>(), m_arraySize(size_t(0)), m_hasArraySize(false), m_arraySizeMin(size_t(0)),
22 m_hasArraySizeMin(false), m_arraySizeMax(size_t(0)), m_hasArraySizeMax(false) {}
23//----------------------------------------------------------------------------------------------
27template <typename TYPE>
29 : TypedValidator<std::vector<TYPE>>(), m_arraySize(size_t(len)), m_hasArraySize(true), m_arraySizeMin(size_t(0)),
30 m_hasArraySizeMin(false), m_arraySizeMax(size_t(0)), m_hasArraySizeMax(false) {}
31//----------------------------------------------------------------------------------------------
36template <typename TYPE>
37ArrayLengthValidator<TYPE>::ArrayLengthValidator(const size_t lenmin, const size_t lenmax)
38 : TypedValidator<std::vector<TYPE>>(), m_arraySize(size_t(0)), m_hasArraySize(false),
39 m_arraySizeMin(size_t(lenmin)), m_hasArraySizeMin(true), m_arraySizeMax(size_t(lenmax)), m_hasArraySizeMax(true) {
40}
41//----------------------------------------------------------------------------------------------
44template <typename TYPE> ArrayLengthValidator<TYPE>::~ArrayLengthValidator() = default;
45
50template <typename TYPE> bool ArrayLengthValidator<TYPE>::hasLength() const { return this->m_hasArraySize; }
51
56template <typename TYPE> bool ArrayLengthValidator<TYPE>::hasMinLength() const { return this->m_hasArraySizeMin; }
57
62template <typename TYPE> bool ArrayLengthValidator<TYPE>::hasMaxLength() const { return this->m_hasArraySizeMax; }
63
68template <typename TYPE> const size_t &ArrayLengthValidator<TYPE>::getLength() const { return this->m_arraySize; }
69
74template <typename TYPE> const size_t &ArrayLengthValidator<TYPE>::getMinLength() const { return this->m_arraySizeMin; }
75
80template <typename TYPE> const size_t &ArrayLengthValidator<TYPE>::getMaxLength() const { return this->m_arraySizeMax; }
81
87template <typename TYPE> void ArrayLengthValidator<TYPE>::setLength(const size_t &value) {
88 this->m_hasArraySize = true;
89 this->m_arraySize = value;
90 this->clearLengthMax();
91 this->clearLengthMin();
92}
93
98template <typename TYPE> void ArrayLengthValidator<TYPE>::setLengthMin(const size_t &value) {
99 this->m_hasArraySizeMin = true;
100 this->m_arraySizeMin = value;
101 this->clearLength();
102}
107template <typename TYPE> void ArrayLengthValidator<TYPE>::setLengthMax(const size_t &value) {
108 this->m_hasArraySizeMax = true;
109 this->m_arraySizeMax = value;
110 this->clearLength();
111}
112
117template <typename TYPE> void ArrayLengthValidator<TYPE>::clearLength() {
118 this->m_hasArraySize = false;
119 this->m_arraySize = size_t(0);
120}
121
126template <typename TYPE> void ArrayLengthValidator<TYPE>::clearLengthMin() {
127 this->m_hasArraySizeMin = false;
128 this->m_arraySizeMin = size_t(0);
129}
130
135template <typename TYPE> void ArrayLengthValidator<TYPE>::clearLengthMax() {
136 this->m_hasArraySizeMax = false;
137 this->m_arraySizeMax = size_t(0);
138}
139
144template <typename TYPE> IValidator_sptr ArrayLengthValidator<TYPE>::clone() const {
145 return std::make_shared<ArrayLengthValidator>(*this);
146}
147
153template <typename TYPE> std::string ArrayLengthValidator<TYPE>::checkValidity(const std::vector<TYPE> &value) const {
154 if (this->hasLength() && value.size() != this->m_arraySize) {
155 return "Incorrect size";
156 }
157 if (this->hasMinLength() && value.size() < this->m_arraySizeMin) {
158 return "Array size too short";
159 }
160 if (this->hasMaxLength() && value.size() > this->m_arraySizeMax) {
161 return "Array size too long";
162 }
163 return "";
164}
165
166// Required explicit instantiations
167template class ArrayLengthValidator<double>;
168template class ArrayLengthValidator<int32_t>;
169template class ArrayLengthValidator<int64_t>;
171#if defined(_WIN32) || defined(__clang__) && defined(__APPLE__)
172template class ArrayLengthValidator<long>;
173#endif
174} // namespace Mantid::Kernel
double value
The value of the point.
Definition: FitMW.cpp:51
ArrayLenghtValidator : Validate length of an array property.
void setLength(const size_t &value)
Set length.
~ArrayLengthValidator() override
Destructor.
void setLengthMax(const size_t &value)
Set length max.
IValidator_sptr clone() const override
Clone function.
std::string checkValidity(const std::vector< TYPE > &value) const override
Private function to check validity.
bool hasLength() const
Return if it has a length.
const size_t & getLength() const
Return the length.
const size_t & getMaxLength() const
Return the maximum length.
bool hasMaxLength() const
Return if it has a length.
bool hasMinLength() const
Return if it has a length.
void setLengthMin(const size_t &value)
Set length min.
const size_t & getMinLength() const
Return the minimum length.
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
Definition: IValidator.h:26
STL namespace.