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 <unordered_map>
18
19namespace Mantid {
20namespace Kernel {
21
29Property::Property(std::string name, const std::type_info &type, const unsigned int &direction)
30 : m_name(std::move(name)), m_documentation(""), m_typeinfo(&type), m_direction(direction), m_units(""), m_group(""),
31 m_remember(true), m_autotrim(true) {
32 if (m_name.empty()) {
33 throw std::invalid_argument("An empty property name is not permitted");
34 }
35
36 // Make sure a random int hasn't been passed in for the direction
37 // Property & PropertyWithValue destructors will be called in this case
38 if (m_direction > 2)
39 throw std::out_of_range("direction should be a member of the Direction enum");
40}
41
44 : m_name(right.m_name), m_documentation(right.m_documentation), m_typeinfo(right.m_typeinfo),
45 m_direction(right.m_direction), m_units(right.m_units), m_group(right.m_group), m_remember(right.m_remember),
46 m_autotrim(right.m_autotrim) {
47 if (m_name.empty()) {
48 throw std::invalid_argument("An empty property name is not permitted");
49 }
50 if (right.m_settings)
51 m_settings.reset(right.m_settings->clone());
52}
53
55Property::~Property() = default;
56
60const std::string &Property::name() const { return m_name; }
61
65const std::string &Property::documentation() const { return m_documentation; }
66
70const std::type_info *Property::type_info() const { return m_typeinfo; }
71
76const std::string Property::type() const { return Mantid::Kernel::getUnmangledTypeName(*m_typeinfo); }
77
82std::string Property::isValid() const {
83 // the no error condition
84 return "";
85}
86
92void Property::setSettings(std::unique_ptr<IPropertySettings> settings) { m_settings = std::move(settings); }
93
99
103void Property::clearSettings() { m_settings.reset(nullptr); }
104
109bool Property::remember() const { return m_remember; }
110
115void Property::setRemember(bool remember) { m_remember = remember; }
116
123std::string Property::valueAsPrettyStr(const size_t maxLength, const bool collapseLists) const {
124 UNUSED_ARG(collapseLists);
125 return Strings::shorten(value(), maxLength);
126}
127
134void Property::setDocumentation(const std::string &documentation) { m_documentation = documentation; }
135
140std::vector<std::string> Property::allowedValues() const { return std::vector<std::string>(); }
141
145
150 std::ostringstream os;
151 os << "__TMP" << this;
152 this->setValue(os.str());
153}
154
160 std::ostringstream os;
161 os << "__TMP" << this;
162 return (os.str() == this->value());
163}
164
165//-------------------------------------------------------------------------------------------------
171int Property::size() const { return 1; }
172
173//-------------------------------------------------------------------------------------------------
179const std::string &Property::units() const { return m_units; }
180
181//-------------------------------------------------------------------------------------------------
186void Property::setUnits(const std::string &unit) { m_units = unit; }
187
188//-------------------------------------------------------------------------------------------------
194void Property::filterByTime(const Types::Core::DateAndTime &start, const Types::Core::DateAndTime &stop) {
195 UNUSED_ARG(start);
196 UNUSED_ARG(stop);
197 // Do nothing in general
198}
199
200//-----------------------------------------------------------------------------------------------
208void Property::splitByTime(std::vector<SplittingInterval> &splitter, std::vector<Property *> outputs,
209 bool isProtonCharge) const {
210 UNUSED_ARG(splitter);
211 UNUSED_ARG(outputs);
212 UNUSED_ARG(isProtonCharge);
213}
214
215} // namespace Kernel
216
217//-------------------------- Utility function for class name lookup
218//-----------------------------
219
220// MG 16/07/09: Some forward declarations.I need this so
221// that the typeid function in getUnmangledTypeName knows about them
222// This way I don't need to actually include the headers and I don't
223// introduce unwanted dependencies
224namespace API {
225class Workspace;
226class WorkspaceGroup;
227class MatrixWorkspace;
228class ITableWorkspace;
230class IMDWorkspace;
231class IEventWorkspace;
232class IPeaksWorkspace;
234class IFunction;
235class IAlgorithm;
236} // namespace API
237namespace DataObjects {
238class EventWorkspace;
239class PeaksWorkspace;
240class LeanElasticPeaksWorkspace;
241class GroupingWorkspace;
242class OffsetsWorkspace;
243class MaskWorkspace;
244class SpecialWorkspace2D;
245class Workspace2D;
246class TableWorkspace;
247class SpecialWorkspace2D;
248class SplittersWorkspace;
249} // namespace DataObjects
250
251namespace Kernel {
252class PropertyManager;
253
259bool operator==(const Property &lhs, const Property &rhs) {
260 if (lhs.name() != rhs.name())
261 return false;
262 if (lhs.type() != rhs.type())
263 return false;
264
265 // check for TimeSeriesProperty specializations
266 auto lhs_tsp_float = dynamic_cast<const TimeSeriesProperty<float> *>(&lhs);
267 if (lhs_tsp_float)
268 return lhs_tsp_float->operator==(rhs);
269
270 auto lhs_tsp_double = dynamic_cast<const TimeSeriesProperty<double> *>(&lhs);
271 if (lhs_tsp_double)
272 return lhs_tsp_double->operator==(rhs);
273
274 auto lhs_tsp_string = dynamic_cast<const TimeSeriesProperty<std::string> *>(&lhs);
275 if (lhs_tsp_string)
276 return lhs_tsp_string->operator==(rhs);
277
278 auto lhs_tsp_bool = dynamic_cast<const TimeSeriesProperty<bool> *>(&lhs);
279 if (lhs_tsp_bool)
280 return lhs_tsp_bool->operator==(rhs);
281
282 // use fallthrough behavior
283 return (lhs.value() == rhs.value());
284}
285
291bool operator!=(const Property &lhs, const Property &rhs) { return (!(lhs == rhs)); }
292
300std::string getUnmangledTypeName(const std::type_info &type) {
301 using std::make_pair;
302 using std::string;
303 using namespace Mantid::API;
304 using namespace Mantid::DataObjects;
305 // Compile a lookup table. This is a static local variable that
306 // will get initialized when the function is first used
307 static std::unordered_map<string, string> typestrings;
308 if (typestrings.empty()) {
309 typestrings.emplace(typeid(char).name(), string("letter"));
310 typestrings.emplace(typeid(int).name(), string("number"));
311 typestrings.emplace(typeid(long long).name(), string("number"));
312 typestrings.emplace(typeid(int64_t).name(), string("number"));
313 typestrings.emplace(typeid(double).name(), string("number"));
314 typestrings.emplace(typeid(bool).name(), string("boolean"));
315 typestrings.emplace(typeid(string).name(), string("string"));
316 typestrings.emplace(typeid(std::vector<string>).name(), string("str list"));
317 typestrings.emplace(typeid(std::vector<int>).name(), string("int list"));
318 typestrings.emplace(typeid(std::vector<long>).name(), string("long list"));
319 typestrings.emplace(typeid(std::vector<int64_t>).name(), string("int list"));
320 typestrings.emplace(typeid(std::vector<size_t>).name(), string("unsigned int list"));
321 typestrings.emplace(typeid(std::vector<double>).name(), string("dbl list"));
322 typestrings.emplace(typeid(std::vector<std::vector<string>>).name(), string("list of str lists"));
323 typestrings.emplace(typeid(OptionalBool).name(), string("optional boolean"));
324
325 // Workspaces
326 typestrings.emplace(typeid(std::shared_ptr<Workspace>).name(), string("Workspace"));
327 typestrings.emplace(typeid(std::shared_ptr<MatrixWorkspace>).name(), string("MatrixWorkspace"));
328 typestrings.emplace(typeid(std::shared_ptr<ITableWorkspace>).name(), string("TableWorkspace"));
329 typestrings.emplace(typeid(std::shared_ptr<IMDWorkspace>).name(), string("IMDWorkspace"));
330 typestrings.emplace(typeid(std::shared_ptr<IMDEventWorkspace>).name(), string("MDEventWorkspace"));
331 typestrings.emplace(typeid(std::shared_ptr<IEventWorkspace>).name(), string("IEventWorkspace"));
332 typestrings.emplace(typeid(std::shared_ptr<Workspace2D>).name(), string("Workspace2D"));
333 typestrings.emplace(typeid(std::shared_ptr<EventWorkspace>).name(), string("EventWorkspace"));
334 typestrings.emplace(typeid(std::shared_ptr<PeaksWorkspace>).name(), string("PeaksWorkspace"));
335 typestrings.emplace(typeid(std::shared_ptr<LeanElasticPeaksWorkspace>).name(), string("LeanElasticPeaksWorkspace"));
336 typestrings.emplace(typeid(std::shared_ptr<IPeaksWorkspace>).name(), string("IPeaksWorkspace"));
337 typestrings.emplace(typeid(std::shared_ptr<GroupingWorkspace>).name(), string("GroupingWorkspace"));
338 typestrings.emplace(typeid(std::shared_ptr<WorkspaceGroup>).name(), string("WorkspaceGroup"));
339 typestrings.emplace(typeid(std::shared_ptr<OffsetsWorkspace>).name(), string("OffsetsWorkspace"));
340 typestrings.emplace(typeid(std::shared_ptr<MaskWorkspace>).name(), string("MaskWorkspace"));
341 typestrings.emplace(typeid(std::shared_ptr<SpecialWorkspace2D>).name(), string("SpecialWorkspace2D"));
342 typestrings.emplace(typeid(std::shared_ptr<IMDHistoWorkspace>).name(), string("IMDHistoWorkspace"));
343 typestrings.emplace(typeid(std::shared_ptr<SplittersWorkspace>).name(), string("SplittersWorkspace"));
344 typestrings.emplace(typeid(std::shared_ptr<SpecialWorkspace2D>).name(), string("SpecialWorkspace2D"));
345 typestrings.emplace(typeid(std::shared_ptr<TableWorkspace>).name(), string("TableWorkspace"));
346 // FunctionProperty
347 typestrings.emplace(typeid(std::shared_ptr<IFunction>).name(), string("Function"));
348 typestrings.emplace(typeid(std::shared_ptr<IAlgorithm>).name(), string("IAlgorithm"));
349 typestrings.emplace(typeid(std::shared_ptr<PropertyManager>).name(), string("Dictionary"));
350 }
351 auto mitr = typestrings.find(type.name());
352 if (mitr != typestrings.end()) {
353 return mitr->second;
354 }
355
356 return type.name();
357}
358
364bool Property::autoTrim() const { return m_autotrim; }
365
371void Property::setAutoTrim(const bool &setting) { m_autotrim = setting; }
372} // namespace Kernel
373
374} // namespace Mantid
const std::vector< double > & rhs
double right
Definition: LineProfile.cpp:81
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition: System.h:64
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:163
Abstract base class for multi-dimension event workspaces (MDEventWorkspace).
Abstract interface to MDHistoWorkspace, for use in exposing to Python.
Basic MD Workspace Abstract Class.
Definition: IMDWorkspace.h:40
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:30
Interface for modifiers to Property's that specify if they should be enabled or visible in a GUI.
OptionalBool : Tri-state bool.
Definition: OptionalBool.h:25
This class stores information about the parameters used by an algorithm.
Base class for properties.
Definition: Property.h:94
virtual const std::string & units() const
Returns the units of the property, if any, as a string.
Definition: Property.cpp:179
void setSettings(std::unique_ptr< IPropertySettings > settings)
Set the PropertySettings object.
Definition: Property.cpp:92
void setDocumentation(const std::string &documentation)
Sets the user level description of the property.
Definition: Property.cpp:134
IPropertySettings * getSettings()
Definition: Property.cpp:98
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:123
bool hasTemporaryValue() const
Property is using a temporary value for this property.
Definition: Property.cpp:159
const std::type_info * m_typeinfo
The type of the property.
Definition: Property.h:217
virtual void filterByTime(const Types::Core::DateAndTime &start, const Types::Core::DateAndTime &stop)
Filter out a property by time.
Definition: Property.cpp:194
virtual int size() const
Return the size of this property.
Definition: Property.cpp:171
Property()
Private default constructor.
virtual const PropertyHistory createHistory() const
Create a PropertyHistory object representing the current state of the Property.
Definition: Property.cpp:144
bool autoTrim() const
Returns if the property is set to automatically trim string unput values of whitespace.
Definition: Property.cpp:364
void setRemember(bool)
Set wheter to remeber this property input.
Definition: Property.cpp:115
void createTemporaryValue()
Create a temporary value for this property.
Definition: Property.cpp:149
std::string m_name
The name of the property.
Definition: Property.h:208
const std::string & documentation() const
Get the property's documentation string.
Definition: Property.cpp:65
bool remember() const
Whether to save input values.
Definition: Property.cpp:109
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:219
virtual void setUnits(const std::string &unit)
Sets the units of the property, as a string.
Definition: Property.cpp:186
virtual std::string isValid() const
Overridden function that checks whether the property, if not overriden returns "".
Definition: Property.cpp:82
bool m_remember
Flag whether to save input values.
Definition: Property.h:233
void clearSettings()
Deletes the PropertySettings object contained.
Definition: Property.cpp:103
const std::string & name() const
Get the property's name.
Definition: Property.cpp:60
void setAutoTrim(const bool &setting)
Sets if the property is set to automatically trim string unput values of whitespace.
Definition: Property.cpp:371
std::unique_ptr< IPropertySettings > m_settings
Property settings (enabled/visible)
Definition: Property.h:224
virtual std::vector< std::string > allowedValues() const
Returns the set of valid values for this property, if such a set exists.
Definition: Property.cpp:140
virtual ~Property()
Virtual destructor.
const std::string type() const
Returns the type of the property as a string.
Definition: Property.cpp:76
std::string m_documentation
Longer, optional description of property.
Definition: Property.h:215
virtual void splitByTime(std::vector< SplittingInterval > &splitter, std::vector< Property * > outputs, bool isProtonCharge=true) const
Split a property by time.
Definition: Property.cpp:208
const std::type_info * type_info() const
Get the property type_info.
Definition: Property.cpp:70
bool m_autotrim
Flag to determine if string inputs to the property should be automatically trimmed of whitespace.
Definition: Property.h:237
std::string m_units
Units of the property (optional)
Definition: Property.h:221
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:54
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:291
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:259
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:300
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.