Mantid
Loading...
Searching...
No Matches
Parameter.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 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#include "MantidGeometry/DllConfig.h"
10#include "MantidKernel/Quat.h"
11#include "MantidKernel/V3D.h"
12#include <iomanip>
13#include <memory>
14#include <sstream>
15#include <stdexcept>
16#include <string>
17#include <type_traits>
18#include <typeinfo>
19#include <vector>
20
21namespace Mantid {
22
23namespace Kernel {
24template <class C, class Base, typename... Args> class Instantiator;
25}
26
27namespace Geometry {
28//--------------------------------------------------------------------------
29// Forward declarations
30//--------------------------------------------------------------------------
31class Instrument;
32class ParameterMap;
33class ParameterInfo;
34
42class MANTID_GEOMETRY_DLL Parameter {
43public:
45 virtual ~Parameter() = default;
46
48 const std::string &type() const { return m_type; }
50 const std::string &name() const { return m_name; }
52 const char *nameAsCString() const { return m_name.c_str(); }
54 const bool &visible() const { return m_visible; }
55
57 virtual Parameter *clone() const = 0;
58
60 virtual std::string asString() const { return m_str_value; }
61
63 virtual void fromString(const std::string &value) { m_str_value = value; }
64
67 template <class T> const T &value() const;
69 virtual void setDescription(const std::string &source) { m_description.assign(source); }
71 virtual const std::string &getDescription() const { return m_description; }
73 virtual std::string getShortDescription() const;
75 virtual void setVisible(const bool &visible) { m_visible = visible; }
77 bool operator==(const Parameter &rhs) const {
78 if (this->name() == rhs.name() && this->type() == rhs.type() && this->asString() == rhs.asString())
79 return true;
80 else
81 return false;
82 }
83
84protected:
85 friend class ParameterMap;
87 friend class ParameterInfo;
89 friend class Instrument;
90
95 template <class T> void set(const T &t);
96
97 friend class ParameterFactory;
99 Parameter() : m_type(""), m_name(""), m_str_value(""), m_visible(true), m_description("") {}
100
101private:
103 std::string m_type;
105 std::string m_name;
106 std::string m_str_value;
110 std::string m_description;
111};
112
114template <class Type> class ParameterType : public Parameter {
115public:
118
120 std::string asString() const override;
122 void fromString(const std::string &value) override;
123
125 inline const Type &value() const { return m_value; }
127 inline const Type &operator()() const { return m_value; }
128
129 Parameter *clone() const override { return new ParameterType(*this); }
130
131private:
132 friend class ParameterMap;
133 friend class ComponentInfo;
134 friend class Parameter;
136 void setValue(const Type &value);
137 ParameterType &operator=(const Type &value);
138
139private:
140 Type m_value;
141};
142
143//--------------------------------------------------------------------------
144// Template definitions - Parameter class
145//--------------------------------------------------------------------------
146
152template <class T> const T &Parameter::value() const {
153 auto const *p = dynamic_cast<ParameterType<T> const *>(this);
154 if (!p)
155 throw std::runtime_error("Wrong type of parameter.");
156 return p->ParameterType<T>::value();
157}
158
162template <class T> void Parameter::set(const T &t) {
163 ParameterType<T> *p = dynamic_cast<ParameterType<T> *>(this);
164 if (!p)
165 throw std::runtime_error("Wrong type of parameter.");
166 p->ParameterType<T>::setValue(t);
167}
168
169//--------------------------------------------------------------------------
170// Template definitions - ParameterType class
171//--------------------------------------------------------------------------
172
178template <class Type> void ParameterType<Type>::fromString(const std::string &value) {
179 if constexpr (std::is_same_v<Type, std::string>) {
180 m_value = value;
181 } else {
182 std::istringstream istr(value);
183 istr >> m_value;
184 }
185}
186
191template <class Type> void ParameterType<Type>::setValue(const Type &value) { m_value = value; }
192
197template <class Type> ParameterType<Type> &ParameterType<Type>::operator=(const Type &value) {
198 setValue(value);
199 return *this;
200}
201
203using Parameter_sptr = std::shared_ptr<Parameter>;
204
205// Instantiated and exported by Parameter.cpp. FitParameter is declared in FitParameter.h.
212
213} // namespace Geometry
214} // namespace Mantid
const std::string & m_value
Definition Algorithm.cpp:72
std::string name
Definition Run.cpp:60
const std::vector< double > & rhs
double value
The value of the point.
Definition FitMW.cpp:51
ComponentInfo : Provides a component centric view on to the instrument.
Base Instrument Class.
Definition Instrument.h:50
The ParameterFactory class creates parameters for the instrument ParameterMap.
ParameterInfo : the named parameters of an instrument, addressed by component index.
Parameter map iterator typedef.
Templated class for parameters of type Type.
Definition Parameter.h:114
void setValue(const Type &value)
Set the value of the parameter.
Definition Parameter.h:191
const Type & operator()() const
Get the value of the parameter.
Definition Parameter.h:127
void fromString(const std::string &value) override
Set the value of the property via a string.
Definition Parameter.h:178
const Type & value() const
Returns the value of the parameter.
Definition Parameter.h:125
std::string asString() const override
Returns the value of the property as a string.
Definition Parameter.cpp:32
Parameter * clone() const override
type-independent clone method;
Definition Parameter.h:129
ParameterType & operator=(const Type &value)
Set the value of the parameter via the assignment operator.
Definition Parameter.h:197
Base class for parameters of an instrument.
Definition Parameter.h:42
std::string m_name
The name of the property.
Definition Parameter.h:105
const std::string & type() const
Parameter type.
Definition Parameter.h:48
std::string m_description
whether the parameter should be visible in InstrumentViewer
Definition Parameter.h:110
const bool & visible() const
Parameter visibility in InstrumentViewer.
Definition Parameter.h:54
const T & value() const
Returns the parameter value of type T if the parameter has type ParameterType<T>
Definition Parameter.h:152
void set(const T &t)
Sets the value of type T to the parameter if it has type ParameterType<T> Throws an exception if the ...
Definition Parameter.h:162
const std::string & name() const
Parameter name.
Definition Parameter.h:50
std::string m_str_value
Parameter value as a string.
Definition Parameter.h:106
bool operator==(const Parameter &rhs) const
Equality operator.
Definition Parameter.h:77
std::string m_type
The type of the property.
Definition Parameter.h:103
virtual Parameter * clone() const =0
type-independent clone method;
virtual const std::string & getDescription() const
get description
Definition Parameter.h:71
virtual void fromString(const std::string &value)
Set the value of the property via a string.
Definition Parameter.h:63
const char * nameAsCString() const
Parameter name.
Definition Parameter.h:52
virtual void setVisible(const bool &visible)
set visibility:
Definition Parameter.h:75
virtual std::string asString() const
Returns the value of the property as a string.
Definition Parameter.h:60
virtual void setDescription(const std::string &source)
set description:
Definition Parameter.h:69
virtual ~Parameter()=default
Virtual destructor.
std::shared_ptr< Parameter > Parameter_sptr
Typedef for the shared pointer.
Definition Parameter.h:203
MANTID_GEOMETRY_EXTERN_TEMPLATE(ParameterType< FitParameter >)
Helper class which provides the Collimation Length for SANS instruments.