Mantid
Loading...
Searching...
No Matches
SingleValueParameter.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
10#ifndef Q_MOC_RUN
11#include <boost/algorithm/string.hpp>
12#include <boost/format.hpp>
13#endif
14
15#include <string>
16
17namespace Mantid {
18namespace API {
19
28template <typename Derived, typename ValType> class SingleValueParameter : public ImplicitFunctionParameter {
29public:
30 using ValueType = ValType;
35 std::string toXMLString() const override;
36 Derived &operator=(const Derived &other);
37 bool operator==(const Derived &other) const;
38 bool operator!=(const Derived &other) const;
39 ValType getValue() const;
40 bool isValid() const override;
41
42protected:
43 ValType m_value;
45};
46
47//----------------------------------------------------------------
48/* Getter for the valid state.
49@return raw underlying value.
50*/
51template <typename Derived, typename ValType> ValType SingleValueParameter<Derived, ValType>::getValue() const {
52 return m_value;
53}
54
55//----------------------------------------------------------------
56/* Getter for the valid state.
57@return true if the object is in a valid state.
58*/
59template <typename Derived, typename ValType> bool SingleValueParameter<Derived, ValType>::isValid() const {
60 return m_isValid;
61}
62
63//----------------------------------------------------------------
64/* Overloaded assignement operator. Utilises CRTP to peform assignements in
65terms of derived class.
66@param other : object to use as origin for assignment.
67@return Ref to assigned object.
68*/
69template <typename Derived, typename ValType>
71 if (&other != this) {
72 this->m_isValid = other.m_isValid;
73 this->m_value = other.m_value;
74 }
75 return *(dynamic_cast<Derived *>(this));
76}
77
78//----------------------------------------------------------------
79/* Overloaded equals operator.
80@param other : object to use as rhs of comparison.
81@return true only if the two objects are considered equal.
82*/
83template <typename Derived, typename ValType>
85 return other.m_value == this->m_value;
86}
87
88//----------------------------------------------------------------
89/* Overloaded not equals operator.
90@param other : object to use as rhs of comparison.
91@return true only if the two objects are not considered equal.
92*/
93template <typename Derived, typename ValType>
95 return !(*this == other);
96}
97
98//----------------------------------------------------------------
99/* Copy constructor
100@param other : ref to object to use as origin for the new instance.
101*/
102template <typename Derived, typename ValType>
106
107template <typename Derived, typename ValType>
110 m_value = other.m_value;
111 m_isValid = other.m_isValid;
112 return *this;
113}
114
116template <typename Derived, typename ValType>
118
119//----------------------------------------------------------------
120/* Constructor. Leads to a valid instance/state.
121@param value : Internal value for the object to wrap.
122*/
123template <typename Derived, typename ValType>
125
126//----------------------------------------------------------------
127/* Serializes the object to an xml string.
128@return xml string containing the underlying value.
129*/
130template <typename Derived, typename ValType> std::string SingleValueParameter<Derived, ValType>::toXMLString() const {
131 std::string valueXMLtext = ElementTraits<ValType>::format(m_value);
132 return this->parameterXMLTemplate(valueXMLtext);
133}
134
135//-----------------------------------------------------------------------------------------------------------------//
136// Macro for generating concrete types of SingleValueParameters.
137//
138// Use of macro enables parameter names to be assigned to each type.
139// Most of the work is done in the VectorParameter base class, which utilises
140// CRTP.
141//
142//-----------------------------------------------------------------------------------------------------------------//
143#define DECLARE_SINGLE_VALUE_PARAMETER(classname, type_) \
144 class classname : public Mantid::API::SingleValueParameter<classname, type_> { \
145 public: \
146 using SuperType = Mantid::API::SingleValueParameter<classname, type_>; \
147 static std::string parameterName() { return #classname; } \
148 classname(type_ value) : SuperType(value) {} \
149 classname() : SuperType() {} \
150 std::string getName() const override { return #classname; } \
151 classname *clone() const override { return new classname(m_value); } \
152 };
153} // namespace API
154} // namespace Mantid
const std::string & m_value
Definition Algorithm.cpp:72
double value
The value of the point.
Definition FitMW.cpp:51
Abstract parameter type for use with IImplicitFunctions.
SingleValueParameter is a templated base class implementing CRTP.
SingleValueParameter()
Default constructor. Object is created in invalid state.
bool operator!=(const Derived &other) const
bool operator==(const Derived &other) const
SingleValueParameter< Derived, ValType > & operator=(const SingleValueParameter< Derived, ValType > &other)
std::string toXMLString() const override
Helper class which provides the Collimation Length for SANS instruments.
Default ElementTraits SFINAE Typetraits are used to provide the correct formatting based on the eleme...