Mantid
Loading...
Searching...
No Matches
VectorParameterParser.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2010 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//----------------------------------------------------------------------
10// Includes
11//----------------------------------------------------------------------
12#include "MantidAPI/DllConfig.h"
13
15
16#include <Poco/DOM/DOMParser.h>
17#include <Poco/DOM/Document.h>
18#include <Poco/DOM/Element.h>
19#include <Poco/DOM/NodeFilter.h>
20#include <Poco/DOM/NodeIterator.h>
21#include <Poco/DOM/NodeList.h>
22#include <Poco/File.h>
23#include <Poco/Path.h>
24#ifndef Q_MOC_RUN
25#include <boost/lexical_cast.hpp>
26#endif
27
28#include <cstddef>
29#include <stdexcept>
30#include <string>
31#include <vector>
32
33namespace Mantid {
34namespace API {
42template <class VectorValueParameterType>
44public:
45 VectorValueParameterType *parseVectorParameter(std::string sValue);
46
47 Mantid::API::ImplicitFunctionParameter *createParameter(Poco::XML::Element *parameterElement) override;
48
49 VectorValueParameterType *createWithoutDelegation(Poco::XML::Element *parameterElement);
50
52};
53
55
56//----------------------------------------------------------------------
57/* Parse the value aspect of the parameter only.
58@param sValue : parameter value in string form.
59@return new parameter object.
60*/
61template <typename VectorValueParameterType>
62VectorValueParameterType *VectorParameterParser<VectorValueParameterType>::parseVectorParameter(std::string sValue) {
63 std::vector<std::string> strs;
64 boost::split(strs, sValue, boost::is_any_of(","));
65
66 auto product = new VectorValueParameterType(strs.size());
67 using ValType = typename VectorValueParameterType::ValueType;
68
69 for (size_t i = 0; i < strs.size(); i++) {
70 boost::erase_all(strs[i], " ");
71 ValType value = boost::lexical_cast<ValType>(strs[i]);
72 product->addValue(i, value);
73 }
74 return product;
75}
76
77//----------------------------------------------------------------------
78/* Create parameter from xml element.
79@param parameterElement : XML element containing the parameter info.
80@return new parameter object.
81*/
82template <typename VectorValueParameterType>
85 std::string typeName = parameterElement->getChildElement("Type")->innerText();
86 if (VectorValueParameterType::parameterName() != typeName) {
87 if (!m_successor) {
88 throw std::runtime_error("No successor ParameterParser!");
89 }
90 return m_successor->createParameter(parameterElement);
91 } else {
92 std::string sParameterValue = parameterElement->getChildElement("Value")->innerText();
93 return parseVectorParameter(sParameterValue);
94 }
95}
96
97//------------------------------------------------------------------------------
98/* Creates a parameter from an xml element. This is single-shot. Does not defer
99to successor if it fails!.
100@param parameterElement : xml Element
101@return A fully constructed VectorValueParameterType.
102*/
103template <class VectorValueParameterType>
104VectorValueParameterType *
106 std::string typeName = parameterElement->getChildElement("Type")->innerText();
107 if (VectorValueParameterType::parameterName() != typeName) {
108 throw std::runtime_error("The attempted ::createWithoutDelegation failed. "
109 "The type provided does not match this parser.");
110 } else {
111 std::string sParameterValue = parameterElement->getChildElement("Value")->innerText();
112 return parseVectorParameter(sParameterValue);
113 }
114}
115
116//----------------------------------------------------------------------
117/* Setter for a successor parser.
118@param paramParser : successor
119*/
120template <typename VectorValueParameterType>
126} // namespace API
127} // namespace Mantid
double value
The value of the point.
Definition FitMW.cpp:51
boost::interprocess::unique_ptr< ImplicitFunctionParameterParser, DeleterPolicy< ImplicitFunctionParameterParser > > SuccessorType
Successor type.
Abstract parameter type for use with IImplicitFunctions.
XML parser for vector value (n elements) parameter types.
void setSuccessorParser(Mantid::API::ImplicitFunctionParameterParser *paramParser) override
VectorValueParameterType * createWithoutDelegation(Poco::XML::Element *parameterElement)
VectorValueParameterType * parseVectorParameter(std::string sValue)
Mantid::API::ImplicitFunctionParameter * createParameter(Poco::XML::Element *parameterElement) override
Helper class which provides the Collimation Length for SANS instruments.