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