Mantid
Loading...
Searching...
No Matches
TypedValidator.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2012 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//----------------------------------------------------------------------
9// Includes
10//----------------------------------------------------------------------
11#include "MantidKernel/DllConfig.h"
13#include <string>
14#include <typeinfo>
15#include <vector>
16
17namespace Mantid {
18namespace Kernel {
28#if !defined(_WIN32)
29template <typename HeldType> class MANTID_KERNEL_DLL TypedValidator : public IValidator {
30#else
31template <typename HeldType> class TypedValidator : public IValidator {
32#endif
33public:
34 TypedValidator() = default;
35 TypedValidator(const TypedValidator &) = default;
36 TypedValidator(TypedValidator &&);
37 TypedValidator &operator=(const TypedValidator &) = default;
38 TypedValidator &operator=(TypedValidator &&);
39 ~TypedValidator() override = default;
40
41protected:
43 virtual std::string checkValidity(const HeldType &) const = 0;
44
45private:
52 std::string check(const boost::any &value) const override {
53 try {
54 const HeldType *dataPtr = boost::any_cast<const HeldType *>(value);
55 return checkValidity(*dataPtr);
56 } catch (boost::bad_any_cast &) {
57 return "Value was not of expected type.";
58 }
59 }
60};
61
62// The move operations are defaulted out of line so that they are user-provided:
63// an exported explicit instantiation then exports them, whereas MSVC does not
64// export implicitly-declared move members.
65template <typename HeldType> TypedValidator<HeldType>::TypedValidator(TypedValidator &&) = default;
66template <typename HeldType> TypedValidator<HeldType> &TypedValidator<HeldType>::operator=(TypedValidator &&) = default;
67
80#if !defined(_WIN32)
81template <typename ElementType>
82class MANTID_KERNEL_DLL TypedValidator<std::shared_ptr<ElementType>> : public IValidator {
83#else
84template <typename ElementType> class TypedValidator<std::shared_ptr<ElementType>> : public IValidator {
85#endif
87 using ElementType_sptr = std::shared_ptr<ElementType>;
88
89protected:
91 virtual std::string checkValidity(const ElementType_sptr &) const = 0;
92
93private:
102 std::string check(const boost::any &value) const override {
103 try {
104 const ElementType_sptr typedValue = extractValue(value);
105 return checkValidity(typedValue);
106 } catch (std::invalid_argument &exc) {
107 return exc.what();
108 }
109 }
115 ElementType_sptr extractValue(const boost::any &value) const {
116// Despite the name and hash code being identical, operator== is returning false
117// in Release mode
118// with clang and libc++. The simplest workaround is to compare hash codes.
119#ifdef __clang__
120 if (value.type().hash_code() == dataitemTypeID().hash_code())
121#else
122 if (value.type() == dataitemTypeID())
123#endif
124 {
125 return extractFromDataItem(value);
126 } else {
127 return extractFromSharedPtr(value);
128 }
129 }
135 ElementType_sptr extractFromDataItem(const boost::any &value) const {
136 const DataItem_sptr data = boost::any_cast<DataItem_sptr>(value);
137 // First try and push it up to the type of the validator
138 ElementType_sptr typedValue = std::dynamic_pointer_cast<ElementType>(data);
139 if (!typedValue) {
140 throw std::invalid_argument("DataItem \"" + data->getName() + "\" is not of the expected type.");
141 }
142 return typedValue;
143 }
149 ElementType_sptr extractFromSharedPtr(const boost::any &value) const {
150 try {
151 return boost::any_cast<ElementType_sptr>(value);
152 } catch (boost::bad_any_cast &) {
153 throw std::invalid_argument("Value was not a shared_ptr type");
154 }
155 }
157 static const std::type_info &dataitemTypeID() { return typeid(std::shared_ptr<DataItem>); }
158};
159
160// Instantiated and exported by StartsWithValidator.cpp and ArrayLengthValidator.cpp. They are the bases of exported
161// validator classes, so consumers must not instantiate their own copy of the inline base members.
162MANTID_KERNEL_EXTERN_TEMPLATE(TypedValidator<std::string>);
163MANTID_KERNEL_EXTERN_TEMPLATE(TypedValidator<std::vector<double>>);
164
166// This switch off an warning because oxygen could not figureout that this is a
167// specialized type of the general one.
168} // namespace Kernel
170} // namespace Mantid
double value
The value of the point.
Definition FitMW.cpp:51
std::shared_ptr< DataItem > DataItem_sptr
Shared pointer to a DataItem.
Definition DataItem.h:84
MANTID_KERNEL_EXTERN_TEMPLATE(PropertyWithValue< Matrix< float > >)
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.