Mantid
Loading...
Searching...
No Matches
Property.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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 +
8
16
17#include <algorithm>
18#include <iterator>
19#include <unordered_map>
20
21namespace Mantid {
22namespace Kernel {
23
31Property::Property(std::string name, const std::type_info &type, const unsigned int &direction)
32 : m_name(std::move(name)), m_documentation(""), m_typeinfo(&type), m_direction(direction), m_units(""), m_group(""),
33 m_remember(true), m_autotrim(true), m_disableReplaceWSButton(false), m_isDynamicDefault(false) {
34 if (m_name.empty()) {
35 throw std::invalid_argument("An empty property name is not permitted");
36 }
37
38 // Make sure a random int hasn't been passed in for the direction
39 // Property & PropertyWithValue destructors will be called in this case
40 if (m_direction > 2)
41 throw std::out_of_range("direction should be a member of the Direction enum");
42}
43
46 : m_name(right.m_name), m_documentation(right.m_documentation), m_typeinfo(right.m_typeinfo),
47 m_direction(right.m_direction), m_units(right.m_units), m_group(right.m_group), m_remember(right.m_remember),
48 m_autotrim(right.m_autotrim), m_disableReplaceWSButton(right.m_disableReplaceWSButton),
49 m_isDynamicDefault(right.m_isDynamicDefault) {
50 if (m_name.empty())
51 throw std::invalid_argument("An empty property name is not permitted");
52
53 std::transform(right.m_settings.begin(), right.m_settings.end(), std::back_inserter(m_settings),
54 [](auto const &settings) { return std::unique_ptr<IPropertySettings const>(settings->clone()); });
55}
56
58Property::~Property() = default;
59
63const std::string &Property::name() const { return m_name; }
64
68void Property::setName(const std::string &name) {
69 if (name.empty()) {
70 throw std::invalid_argument("An empty property name is not permitted");
71 }
72 m_name = name;
73}
74
78const std::string &Property::documentation() const { return m_documentation; }
79
83const std::type_info *Property::type_info() const { return m_typeinfo; }
84
89const std::string Property::type() const { return Mantid::Kernel::getUnmangledTypeName(*m_typeinfo); }
90
95std::string Property::isValid() const {
96 // the no error condition
97 return "";
98}
99
105void Property::setSettings(std::unique_ptr<IPropertySettings const> settings) {
106 m_settings.emplace_back(std::move(settings));
107}
108
113std::vector<std::unique_ptr<IPropertySettings const>> const &Property::getSettings() const { return m_settings; }
114
119
124bool Property::remember() const { return m_remember; }
125
130void Property::setRemember(bool remember) { m_remember = remember; }
131
138std::string Property::valueAsPrettyStr(const size_t maxLength, const bool collapseLists) const {
139 UNUSED_ARG(collapseLists);
140 return Strings::shorten(value(), maxLength);
141}
142
149void Property::setDocumentation(const std::string &documentation) { m_documentation = documentation; }
150
155std::vector<std::string> Property::allowedValues() const { return std::vector<std::string>(); }
156
160
165 std::ostringstream os;
166 os << "__TMP" << this;
167 this->setValue(os.str());
168}
169
175 std::ostringstream os;
176 os << "__TMP" << this;
177 return (os.str() == this->value());
178}
179
180//-------------------------------------------------------------------------------------------------
186int Property::size() const { return 1; }
187
188//-------------------------------------------------------------------------------------------------
194const std::string &Property::units() const { return m_units; }
195
196//-------------------------------------------------------------------------------------------------
201void Property::setUnits(const std::string &unit) { m_units = unit; }
202
203} // namespace Kernel
204
205//-------------------------- Utility function for class name lookup
206//-----------------------------
207
208// MG 16/07/09: Some forward declarations.I need this so
209// that the typeid function in getUnmangledTypeName knows about them
210// This way I don't need to actually include the headers and I don't
211// introduce unwanted dependencies
212namespace API {
213class Workspace;
214class WorkspaceGroup;
215class MatrixWorkspace;
216class ITableWorkspace;
218class IMDWorkspace;
219class IEventWorkspace;
220class IPeaksWorkspace;
222class IFunction;
223class IAlgorithm;
224} // namespace API
225namespace DataObjects {
226class EventWorkspace;
227class PeaksWorkspace;
228class LeanElasticPeaksWorkspace;
229class GroupingWorkspace;
230class OffsetsWorkspace;
231class MaskWorkspace;
232class SpecialWorkspace2D;
233class Workspace2D;
234class TableWorkspace;
235class SplittersWorkspace;
236} // namespace DataObjects
237
238namespace Kernel {
239class PropertyManager;
240
246bool operator==(const Property &lhs, const Property &rhs) {
247 if (lhs.name() != rhs.name())
248 return false;
249 if (lhs.type() != rhs.type())
250 return false;
251
252 // check for TimeSeriesProperty specializations
253 auto lhs_tsp_float = dynamic_cast<const TimeSeriesProperty<float> *>(&lhs);
254 if (lhs_tsp_float)
255 return lhs_tsp_float->operator==(rhs);
256
257 auto lhs_tsp_double = dynamic_cast<const TimeSeriesProperty<double> *>(&lhs);
258 if (lhs_tsp_double)
259 return lhs_tsp_double->operator==(rhs);
260
261 auto lhs_tsp_string = dynamic_cast<const TimeSeriesProperty<std::string> *>(&lhs);
262 if (lhs_tsp_string)
263 return lhs_tsp_string->operator==(rhs);
264
265 auto lhs_tsp_bool = dynamic_cast<const TimeSeriesProperty<bool> *>(&lhs);
266 if (lhs_tsp_bool)
267 return lhs_tsp_bool->operator==(rhs);
268
269 // use fallthrough behavior
270 return (lhs.value() == rhs.value());
271}
272
278bool operator!=(const Property &lhs, const Property &rhs) { return (!(lhs == rhs)); }
279
287std::string getUnmangledTypeName(const std::type_info &type) {
288 using std::make_pair;
289 using std::string;
290 using namespace Mantid::API;
291 using namespace Mantid::DataObjects;
292 // Compile a lookup table. This is a static local variable that
293 // will get initialized when the function is first used
294 static std::unordered_map<string, string> typestrings;
295 if (typestrings.empty()) {
296 typestrings.emplace(typeid(char).name(), string("letter"));
297 typestrings.emplace(typeid(int).name(), string("number"));
298 typestrings.emplace(typeid(long long).name(), string("number"));
299 typestrings.emplace(typeid(int64_t).name(), string("number"));
300 typestrings.emplace(typeid(double).name(), string("number"));
301 typestrings.emplace(typeid(bool).name(), string("boolean"));
302 typestrings.emplace(typeid(string).name(), string("string"));
303 typestrings.emplace(typeid(std::vector<string>).name(), string("str list"));
304 typestrings.emplace(typeid(std::vector<int>).name(), string("int list"));
305 typestrings.emplace(typeid(std::vector<long>).name(), string("long list"));
306 typestrings.emplace(typeid(std::vector<int64_t>).name(), string("int list"));
307 typestrings.emplace(typeid(std::vector<size_t>).name(), string("unsigned int list"));
308 typestrings.emplace(typeid(std::vector<double>).name(), string("dbl list"));
309 typestrings.emplace(typeid(std::vector<std::vector<string>>).name(), string("list of str lists"));
310 typestrings.emplace(typeid(OptionalBool).name(), string("optional boolean"));
311
312 // Workspaces
313 typestrings.emplace(typeid(std::shared_ptr<Workspace>).name(), string("Workspace"));
314 typestrings.emplace(typeid(std::shared_ptr<MatrixWorkspace>).name(), string("MatrixWorkspace"));
315 typestrings.emplace(typeid(std::shared_ptr<ITableWorkspace>).name(), string("TableWorkspace"));
316 typestrings.emplace(typeid(std::shared_ptr<IMDWorkspace>).name(), string("IMDWorkspace"));
317 typestrings.emplace(typeid(std::shared_ptr<IMDEventWorkspace>).name(), string("MDEventWorkspace"));
318 typestrings.emplace(typeid(std::shared_ptr<IEventWorkspace>).name(), string("IEventWorkspace"));
319 typestrings.emplace(typeid(std::shared_ptr<Workspace2D>).name(), string("Workspace2D"));
320 typestrings.emplace(typeid(std::shared_ptr<EventWorkspace>).name(), string("EventWorkspace"));
321 typestrings.emplace(typeid(std::shared_ptr<PeaksWorkspace>).name(), string("PeaksWorkspace"));
322 typestrings.emplace(typeid(std::shared_ptr<LeanElasticPeaksWorkspace>).name(), string("LeanElasticPeaksWorkspace"));
323 typestrings.emplace(typeid(std::shared_ptr<IPeaksWorkspace>).name(), string("IPeaksWorkspace"));
324 typestrings.emplace(typeid(std::shared_ptr<GroupingWorkspace>).name(), string("GroupingWorkspace"));
325 typestrings.emplace(typeid(std::shared_ptr<WorkspaceGroup>).name(), string("WorkspaceGroup"));
326 typestrings.emplace(typeid(std::shared_ptr<OffsetsWorkspace>).name(), string("OffsetsWorkspace"));
327 typestrings.emplace(typeid(std::shared_ptr<MaskWorkspace>).name(), string("MaskWorkspace"));
328 typestrings.emplace(typeid(std::shared_ptr<SpecialWorkspace2D>).name(), string("SpecialWorkspace2D"));
329 typestrings.emplace(typeid(std::shared_ptr<IMDHistoWorkspace>).name(), string("IMDHistoWorkspace"));
330 typestrings.emplace(typeid(std::shared_ptr<SplittersWorkspace>).name(), string("SplittersWorkspace"));
331 typestrings.emplace(typeid(std::shared_ptr<SpecialWorkspace2D>).name(), string("SpecialWorkspace2D"));
332 typestrings.emplace(typeid(std::shared_ptr<TableWorkspace>).name(), string("TableWorkspace"));
333 // FunctionProperty
334 typestrings.emplace(typeid(std::shared_ptr<IFunction>).name(), string("Function"));
335 typestrings.emplace(typeid(std::shared_ptr<IAlgorithm>).name(), string("IAlgorithm"));
336 typestrings.emplace(typeid(std::shared_ptr<PropertyManager>).name(), string("Dictionary"));
337 }
338 auto mitr = typestrings.find(type.name());
339 if (mitr != typestrings.end()) {
340 return mitr->second;
341 }
342 /* if a type name looks like
343 N6Mantid6Kernel16EnumeratedStringINS_12_GLOBAL__N_111BinningModeEXadL_ZNS2_L16binningModeNamesEEEXadL_ZNS0_12_GLOBAL__N_114compareStringsEEEEE
344 we assume it is a EnumeratedStringProperty and return a "string" for it */
345 string type_name = type.name();
346 if (type_name.find("Mantid") != std::string::npos && type_name.find("EnumeratedString") != std::string::npos) {
347 return "string";
348 }
349 return type.name();
350}
351
357bool Property::autoTrim() const { return m_autotrim; }
358
364void Property::setAutoTrim(const bool &setting) { m_autotrim = setting; }
365
371
376void Property::setDisableReplaceWSButton(const bool &disable) { m_disableReplaceWSButton = disable; }
377
383
387void Property::setIsDynamicDefault(const bool &flag) { m_isDynamicDefault = flag; }
388
389} // namespace Kernel
390
391} // namespace Mantid
std::string name
Definition Run.cpp:60
const std::vector< double > & rhs
double right
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition System.h:48
IAlgorithm is the interface implemented by the Algorithm base class.
Definition IAlgorithm.h:45
This class provides an interface to an EventWorkspace.
This is an interface to a fitting function - a semi-abstarct class.
Definition IFunction.h:166
Abstract base class for multi-dimension event workspaces (MDEventWorkspace).
Abstract interface to MDHistoWorkspace, for use in exposing to Python.
Basic MD Workspace Abstract Class.
Interface to the class Mantid::DataObjects::PeaksWorkspace.
ITableWorkspace is an implementation of Workspace in which the data are organised in columns of same ...
Base MatrixWorkspace Abstract Class.
Class to hold a set of workspaces.
Base Workspace Abstract Class.
Definition Workspace.h:29
OptionalBool : Tri-state bool.
This class stores information about the parameters used by an algorithm.
Base class for properties.
Definition Property.h:94
bool isDynamicDefault() const
Returns a flag indicating that the property's value has been set programmatically,...
Definition Property.cpp:382
virtual const std::string & units() const
Returns the units of the property, if any, as a string.
Definition Property.cpp:194
void setDocumentation(const std::string &documentation)
Sets the user level description of the property.
Definition Property.cpp:149
void setIsDynamicDefault(const bool &flag)
Set or clear the flag indicating whether or not the property's value has been set programmatically.
Definition Property.cpp:387
virtual std::string valueAsPrettyStr(const size_t maxLength=0, const bool collapseLists=true) const
Returns the value of the property as a pretty printed string.
Definition Property.cpp:138
bool hasTemporaryValue() const
Property is using a temporary value for this property.
Definition Property.cpp:174
const std::type_info * m_typeinfo
The type of the property.
Definition Property.h:224
std::vector< std::unique_ptr< IPropertySettings const > > m_settings
Property settings (enabled/visible)
Definition Property.h:231
void setSettings(std::unique_ptr< IPropertySettings const > settings)
Set the PropertySettings object.
Definition Property.cpp:105
virtual int size() const
Return the size of this property.
Definition Property.cpp:186
Property()
Private default constructor.
void setDisableReplaceWSButton(const bool &disable)
Sets the property to disable the creation of the "Replace Workspace" button.
Definition Property.cpp:376
virtual const PropertyHistory createHistory() const
Create a PropertyHistory object representing the current state of the Property.
Definition Property.cpp:159
bool autoTrim() const
Returns if the property is set to automatically trim string unput values of whitespace.
Definition Property.cpp:357
void setRemember(bool)
Set wheter to remeber this property input.
Definition Property.cpp:130
std::vector< std::unique_ptr< IPropertySettings const > > const & getSettings() const
Definition Property.cpp:113
void createTemporaryValue()
Create a temporary value for this property.
Definition Property.cpp:164
std::string m_name
The name of the property.
Definition Property.h:215
const std::string & documentation() const
Get the property's documentation string.
Definition Property.cpp:78
bool remember() const
Whether to save input values.
Definition Property.cpp:124
virtual std::string setValue(const std::string &)=0
Set the value of the property via a string.
const unsigned int m_direction
Whether the property is used as input, output or both to an algorithm.
Definition Property.h:226
virtual void setUnits(const std::string &unit)
Sets the units of the property, as a string.
Definition Property.cpp:201
void setName(const std::string &name)
Set the property's name.
Definition Property.cpp:68
virtual std::string isValid() const
Overridden function that checks whether the property, if not overriden returns "".
Definition Property.cpp:95
bool m_remember
Flag whether to save input values.
Definition Property.h:240
void clearSettings()
Clears the vector of PropertySettings for this property.
Definition Property.cpp:118
const std::string & name() const
Get the property's name.
Definition Property.cpp:63
void setAutoTrim(const bool &setting)
Sets if the property is set to automatically trim string unput values of whitespace.
Definition Property.cpp:364
bool m_disableReplaceWSButton
Flag to disable the generation of the "Replace Workspace" button on the OutputWorkspace property.
Definition Property.h:247
virtual std::vector< std::string > allowedValues() const
Returns the set of valid values for this property, if such a set exists.
Definition Property.cpp:155
virtual ~Property()
Virtual destructor.
const std::string type() const
Returns the type of the property as a string.
Definition Property.cpp:89
std::string m_documentation
Longer, optional description of property.
Definition Property.h:222
const std::type_info * type_info() const
Get the property type_info.
Definition Property.cpp:83
bool m_autotrim
Flag to determine if string inputs to the property should be automatically trimmed of whitespace.
Definition Property.h:244
bool disableReplaceWSButton() const
Returns if the property is set to disable the creation of the "Replace Workspace" button.
Definition Property.cpp:370
std::string m_units
Units of the property (optional)
Definition Property.h:228
bool m_isDynamicDefault
Flag to indicate that the property's value has been set programmatically, for example,...
Definition Property.h:251
virtual std::string value() const =0
Returns the value of the property as a string.
A specialised Property class for holding a series of time-value pairs.
MANTID_KERNEL_DLL std::string shorten(const std::string &input, const size_t max_length)
Converts long strings into "start ... end".
Definition Strings.cpp:52
MANTID_KERNEL_DLL bool operator!=(const Mantid::Kernel::Property &lhs, const Mantid::Kernel::Property &rhs)
Compares this to another property for inequality.
Definition Property.cpp:278
MANTID_KERNEL_DLL bool operator==(const Mantid::Kernel::Property &lhs, const Mantid::Kernel::Property &rhs)
Compares this to another property for equality.
Definition Property.cpp:246
MANTID_KERNEL_DLL std::string getUnmangledTypeName(const std::type_info &type)
Return the name corresponding to the mangled string given by typeid.
Definition Property.cpp:287
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.