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 <iomanip>
11#include <memory>
12#include <sstream>
13#include <stdexcept>
14#include <string>
15#include <typeinfo>
16#include <vector>
17
18namespace Mantid {
19
20namespace Kernel {
21template <class C, class Base, typename... Args> class Instantiator;
22}
23
24namespace Geometry {
25//--------------------------------------------------------------------------
26// Forward declarations
27//--------------------------------------------------------------------------
28class ParameterMap;
29
37class MANTID_GEOMETRY_DLL Parameter {
38public:
40 virtual ~Parameter() = default;
41
43 const std::string &type() const { return m_type; }
45 const std::string &name() const { return m_name; }
47 const char *nameAsCString() const { return m_name.c_str(); }
49 const bool &visible() const { return m_visible; }
50
52 virtual Parameter *clone() const = 0;
53
55 virtual std::string asString() const { return m_str_value; }
56
58 virtual void fromString(const std::string &value) { m_str_value = value; }
59
62 template <class T> const T &value();
64 virtual void setDescription(const std::string &source) { m_description.assign(source); }
66 virtual const std::string &getDescription() const { return m_description; }
68 virtual std::string getShortDescription() const;
70 virtual void setVisible(const bool &visible) { m_visible = visible; }
72 bool operator==(const Parameter &rhs) const {
73 if (this->name() == rhs.name() && this->type() == rhs.type() && this->asString() == rhs.asString())
74 return true;
75 else
76 return false;
77 }
78
79protected:
80 friend class ParameterMap;
81
86 template <class T> void set(const T &t);
87
88 friend class ParameterFactory;
90 Parameter() : m_type(""), m_name(""), m_str_value(""), m_visible(true), m_description("") {}
91
92private:
94 std::string m_type;
96 std::string m_name;
97 std::string m_str_value;
98 bool m_visible;
101 std::string m_description;
102};
103
105template <class Type> class DLLExport ParameterType : public Parameter {
106public:
109
111 std::string asString() const override;
113 void fromString(const std::string &value) override;
114
116 inline const Type &value() const { return m_value; }
118 inline const Type &operator()() const { return m_value; }
119
120 Parameter *clone() const override { return new ParameterType(*this); }
121
122private:
123 friend class ParameterMap;
124 friend class Parameter;
126 void setValue(const Type &value);
127 ParameterType &operator=(const Type &value);
128
129private:
130 Type m_value;
131};
132
133//--------------------------------------------------------------------------
134// Template definitions - Parameter class
135//--------------------------------------------------------------------------
136
142template <class T> const T &Parameter::value() {
143 ParameterType<T> *p = dynamic_cast<ParameterType<T> *>(this);
144 if (!p)
145 throw std::runtime_error("Wrong type of parameter.");
146 return p->ParameterType<T>::value();
147}
148
153template <class T> void Parameter::set(const T &t) {
154 ParameterType<T> *p = dynamic_cast<ParameterType<T> *>(this);
155 if (!p)
156 throw std::runtime_error("Wrong type of parameter.");
157 p->ParameterType<T>::setValue(t);
158}
159
160//--------------------------------------------------------------------------
161// Template definitions - ParameterType class
162//--------------------------------------------------------------------------
163
169template <class Type> void ParameterType<Type>::fromString(const std::string &value) {
170 std::istringstream istr(value);
171 istr >> m_value;
172}
173
177template <> inline void ParameterType<std::string>::fromString(const std::string &value) { m_value = value; }
178
183template <class Type> void ParameterType<Type>::setValue(const Type &value) { m_value = value; }
184
189template <class Type> ParameterType<Type> &ParameterType<Type>::operator=(const Type &value) {
190 setValue(value);
191 return *this;
192}
193
195using Parameter_sptr = std::shared_ptr<Parameter>;
196
197} // namespace Geometry
198} // namespace Mantid
const std::string & m_value
Definition: Algorithm.cpp:71
const std::vector< double > & rhs
double value
The value of the point.
Definition: FitMW.cpp:51
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition: System.h:53
The ParameterFactory class creates parameters for the instrument ParameterMap.
Templated class for parameters of type Type.
Definition: Parameter.h:105
void setValue(const Type &value)
Set the value of the parameter.
Definition: Parameter.h:183
const Type & operator()() const
Get the value of the parameter.
Definition: Parameter.h:118
void fromString(const std::string &value) override
Set the value of the property via a string.
Definition: Parameter.h:169
const Type & value() const
Returns the value of the parameter.
Definition: Parameter.h:116
Parameter * clone() const override
type-independent clone method;
Definition: Parameter.h:120
ParameterType & operator=(const Type &value)
Set the value of the parameter via the assignment operator.
Definition: Parameter.h:189
Base class for parameters of an instrument.
Definition: Parameter.h:37
std::string m_name
The name of the property.
Definition: Parameter.h:96
const std::string & type() const
Parameter type.
Definition: Parameter.h:43
std::string m_description
whether the parameter should be visible in InstrumentViewer
Definition: Parameter.h:101
const bool & visible() const
Parameter visibility in InstrumentViewer.
Definition: Parameter.h:49
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:153
const std::string & name() const
Parameter name.
Definition: Parameter.h:45
std::string m_str_value
Parameter value as a string.
Definition: Parameter.h:97
Parameter()
Constructor.
Definition: Parameter.h:90
bool operator==(const Parameter &rhs) const
Equality operator.
Definition: Parameter.h:72
std::string m_type
The type of the property.
Definition: Parameter.h:94
virtual Parameter * clone() const =0
type-independent clone method;
virtual const std::string & getDescription() const
get description
Definition: Parameter.h:66
virtual void fromString(const std::string &value)
Set the value of the property via a string.
Definition: Parameter.h:58
const char * nameAsCString() const
Parameter name.
Definition: Parameter.h:47
const T & value()
Returns the parameter value of type T if the parameter has type ParameterType<T>
Definition: Parameter.h:142
virtual void setVisible(const bool &visible)
set visibility:
Definition: Parameter.h:70
virtual std::string asString() const
Returns the value of the property as a string.
Definition: Parameter.h:55
virtual void setDescription(const std::string &source)
set description:
Definition: Parameter.h:64
virtual ~Parameter()=default
Virtual destructor.
std::shared_ptr< Parameter > Parameter_sptr
Typedef for the shared pointer.
Definition: Parameter.h:195
Helper class which provides the Collimation Length for SANS instruments.