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;
29class ParameterInfo;
30
38class MANTID_GEOMETRY_DLL Parameter {
39public:
41 virtual ~Parameter() = default;
42
44 const std::string &type() const { return m_type; }
46 const std::string &name() const { return m_name; }
48 const char *nameAsCString() const { return m_name.c_str(); }
50 const bool &visible() const { return m_visible; }
51
53 virtual Parameter *clone() const = 0;
54
56 virtual std::string asString() const { return m_str_value; }
57
59 virtual void fromString(const std::string &value) { m_str_value = value; }
60
63 template <class T> const T &value();
65 virtual void setDescription(const std::string &source) { m_description.assign(source); }
67 virtual const std::string &getDescription() const { return m_description; }
69 virtual std::string getShortDescription() const;
71 virtual void setVisible(const bool &visible) { m_visible = visible; }
73 bool operator==(const Parameter &rhs) const {
74 if (this->name() == rhs.name() && this->type() == rhs.type() && this->asString() == rhs.asString())
75 return true;
76 else
77 return false;
78 }
79
80protected:
81 friend class ParameterMap;
83 friend class ParameterInfo;
84
89 template <class T> void set(const T &t);
90
91 friend class ParameterFactory;
93 Parameter() : m_type(""), m_name(""), m_str_value(""), m_visible(true), m_description("") {}
94
95private:
97 std::string m_type;
99 std::string m_name;
100 std::string m_str_value;
104 std::string m_description;
105};
106
108template <class Type> class DLLExport ParameterType : public Parameter {
109public:
112
114 std::string asString() const override;
116 void fromString(const std::string &value) override;
117
119 inline const Type &value() const { return m_value; }
121 inline const Type &operator()() const { return m_value; }
122
123 Parameter *clone() const override { return new ParameterType(*this); }
124
125private:
126 friend class ParameterMap;
127 friend class ComponentInfo;
128 friend class Parameter;
130 void setValue(const Type &value);
131 ParameterType &operator=(const Type &value);
132
133private:
134 Type m_value;
135};
136
137//--------------------------------------------------------------------------
138// Template definitions - Parameter class
139//--------------------------------------------------------------------------
140
146template <class T> const T &Parameter::value() {
147 ParameterType<T> *p = dynamic_cast<ParameterType<T> *>(this);
148 if (!p)
149 throw std::runtime_error("Wrong type of parameter.");
150 return p->ParameterType<T>::value();
151}
152
156template <class T> void Parameter::set(const T &t) {
157 ParameterType<T> *p = dynamic_cast<ParameterType<T> *>(this);
158 if (!p)
159 throw std::runtime_error("Wrong type of parameter.");
160 p->ParameterType<T>::setValue(t);
161}
162
163//--------------------------------------------------------------------------
164// Template definitions - ParameterType class
165//--------------------------------------------------------------------------
166
172template <class Type> void ParameterType<Type>::fromString(const std::string &value) {
173 std::istringstream istr(value);
174 istr >> m_value;
175}
176
180template <> inline void ParameterType<std::string>::fromString(const std::string &value) { m_value = value; }
181
186template <class Type> void ParameterType<Type>::setValue(const Type &value) { m_value = value; }
187
192template <class Type> ParameterType<Type> &ParameterType<Type>::operator=(const Type &value) {
193 setValue(value);
194 return *this;
195}
196
198using Parameter_sptr = std::shared_ptr<Parameter>;
199
200} // namespace Geometry
201} // 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
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition System.h:33
ComponentInfo : Provides a component centric view on to the instrument.
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:108
void setValue(const Type &value)
Set the value of the parameter.
Definition Parameter.h:186
const Type & operator()() const
Get the value of the parameter.
Definition Parameter.h:121
void fromString(const std::string &value) override
Set the value of the property via a string.
Definition Parameter.h:172
const Type & value() const
Returns the value of the parameter.
Definition Parameter.h:119
Parameter * clone() const override
type-independent clone method;
Definition Parameter.h:123
ParameterType & operator=(const Type &value)
Set the value of the parameter via the assignment operator.
Definition Parameter.h:192
Base class for parameters of an instrument.
Definition Parameter.h:38
std::string m_name
The name of the property.
Definition Parameter.h:99
const std::string & type() const
Parameter type.
Definition Parameter.h:44
std::string m_description
whether the parameter should be visible in InstrumentViewer
Definition Parameter.h:104
const bool & visible() const
Parameter visibility in InstrumentViewer.
Definition Parameter.h:50
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:156
const std::string & name() const
Parameter name.
Definition Parameter.h:46
std::string m_str_value
Parameter value as a string.
Definition Parameter.h:100
bool operator==(const Parameter &rhs) const
Equality operator.
Definition Parameter.h:73
std::string m_type
The type of the property.
Definition Parameter.h:97
virtual Parameter * clone() const =0
type-independent clone method;
virtual const std::string & getDescription() const
get description
Definition Parameter.h:67
virtual void fromString(const std::string &value)
Set the value of the property via a string.
Definition Parameter.h:59
const char * nameAsCString() const
Parameter name.
Definition Parameter.h:48
const T & value()
Returns the parameter value of type T if the parameter has type ParameterType<T>
Definition Parameter.h:146
virtual void setVisible(const bool &visible)
set visibility:
Definition Parameter.h:71
virtual std::string asString() const
Returns the value of the property as a string.
Definition Parameter.h:56
virtual void setDescription(const std::string &source)
set description:
Definition Parameter.h:65
virtual ~Parameter()=default
Virtual destructor.
std::shared_ptr< Parameter > Parameter_sptr
Typedef for the shared pointer.
Definition Parameter.h:198
Helper class which provides the Collimation Length for SANS instruments.