Skip to content

Commit

Permalink
Exclusive bounds for ArrayBoundedValidator
Browse files Browse the repository at this point in the history
Refactor ArrayBoundedValidator to be more in line with BoundedValidator
by adding methods to deal with exclusive bounds.

Re #9777
  • Loading branch information
Antti Soininen committed Mar 11, 2019
1 parent 75e6bb4 commit a1f4a68
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 133 deletions.
47 changes: 26 additions & 21 deletions Framework/Kernel/inc/MantidKernel/ArrayBoundedValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/DllConfig.h"
#include "MantidKernel/IValidator.h"
#include "MantidKernel/TypedValidator.h"
#include <string>
#include <vector>

Expand All @@ -26,42 +24,49 @@ namespace Kernel {
@date 09/11/2010
*/
template <typename TYPE>
class MANTID_KERNEL_DLL ArrayBoundedValidator
class MANTID_KERNEL_DLL ArrayBoundedValidator final
: public TypedValidator<std::vector<TYPE>> {
public:
ArrayBoundedValidator();
ArrayBoundedValidator(const ArrayBoundedValidator<TYPE> &abv);
ArrayBoundedValidator(const TYPE lowerBound, const TYPE upperBound);
ArrayBoundedValidator(BoundedValidator<TYPE> &bv);
~ArrayBoundedValidator() override;
ArrayBoundedValidator() = default;
ArrayBoundedValidator(const ArrayBoundedValidator<TYPE> &abv) noexcept;
ArrayBoundedValidator(const TYPE lowerBound, const TYPE upperBound) noexcept;
ArrayBoundedValidator(TYPE lowerBound, TYPE upperBound,
bool exclusive) noexcept;
ArrayBoundedValidator(BoundedValidator<TYPE> &bv) noexcept;
/// Clone the current state
IValidator_sptr clone() const override;
/// Return the object that checks the bounds
boost::shared_ptr<BoundedValidator<TYPE>> getValidator() const;

/// Return if it has a lower bound
bool hasLower() const;
bool hasLower() const noexcept;
/// Return if it has a lower bound
bool hasUpper() const;
bool hasUpper() const noexcept;
/// Return the lower bound value
const TYPE &lower() const;
TYPE lower() const noexcept;
/// Return the upper bound value
const TYPE &upper() const;
TYPE upper() const noexcept;
bool isLowerExclusive() const noexcept;
/// Check if upper bound is exclusive
bool isUpperExclusive() const noexcept;
/// Set the lower bound to be exclusive
void setLowerExclusive(const bool exclusive) noexcept;
/// Set the upper bound to be exclusive
void setUpperExclusive(const bool exclusive) noexcept;

/// Set both the upper and lower bounds to be exclusive
void setExclusive(const bool exclusive) noexcept;

/// Set lower bound value
void setLower(const TYPE &value);
void setLower(const TYPE &value) noexcept;
/// Set upper bound value
void setUpper(const TYPE &value);
void setUpper(const TYPE &value) noexcept;
/// Clear lower bound value
void clearLower();
void clearLower() noexcept;
/// Clear upper bound value
void clearUpper();
void clearUpper() noexcept;

private:
std::string checkValidity(const std::vector<TYPE> &value) const override;

/// The object used to do the actual validation
boost::shared_ptr<BoundedValidator<TYPE>> boundVal;
BoundedValidator<TYPE> m_actualValidator;
};

} // namespace Kernel
Expand Down
40 changes: 22 additions & 18 deletions Framework/Kernel/inc/MantidKernel/BoundedValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ namespace Kernel {
@date 28/11/2007
*/
template <class TYPE>
class DLLExport BoundedValidator : public TypedValidator<TYPE> {
class DLLExport BoundedValidator final : public TypedValidator<TYPE> {
public:
/// No-arg Constructor
BoundedValidator()
BoundedValidator() noexcept
: TypedValidator<TYPE>(), m_hasLowerBound(false), m_hasUpperBound(false),
m_lowerExclusive(false), m_upperExclusive(false), m_lowerBound(TYPE()),
m_upperBound(TYPE()) {}
Expand All @@ -39,65 +39,69 @@ class DLLExport BoundedValidator : public TypedValidator<TYPE> {
* @param exclusive :: make bounds exclusive (default inclusive)
*/
BoundedValidator(const TYPE lowerBound, const TYPE upperBound,
bool exclusive = false)
bool exclusive = false) noexcept
: TypedValidator<TYPE>(), m_hasLowerBound(true), m_hasUpperBound(true),
m_lowerExclusive(exclusive), m_upperExclusive(exclusive),
m_lowerBound(lowerBound), m_upperBound(upperBound) {}

/// Return if it has a lower bound
bool hasLower() const { return m_hasLowerBound; }
bool hasLower() const noexcept { return m_hasLowerBound; }
/// Return if it has a lower bound
bool hasUpper() const { return m_hasUpperBound; }
bool hasUpper() const noexcept { return m_hasUpperBound; }
/// Return the lower bound value
const TYPE &lower() const { return m_lowerBound; }
TYPE lower() const noexcept { return m_lowerBound; }
/// Return the upper bound value
const TYPE &upper() const { return m_upperBound; }
TYPE upper() const noexcept { return m_upperBound; }
/// Check if lower bound is exclusive
bool isLowerExclusive() const { return m_lowerExclusive; }
bool isLowerExclusive() const noexcept { return m_lowerExclusive; }
/// Check if upper bound is exclusive
bool isUpperExclusive() const { return m_upperExclusive; }
bool isUpperExclusive() const noexcept { return m_upperExclusive; }
/// Set the lower bound to be exclusive
void setLowerExclusive(const bool exclusive) { m_lowerExclusive = exclusive; }
void setLowerExclusive(const bool exclusive) noexcept {
m_lowerExclusive = exclusive;
}
/// Set the upper bound to be exclusive
void setUpperExclusive(const bool exclusive) { m_upperExclusive = exclusive; }
void setUpperExclusive(const bool exclusive) noexcept {
m_upperExclusive = exclusive;
}

/// Set both the upper and lower bounds to be exclusive
void setExclusive(const bool exclusive) {
void setExclusive(const bool exclusive) noexcept {
setLowerExclusive(exclusive);
setUpperExclusive(exclusive);
}

/// Set lower bound value
void setLower(const TYPE &value) {
void setLower(const TYPE &value) noexcept {
m_hasLowerBound = true;
m_lowerBound = value;
}

/// Set upper bound value
void setUpper(const TYPE &value) {
void setUpper(const TYPE &value) noexcept {
m_hasUpperBound = true;
m_upperBound = value;
}

/// Clear lower bound value
void clearLower() {
void clearLower() noexcept {
m_hasLowerBound = false;
m_lowerBound = TYPE();
}
/// Clear upper bound value
void clearUpper() {
void clearUpper() noexcept {
m_hasUpperBound = false;
m_upperBound = TYPE();
}

/// Set both bounds (lower and upper) at the same time
void setBounds(const TYPE &lower, const TYPE &upper) {
void setBounds(const TYPE &lower, const TYPE &upper) noexcept {
setLower(lower);
setUpper(upper);
}

/// Clear both bounds (lower and upper) at the same time
void clearBounds() {
void clearBounds() noexcept {
clearLower();
clearUpper();
}
Expand Down
120 changes: 66 additions & 54 deletions Framework/Kernel/src/ArrayBoundedValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,42 @@
namespace Mantid {
namespace Kernel {

/**
* Default constructor
*/
template <typename TYPE>
ArrayBoundedValidator<TYPE>::ArrayBoundedValidator()
: TypedValidator<std::vector<TYPE>>(),
boundVal(boost::make_shared<BoundedValidator<TYPE>>()) {}

/**
* Copy constructor
* @param abv :: the ArrayBoundedValidator to copy
*/
template <typename TYPE>
ArrayBoundedValidator<TYPE>::ArrayBoundedValidator(
const ArrayBoundedValidator<TYPE> &abv)
: TypedValidator<std::vector<TYPE>>() {
this->boundVal = boost::dynamic_pointer_cast<BoundedValidator<TYPE>>(
abv.boundVal->clone());
}
const ArrayBoundedValidator<TYPE> &abv) noexcept
: TypedValidator<std::vector<TYPE>>(),
m_actualValidator(abv.m_actualValidator) {}

/**
* Constructor via bounds parameters
* @param lowerBound :: the lower bound value to validate
* @param upperBound :: the upper bound value to validate
*/
template <typename TYPE>
ArrayBoundedValidator<TYPE>::ArrayBoundedValidator(const TYPE lowerBound,
const TYPE upperBound)
ArrayBoundedValidator<TYPE>::ArrayBoundedValidator(
const TYPE lowerBound, const TYPE upperBound) noexcept
: TypedValidator<std::vector<TYPE>>(),
boundVal(
boost::make_shared<BoundedValidator<TYPE>>(lowerBound, upperBound)) {}
m_actualValidator(lowerBound, upperBound) {}

/**
* Constructor via a BoundedValidator
* @param bv :: the BoundedValidator object to use
*/
template <typename TYPE>
ArrayBoundedValidator<TYPE>::ArrayBoundedValidator(BoundedValidator<TYPE> &bv) {
this->boundVal =
boost::dynamic_pointer_cast<BoundedValidator<TYPE>>(bv.clone());
}
ArrayBoundedValidator<TYPE>::ArrayBoundedValidator(TYPE lowerBound,
TYPE upperBound,
bool exclusive) noexcept
: TypedValidator<std::vector<TYPE>>(),
m_actualValidator(lowerBound, upperBound, exclusive) {}

/**
* Object destructor
* Constructor via a BoundedValidator
* @param bv :: the BoundedValidator object to use
*/
template <typename TYPE>
ArrayBoundedValidator<TYPE>::~ArrayBoundedValidator() {}
ArrayBoundedValidator<TYPE>::ArrayBoundedValidator(
BoundedValidator<TYPE> &bv) noexcept
: TypedValidator<std::vector<TYPE>>(), m_actualValidator(bv) {}

/**
* Create a clone of the current ArrayBoundedValidator
Expand All @@ -69,16 +58,6 @@ IValidator_sptr ArrayBoundedValidator<TYPE>::clone() const {
return boost::make_shared<ArrayBoundedValidator<TYPE>>(*this);
}

/**
* Accessor for the stored BoundedValidator
* @return a pointer to the stored BoundedValidator
*/
template <typename TYPE>
boost::shared_ptr<BoundedValidator<TYPE>>
ArrayBoundedValidator<TYPE>::getValidator() const {
return this->boundVal;
}

/**
* Function that actually does the work of checking the validity of the
* array elements.
Expand All @@ -95,7 +74,7 @@ std::string ArrayBoundedValidator<TYPE>::checkValidity(
typename std::vector<TYPE>::const_iterator it;
std::size_t index = 0;
for (it = value.begin(); it != value.end(); ++it) {
std::string retval = this->boundVal->isValid(*it);
std::string retval = m_actualValidator.isValid(*it);
if (!retval.empty()) {
error << "At index " << index << ": " << retval;
}
Expand All @@ -105,40 +84,73 @@ std::string ArrayBoundedValidator<TYPE>::checkValidity(
return error.str();
}

template <typename TYPE> bool ArrayBoundedValidator<TYPE>::hasLower() const {
return this->boundVal->hasLower();
template <typename TYPE>
bool ArrayBoundedValidator<TYPE>::hasLower() const noexcept {
return m_actualValidator.hasLower();
}

template <typename TYPE> bool ArrayBoundedValidator<TYPE>::hasUpper() const {
return this->boundVal->hasUpper();
template <typename TYPE>
bool ArrayBoundedValidator<TYPE>::hasUpper() const noexcept {
return m_actualValidator.hasUpper();
}

template <typename TYPE>
const TYPE &ArrayBoundedValidator<TYPE>::lower() const {
return this->boundVal->lower();
TYPE ArrayBoundedValidator<TYPE>::lower() const noexcept {
return m_actualValidator.lower();
}

template <typename TYPE>
const TYPE &ArrayBoundedValidator<TYPE>::upper() const {
return this->boundVal->upper();
TYPE ArrayBoundedValidator<TYPE>::upper() const noexcept {
return m_actualValidator.upper();
}

template <typename TYPE>
void ArrayBoundedValidator<TYPE>::setLower(const TYPE &value) {
this->boundVal->setLower(value);
bool ArrayBoundedValidator<TYPE>::isLowerExclusive() const noexcept {
return m_actualValidator.isLowerExclusive();
}
/// Check if upper bound is exclusive
template <typename TYPE>
bool ArrayBoundedValidator<TYPE>::isUpperExclusive() const noexcept {
return m_actualValidator.isUpperExclusive();
}
/// Set the lower bound to be exclusive
template <typename TYPE>
void ArrayBoundedValidator<TYPE>::setLowerExclusive(
const bool exclusive) noexcept {
m_actualValidator.setLowerExclusive(exclusive);
}
/// Set the upper bound to be exclusive
template <typename TYPE>
void ArrayBoundedValidator<TYPE>::setUpperExclusive(
const bool exclusive) noexcept {
m_actualValidator.setUpperExclusive(exclusive);
}

/// Set both the upper and lower bounds to be exclusive
template <typename TYPE>
void ArrayBoundedValidator<TYPE>::setUpper(const TYPE &value) {
this->boundVal->setUpper(value);
void ArrayBoundedValidator<TYPE>::setExclusive(const bool exclusive) noexcept {
m_actualValidator.setLowerExclusive(exclusive);
m_actualValidator.setUpperExclusive(exclusive);
}

template <typename TYPE> void ArrayBoundedValidator<TYPE>::clearLower() {
this->boundVal->clearLower();
template <typename TYPE>
void ArrayBoundedValidator<TYPE>::setLower(const TYPE &value) noexcept {
m_actualValidator.setLower(value);
}

template <typename TYPE>
void ArrayBoundedValidator<TYPE>::setUpper(const TYPE &value) noexcept {
m_actualValidator.setUpper(value);
}

template <typename TYPE> void ArrayBoundedValidator<TYPE>::clearUpper() {
this->boundVal->clearUpper();
template <typename TYPE>
void ArrayBoundedValidator<TYPE>::clearLower() noexcept {
m_actualValidator.clearLower();
}

template <typename TYPE>
void ArrayBoundedValidator<TYPE>::clearUpper() noexcept {
m_actualValidator.clearUpper();
}

// Required explicit instantiations
Expand Down

0 comments on commit a1f4a68

Please sign in to comment.