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"
11
12#ifndef Q_MOC_RUN
13#include <boost/any.hpp>
14#include <memory>
15#endif
16#include <stdexcept>
17#include <vector>
18
19namespace Mantid {
20namespace Kernel {
21// Forward declaration so that the typedef std::shared_ptr<Validator>
22// understand it
23class IValidator;
24
26using IValidator_sptr = std::shared_ptr<IValidator>;
27
28namespace {
31template <class T> struct IsPtrType : public std::is_pointer<T> {};
34template <class T> struct IsPtrType<std::shared_ptr<T>> : public std::true_type {};
35template <> struct IsPtrType<decltype(nullptr)> : public std::true_type {};
36} // namespace
37
43class MANTID_KERNEL_DLL IValidator {
44public:
46 IValidator() = default;
47
49 virtual ~IValidator() = default;
50
51 //------------------------------------------------------------------------------------------------------------
58 template <typename TYPE> std::string isValid(const TYPE &value) const { return runCheck(value, IsPtrType<TYPE>()); }
59
67 std::string isValid(const char *value) const { return isValid(std::string(value)); }
68
69 //------------------------------------------------------------------------------------------------------------
77 virtual std::vector<std::string> allowedValues() const { return std::vector<std::string>(); }
78
82 virtual bool isMultipleSelectionAllowed() { return false; };
83
92 virtual std::string getValueForAlias(const std::string &alias) const {
93 UNUSED_ARG(alias);
94 throw std::invalid_argument("Validator does'n support value aliasing.");
95 }
96
98 virtual IValidator_sptr clone() const = 0;
99
105 virtual std::string check(const boost::any &) const = 0;
106
107private:
113 template <typename T> std::string runCheck(const T &value, const std::false_type &) const {
114 const T *valuePtr = &value; // Avoid a copy by storing the pointer in the any holder
115 return check(boost::any(valuePtr));
116 }
117
123 template <typename T> std::string runCheck(const T &value, const std::true_type &) const {
124 return runCheckWithDataItemPtr(value, std::integral_constant < bool,
125 std::is_convertible<T, DataItem_sptr>::value &&
126 !std::is_same<T, decltype(nullptr)>::value > ());
127 }
134 template <typename T> std::string runCheckWithDataItemPtr(const T &value, const std::false_type &) const {
135 return check(boost::any(value));
136 }
143 template <typename T> std::string runCheckWithDataItemPtr(const T &value, const std::true_type &) const {
144 return check(boost::any(std::static_pointer_cast<DataItem>(value)));
145 }
146};
147
148} // namespace Kernel
149} // 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:64
IValidator is the basic interface for all validators for properties.
Definition: IValidator.h:43
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:113
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:92
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:143
virtual bool isMultipleSelectionAllowed()
Is Multiple Selection Allowed.
Definition: IValidator.h:82
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:67
std::string isValid(const TYPE &value) const
Calls the validator.
Definition: IValidator.h:58
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:123
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:134
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:77
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.
Definition: IValidator.h:26
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.