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//----------------------------------------------------------------------
12#include <typeinfo>
13
14namespace Mantid {
15namespace Kernel {
25template <typename HeldType> class DLLExport TypedValidator : public IValidator {
26protected:
28 virtual std::string checkValidity(const HeldType &) const = 0;
29
30private:
37 std::string check(const boost::any &value) const override {
38 try {
39 const HeldType *dataPtr = boost::any_cast<const HeldType *>(value);
40 return checkValidity(*dataPtr);
41 } catch (boost::bad_any_cast &) {
42 return "Value was not of expected type.";
43 }
44 }
45};
46
59template <typename ElementType> class DLLExport TypedValidator<std::shared_ptr<ElementType>> : public IValidator {
61 using ElementType_sptr = std::shared_ptr<ElementType>;
62
63protected:
65 virtual std::string checkValidity(const ElementType_sptr &) const = 0;
66
67private:
76 std::string check(const boost::any &value) const override {
77 try {
78 const ElementType_sptr typedValue = extractValue(value);
79 return checkValidity(typedValue);
80 } catch (std::invalid_argument &exc) {
81 return exc.what();
82 }
83 }
89 ElementType_sptr extractValue(const boost::any &value) const {
90// Despite the name and hash code being identical, operator== is returning false
91// in Release mode
92// with clang and libc++. The simplest workaround is to compare hash codes.
93#ifdef __clang__
94 if (value.type().hash_code() == m_dataitemTypeID.hash_code())
95#else
96 if (value.type() == m_dataitemTypeID)
97#endif
98 {
99 return extractFromDataItem(value);
100 } else {
101 return extractFromSharedPtr(value);
102 }
103 }
109 ElementType_sptr extractFromDataItem(const boost::any &value) const {
110 const DataItem_sptr data = boost::any_cast<DataItem_sptr>(value);
111 // First try and push it up to the type of the validator
112 ElementType_sptr typedValue = std::dynamic_pointer_cast<ElementType>(data);
113 if (!typedValue) {
114 throw std::invalid_argument("DataItem \"" + data->getName() + "\" is not of the expected type.");
115 }
116 return typedValue;
117 }
123 ElementType_sptr extractFromSharedPtr(const boost::any &value) const {
124 try {
125 return boost::any_cast<ElementType_sptr>(value);
126 } catch (boost::bad_any_cast &) {
127 throw std::invalid_argument("Value was not a shared_ptr type");
128 }
129 }
131 static const std::type_info &m_dataitemTypeID;
132};
133
135// This switch off an warning because oxygen could not figureout that this is a
136// specialized type of the general one.
139template <typename T>
140const std::type_info &TypedValidator<std::shared_ptr<T>>::m_dataitemTypeID = typeid(std::shared_ptr<DataItem>);
141} // namespace Kernel
143} // namespace Mantid
double value
The value of the point.
Definition: FitMW.cpp:51
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition: System.h:53
std::shared_ptr< DataItem > DataItem_sptr
Shared pointer to a DataItem.
Definition: DataItem.h:84
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.