Mantid
Loading...
Searching...
No Matches
VectorParameter.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
11#include <cstddef>
12#include <stdexcept>
13
14namespace Mantid {
15namespace API {
16
25template <typename Derived, typename ElemType> class VectorParameter : public ImplicitFunctionParameter {
26public:
27 using ValueType = ElemType;
29 VectorParameter(size_t size);
32 ~VectorParameter() override;
33 void addValue(const size_t index, const ElemType &value);
34 std::string toXMLString() const override;
35 Derived &assignFrom(const Derived &other);
36 bool operator==(const Derived &other) const;
37 bool operator!=(const Derived &other) const;
38 bool isValid() const override;
39 ElemType &operator[](int index);
40 const ElemType *getPointerToStart();
41 size_t getSize() const;
42 ElemType &at(size_t index);
43
44protected:
45 ElemType *m_arry;
46 size_t m_size;
48
49private:
50 Derived &operator=(const Derived &other);
51};
52
53//----------------------------------------------------------------------
54/* Getter for the valid state.
55@return true if object is valid.
56*/
57template <typename Derived, typename ElemType> bool VectorParameter<Derived, ElemType>::isValid() const {
58 return m_isValid;
59}
60
61//----------------------------------------------------------------------
62/* Assignment
63@param other : object to assign from.
64@return ref to assined object.
65*/
66template <typename Derived, typename ElemType>
67Derived &VectorParameter<Derived, ElemType>::assignFrom(const Derived &other) {
68 if (other.getSize() != this->getSize()) {
69 throw std::runtime_error("Cannot assign between VectorParameters where the "
70 "size of the vectors are different.");
71 }
72 if (&other != this) {
73 this->m_isValid = other.m_isValid;
74 for (size_t i = 0; i < other.getSize(); i++) {
75 this->m_arry[i] = other.m_arry[i];
76 }
77 }
78 return *(dynamic_cast<Derived *>(this));
79}
80
81//----------------------------------------------------------------------
82/* Overriden equality operator
83@param other : object to compare with
84@return true if to be considered equal
85*/
86template <typename Derived, typename ElemType>
87bool VectorParameter<Derived, ElemType>::operator==(const Derived &other) const {
88 if (other.m_isValid != this->m_isValid) {
89 return false; // Early termination
90 }
91 if (other.getSize() != this->getSize()) {
92 return false; // Early termination
93 }
94 for (size_t i = 0; i < other.getSize(); i++) {
95 if (this->m_arry[i] != other.m_arry[i]) {
96 return false;
97 }
98 }
99 return true;
100}
101
102//----------------------------------------------------------------------
103/* Overriden not equals operator
104@param other : object to compare with
105@return true if to be considered not equal
106*/
107template <typename Derived, typename ElemType>
108bool VectorParameter<Derived, ElemType>::operator!=(const Derived &other) const {
109 return !(*this == other);
110}
111
112//----------------------------------------------------------------------
113/* Copy constructor
114@param other : object to act as source of copy.
115*/
116template <typename Derived, typename ElemType>
120
121template <typename Derived, typename ElemType>
124 m_size = other.m_size;
125 m_isValid = other.m_isValid;
126 m_arry = new ElemType[other.getSize()];
127 for (size_t i = 0; i < other.getSize(); i++) {
128 this->m_arry[i] = other.m_arry[i];
129 }
130 return *this;
131}
132
134template <typename Derived, typename ElemType>
136 m_isValid = false;
137}
138
139//----------------------------------------------------------------------
140/* Constructor
141@param size : size of array
142*/
143template <typename Derived, typename ElemType>
145 m_arry = new ElemType[size];
146 m_isValid = true;
147}
148
150template <typename Derived, typename ElemType> VectorParameter<Derived, ElemType>::~VectorParameter() {
151 delete[] m_arry;
152}
153
154//----------------------------------------------------------------------
155/* Setter for values on vector
156@param value : Value to add.
157*/
158template <typename Derived, typename ElemType>
159void VectorParameter<Derived, ElemType>::addValue(const size_t index, const ElemType &value) {
160 m_arry[index] = value;
161}
162
163//----------------------------------------------------------------------
164/* Serialize the object to an xml string.
165@return string containing object in xml serialized form.
166*/
167template <typename Derived, typename ElemType> std::string VectorParameter<Derived, ElemType>::toXMLString() const {
168 if (!m_isValid) {
169 throw std::runtime_error("Cannot serialize VectorParameter if it is not valid!");
170 }
171 std::string valueXMLtext;
172 for (size_t i = 0; i < m_size; i++) {
173 if (i < (m_size - 1)) {
174 valueXMLtext.append(ElementTraits<ElemType>::formatCS(m_arry[i])); // Comma-seperated
175 } else {
176 valueXMLtext.append(ElementTraits<ElemType>::format(m_arry[i])); // No comma sepearation for last value
177 }
178 }
179 return this->parameterXMLTemplate(valueXMLtext);
180}
181
182//----------------------------------------------------------------------
183/* Overrriden array operator
184@param index : array index to access.
185@return ref to the element at the specified index.
186*/
187template <typename Derived, typename ElemType> ElemType &VectorParameter<Derived, ElemType>::operator[](int index) {
188 return m_arry[index];
189}
190
191//----------------------------------------------------------------------
192/* Getter for the vector size.
193@return the size of the vector
194*/
195template <typename Derived, typename ElemType> size_t VectorParameter<Derived, ElemType>::getSize() const {
196 return m_size;
197}
198
199//----------------------------------------------------------------------
200/* Getter to pointer to the start of the underlying array
201@return pointer to start.
202*/
203template <typename Derived, typename ElemType> const ElemType *VectorParameter<Derived, ElemType>::getPointerToStart() {
204 return m_arry;
205}
206
207//----------------------------------------------------------------------
208/* Convienince array access method
209@param index : index to get element at
210@return element at index
211*/
212template <typename Derived, typename ElemType> ElemType &VectorParameter<Derived, ElemType>::at(size_t index) {
213 return m_arry[index];
214}
215
216//-----------------------------------------------------------------------------------------------------------------//
217// Macro for generating concrete types of VectorParameters.
218//
219// Use of macro enables parameter names to be assigned to each type.
220// Most of the work is done in the VectorParamter base class, which utilises
221// CRTP.
222//-----------------------------------------------------------------------------------------------------------------//
223#define DECLARE_VECTOR_PARAMETER(classname, type_) \
224 class classname : public Mantid::API::VectorParameter<classname, type_> { \
225 public: \
226 using SuperType = Mantid::API::VectorParameter<classname, type_>; \
227 static std::string parameterName() { return #classname; } \
228 classname() : SuperType() {} \
229 classname(size_t index) : SuperType(index) {} \
230 std::string getName() const override { return #classname; } \
231 classname *clone() const override { return new classname(*this); } \
232 };
233} // namespace API
234} // namespace Mantid
size_t m_size
Maximum size of the store.
double value
The value of the point.
Definition FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
Abstract parameter type for use with IImplicitFunctions.
VectorParameter is abstract type implementing curiously recurring template pattern to implement commo...
VectorParameter()
Default constructor.
bool isValid() const override
bool operator==(const Derived &other) const
ElemType & at(size_t index)
~VectorParameter() override
Destructor.
std::string toXMLString() const override
VectorParameter< Derived, ElemType > & operator=(const VectorParameter< Derived, ElemType > &other)
bool operator!=(const Derived &other) const
Derived & assignFrom(const Derived &other)
const ElemType * getPointerToStart()
Derived & operator=(const Derived &other)
void addValue(const size_t index, const ElemType &value)
ElemType & operator[](int index)
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...