Skip to content

Commit

Permalink
adjust template for arrayBoundedValidator Re #9777
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewBowles committed Jul 17, 2017
1 parent 3750b70 commit 4ee1283
Showing 1 changed file with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
#include "MantidKernel/ArrayBoundedValidator.h"
#include "MantidPythonInterface/kernel/IsNone.h"
#include <boost/python/class.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/make_constructor.hpp>



using Mantid::Kernel::ArrayBoundedValidator;
using Mantid::Kernel::IValidator;
using namespace boost::python;

namespace {
/**
* Factory function to allow more flexibility in the constructor
* @param lower An optional lower bound
* @param upper An optional upper bound
* @returns A pointer to a new ArrayBoundedValidator object
*/
template <typename T>
ArrayBoundedValidator<T> *createArrayBoundedValidator(object lower = object(),
object upper = object()) {
auto validator = new ArrayBoundedValidator<T>();
if (!Mantid::PythonInterface::isNone(lower)) {
validator->setLower(extract<T>(lower));
}
if (!Mantid::PythonInterface::isNone(upper)) {
validator->setUpper(extract<T>(upper));
}
return validator;
}

#define EXPORT_ARRAYBOUNDEDVALIDATOR(type, prefix) \
class_<ArrayBoundedValidator<type>, bases<IValidator>, boost::noncopyable>( \
#prefix "ArrayBoundedValidator") \
.def(init<type, type>( \
(arg("self"), arg("lowerBound"), arg("upperBound")), \
"A validator to ensure each value is in the given range")) \
.def("__init__", \
make_constructor( \
&createArrayBoundedValidator<type>, default_call_policies(), \
(arg("lower") = object(), arg("upper") = object()))) \
.def("hasLower", &ArrayBoundedValidator<type>::hasLower, arg("self"), \
"Returns true if a lower bound has been set") \
.def("hasUpper", &ArrayBoundedValidator<type>::hasUpper, arg("self"), \
Expand All @@ -33,8 +57,10 @@ namespace {
.def("clearUpper", &ArrayBoundedValidator<type>::clearUpper, \
arg("self"), "Clear any set upper bound");
}

void export_ArrayBoundedValidator() {
EXPORT_ARRAYBOUNDEDVALIDATOR(double, Float);
EXPORT_ARRAYBOUNDEDVALIDATOR(long, Int);

}

0 comments on commit 4ee1283

Please sign in to comment.