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 +
9
10#include <cstdint>
11#include <memory>
12
13using namespace Mantid::Kernel;
14
15namespace Mantid::Kernel {
16
17//----------------------------------------------------------------------------------------------
20template <typename TYPE>
22 : TypedValidator<std::vector<TYPE>>(), m_arraySize(size_t(0)), m_hasArraySize(false), m_arraySizeMin(size_t(0)),
23 m_hasArraySizeMin(false), m_arraySizeMax(size_t(0)), m_hasArraySizeMax(false) {}
24//----------------------------------------------------------------------------------------------
28template <typename TYPE>
30 : TypedValidator<std::vector<TYPE>>(), m_arraySize(size_t(len)), m_hasArraySize(true), m_arraySizeMin(size_t(0)),
31 m_hasArraySizeMin(false), m_arraySizeMax(size_t(0)), m_hasArraySizeMax(false) {}
32//----------------------------------------------------------------------------------------------
37template <typename TYPE>
38ArrayLengthValidator<TYPE>::ArrayLengthValidator(const size_t lenmin, const size_t lenmax)
39 : TypedValidator<std::vector<TYPE>>(), m_arraySize(size_t(0)), m_hasArraySize(false),
40 m_arraySizeMin(size_t(lenmin)), m_hasArraySizeMin(true), m_arraySizeMax(size_t(lenmax)), m_hasArraySizeMax(true) {
41}
42//----------------------------------------------------------------------------------------------
45template <typename TYPE> ArrayLengthValidator<TYPE>::~ArrayLengthValidator() = default;
46
51template <typename TYPE> bool ArrayLengthValidator<TYPE>::hasLength() const { return this->m_hasArraySize; }
52
57template <typename TYPE> bool ArrayLengthValidator<TYPE>::hasMinLength() const { return this->m_hasArraySizeMin; }
58
63template <typename TYPE> bool ArrayLengthValidator<TYPE>::hasMaxLength() const { return this->m_hasArraySizeMax; }
64
69template <typename TYPE> const size_t &ArrayLengthValidator<TYPE>::getLength() const { return this->m_arraySize; }
70
75template <typename TYPE> const size_t &ArrayLengthValidator<TYPE>::getMinLength() const { return this->m_arraySizeMin; }
76
81template <typename TYPE> const size_t &ArrayLengthValidator<TYPE>::getMaxLength() const { return this->m_arraySizeMax; }
82
88template <typename TYPE> void ArrayLengthValidator<TYPE>::setLength(const size_t &value) {
89 this->m_hasArraySize = true;
90 this->m_arraySize = value;
91 this->clearLengthMax();
92 this->clearLengthMin();
93}
94
99template <typename TYPE> void ArrayLengthValidator<TYPE>::setLengthMin(const size_t &value) {
100 this->m_hasArraySizeMin = true;
101 this->m_arraySizeMin = value;
102 this->clearLength();
103}
108template <typename TYPE> void ArrayLengthValidator<TYPE>::setLengthMax(const size_t &value) {
109 this->m_hasArraySizeMax = true;
110 this->m_arraySizeMax = value;
111 this->clearLength();
112}
113
118template <typename TYPE> void ArrayLengthValidator<TYPE>::clearLength() {
119 this->m_hasArraySize = false;
120 this->m_arraySize = size_t(0);
121}
122
127template <typename TYPE> void ArrayLengthValidator<TYPE>::clearLengthMin() {
128 this->m_hasArraySizeMin = false;
129 this->m_arraySizeMin = size_t(0);
130}
131
136template <typename TYPE> void ArrayLengthValidator<TYPE>::clearLengthMax() {
137 this->m_hasArraySizeMax = false;
138 this->m_arraySizeMax = size_t(0);
139}
140
145template <typename TYPE> IValidator_sptr ArrayLengthValidator<TYPE>::clone() const {
146 return std::make_shared<ArrayLengthValidator>(*this);
147}
148
154template <typename TYPE> std::string ArrayLengthValidator<TYPE>::checkValidity(const std::vector<TYPE> &value) const {
155 if (this->hasLength() && value.size() != this->m_arraySize) {
156 return "Incorrect size";
157 }
158 if (this->hasMinLength() && value.size() < this->m_arraySizeMin) {
159 return "Array size too short";
160 }
161 if (this->hasMaxLength() && value.size() > this->m_arraySizeMax) {
162 return "Array size too long";
163 }
164 return "";
165}
166
167// Required explicit instantiations
169// TypedValidator.h declares this with MANTID_KERNEL_EXTERN_TEMPLATE, which already carries the export attribute, so gcc
170// warns that the one here is ignored.
171GNU_DIAG_OFF("attributes")
172template class MANTID_KERNEL_DLL TypedValidator<std::vector<double>>;
173GNU_DIAG_ON("attributes")
175template class ArrayLengthValidator<double>;
176template class ArrayLengthValidator<int32_t>;
177template class ArrayLengthValidator<int64_t>;
178template class ArrayLengthValidator<std::string>;
179#if defined(_WIN32) || defined(__clang__) && defined(__APPLE__)
180template class ArrayLengthValidator<long>;
181#endif
182} // namespace Mantid::Kernel
double value
The value of the point.
Definition FitMW.cpp:51
#define GNU_DIAG_ON(x)
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
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.
STL namespace.