Mantid
Loading...
Searching...
No Matches
IValidator.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 "MantidKernel/DllConfig.h"
12
13#ifndef Q_MOC_RUN
14#include <boost/any.hpp>
15#include <memory>
16#endif
17#include <stdexcept>
18#include <vector>
19
20namespace Mantid {
21namespace Kernel {
22namespace {
25template <class T> struct IsPtrType : public std::is_pointer<T> {};
28template <class T> struct IsPtrType<std::shared_ptr<T>> : public std::true_type {};
29template <> struct IsPtrType<decltype(nullptr)> : public std::true_type {};
30} // namespace
31
37class MANTID_KERNEL_DLL IValidator {
38public:
40 IValidator() = default;
41
43 virtual ~IValidator() = default;
44
45 //------------------------------------------------------------------------------------------------------------
52 template <typename TYPE> std::string isValid(const TYPE &value) const { return runCheck(value, IsPtrType<TYPE>()); }
53
61 std::string isValid(const char *value) const { return isValid(std::string(value)); }
62
63 //------------------------------------------------------------------------------------------------------------
71 virtual std::vector<std::string> allowedValues() const { return std::vector<std::string>(); }
72
76 virtual bool isMultipleSelectionAllowed() { return false; };
77
86 virtual std::string getValueForAlias(const std::string &alias) const {
87 UNUSED_ARG(alias);
88 throw std::invalid_argument("Validator does'n support value aliasing.");
89 }
90
92 virtual IValidator_sptr clone() const = 0;
93
99 virtual std::string check(const boost::any &) const = 0;
100
101private:
107 template <typename T> std::string runCheck(const T &value, const std::false_type &) const {
108 const T *valuePtr = &value; // Avoid a copy by storing the pointer in the any holder
109 return check(boost::any(valuePtr));
110 }
111
117 template <typename T> std::string runCheck(const T &value, const std::true_type &) const {
118 return runCheckWithDataItemPtr(value,
119 std::integral_constant<bool, std::is_convertible<T, DataItem_sptr>::value &&
120 !std::is_same<T, decltype(nullptr)>::value>());
121 }
128 template <typename T> std::string runCheckWithDataItemPtr(const T &value, const std::false_type &) const {
129 return check(boost::any(value));
130 }
137 template <typename T> std::string runCheckWithDataItemPtr(const T &value, const std::true_type &) const {
138 return check(boost::any(std::static_pointer_cast<DataItem>(value)));
139 }
140};
141
142} // namespace Kernel
143} // namespace Mantid
double value
The value of the point.
Definition FitMW.cpp:51
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition System.h:44
IValidator is the basic interface for all validators for properties.
Definition IValidator.h:37
std::string runCheck(const T &value, const std::false_type &) const
Calls the validator for a type that is not a pointer type.
Definition IValidator.h:107
IValidator()=default
Constructor.
virtual std::string getValueForAlias(const std::string &alias) const
Implement this method for validators which wish to support aliasing for alloeed values.
Definition IValidator.h:86
std::string runCheckWithDataItemPtr(const T &value, const std::true_type &) const
Calls the validator for a pointer type that IS convertible to DataItem_sptr.
Definition IValidator.h:137
virtual bool isMultipleSelectionAllowed()
Is Multiple Selection Allowed.
Definition IValidator.h:76
std::string isValid(const char *value) const
Deal with a C-style string by first converting it to a std::string so that boost::any can deal with i...
Definition IValidator.h:61
std::string isValid(const TYPE &value) const
Calls the validator.
Definition IValidator.h:52
std::string runCheck(const T &value, const std::true_type &) const
Calls the validator for a type that is either a raw pointer or a std::shared pointer.
Definition IValidator.h:117
std::string runCheckWithDataItemPtr(const T &value, const std::false_type &) const
Calls the validator for a pointer type that is NOT convertible to DataItem_sptr.
Definition IValidator.h:128
virtual IValidator_sptr clone() const =0
Make a copy of the present type of validator.
virtual std::vector< std::string > allowedValues() const
The set of allowed values that this validator may have, if a discrete set exists.
Definition IValidator.h:71
virtual std::string check(const boost::any &) const =0
Checks the value based on the validator's rules.
virtual ~IValidator()=default
virtual Destructor
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.