forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Property.cpp
391 lines (343 loc) · 14.7 KB
/
Property.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidKernel/Property.h"
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/IPropertySettings.h"
#include "MantidKernel/OptionalBool.h"
#include "MantidKernel/PropertyHistory.h"
#include "MantidKernel/Strings.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include <unordered_map>
namespace Mantid {
namespace Kernel {
/** Constructor
* @param name :: The name of the property
* @param type :: The type of the property
* @param direction :: Whether this is a Direction::Input, Direction::Output or
* Direction::InOut (Input & Output) property
* @throws std::invalid_argument if the name is empty
*/
Property::Property(std::string name, const std::type_info &type, const unsigned int &direction)
: m_name(std::move(name)), m_documentation(""), m_typeinfo(&type), m_direction(direction), m_units(""), m_group(""),
m_remember(true), m_autotrim(true) {
if (m_name.empty()) {
throw std::invalid_argument("An empty property name is not permitted");
}
// Make sure a random int hasn't been passed in for the direction
// Property & PropertyWithValue destructors will be called in this case
if (m_direction > 2)
throw std::out_of_range("direction should be a member of the Direction enum");
}
/// Copy constructor
Property::Property(const Property &right)
: m_name(right.m_name), m_documentation(right.m_documentation), m_typeinfo(right.m_typeinfo),
m_direction(right.m_direction), m_units(right.m_units), m_group(right.m_group), m_remember(right.m_remember),
m_autotrim(right.m_autotrim) {
if (m_name.empty()) {
throw std::invalid_argument("An empty property name is not permitted");
}
if (right.m_settings)
m_settings.reset(right.m_settings->clone());
}
/// Virtual destructor
Property::~Property() = default;
/** Get the property's name
* @return The name of the property
*/
const std::string &Property::name() const { return m_name; }
/** Get the property's documentation string
* @return The documentation string
*/
const std::string &Property::documentation() const { return m_documentation; }
/** Get the property's short documentation string
* @return The documentation string
*/
const std::string &Property::briefDocumentation() const { return m_shortDoc; }
/** Get the property type_info
* @return The type of the property
*/
const std::type_info *Property::type_info() const { return m_typeinfo; }
/** Returns the type of the property as a string.
* Note that this is implementation dependent.
* @return The property type
*/
const std::string Property::type() const { return Mantid::Kernel::getUnmangledTypeName(*m_typeinfo); }
/** Overridden functions checks whether the property has a valid value.
*
* @return empty string ""
*/
std::string Property::isValid() const {
// the no error condition
return "";
}
/**
* Set the PropertySettings determining when this property is visible/enabled.
* Takes ownership of the given object
* @param settings A pointer to an object specifying the settings type
*/
void Property::setSettings(std::unique_ptr<IPropertySettings> settings) { m_settings = std::move(settings); }
/**
*
* @return the PropertySettings for this property
*/
IPropertySettings *Property::getSettings() { return m_settings.get(); }
/**
* Deletes the PropertySettings object contained
*/
void Property::clearSettings() { m_settings.reset(nullptr); }
/**
* Whether to remember this property input
* @return whether to remember this property's input
*/
bool Property::remember() const { return m_remember; }
/**
* Set wheter to remeber this property input
* @param remember :: true to remember
*/
void Property::setRemember(bool remember) { m_remember = remember; }
/**
* Returns the value as a pretty printed string
* The default implementation just returns the value with the size limit applied
* @param maxLength :: The Max length of the returned string
* @param collapseLists :: Whether to collapse 1,2,3 into 1-3
*/
std::string Property::valueAsPrettyStr(const size_t maxLength, const bool collapseLists) const {
UNUSED_ARG(collapseLists);
return Strings::shorten(value(), maxLength);
}
/** Sets the user level description of the property.
* In addition, if the brief documentation string is empty it will be set to
* the portion of the provided string up to the first period
* (or the entire string if no period is found).
* @param documentation The string containing the descriptive comment
*/
void Property::setDocumentation(const std::string &documentation) {
m_documentation = documentation;
if (m_shortDoc.empty()) {
auto period = documentation.find_first_of('.');
setBriefDocumentation(documentation.substr(0, period));
}
}
/** Sets the
*
*/
void Property::setBriefDocumentation(const std::string &documentation) { m_shortDoc = documentation; }
/** Returns the set of valid values for this property, if such a set exists.
* If not, it returns an empty set.
* @return the set of valid values for this property or an empty set
*/
std::vector<std::string> Property::allowedValues() const { return std::vector<std::string>(); }
/// Create a PropertyHistory object representing the current state of the
/// Property.
const PropertyHistory Property::createHistory() const { return PropertyHistory(this); }
/** Creates a temporary property value based on the memory address of
* the property.
*/
void Property::createTemporaryValue() {
std::ostringstream os;
os << "__TMP" << this;
this->setValue(os.str());
}
/** Checks if the property value is a temporary one based on the memory address
* of
* the property.
*/
bool Property::hasTemporaryValue() const {
std::ostringstream os;
os << "__TMP" << this;
return (os.str() == this->value());
}
//-------------------------------------------------------------------------------------------------
/** Return the size of this property.
* Single-Value properties return 1.
* TimeSeriesProperties return the # of entries.
* @return the size of the property
*/
int Property::size() const { return 1; }
//-------------------------------------------------------------------------------------------------
/** Returns the units of the property, if any, as a string.
* Units are optional, and will return empty string if they have
* not been set before.
* @return the property's units
*/
const std::string &Property::units() const { return m_units; }
//-------------------------------------------------------------------------------------------------
/** Sets the units of the property, as a string. This is optional.
*
* @param unit :: string to set for the units.
*/
void Property::setUnits(const std::string &unit) { m_units = unit; }
//-------------------------------------------------------------------------------------------------
/** Filter out a property by time. Will be overridden by TimeSeriesProperty
* (only)
* @param start :: the beginning time to filter from
* @param stop :: the ending time to filter to
* */
void Property::filterByTime(const Types::Core::DateAndTime &start, const Types::Core::DateAndTime &stop) {
UNUSED_ARG(start);
UNUSED_ARG(stop);
// Do nothing in general
}
//-----------------------------------------------------------------------------------------------
/** Split a property by time. Will be overridden by TimeSeriesProperty (only)
* For any other property type, this does nothing.
* @param splitter :: time splitter
* @param outputs :: holder for splitter output
* @param isProtonCharge :: a flag to tell whether the property is periodic or
* not
*/
void Property::splitByTime(std::vector<SplittingInterval> &splitter, std::vector<Property *> outputs,
bool isProtonCharge) const {
UNUSED_ARG(splitter);
UNUSED_ARG(outputs);
UNUSED_ARG(isProtonCharge);
}
} // namespace Kernel
//-------------------------- Utility function for class name lookup
//-----------------------------
// MG 16/07/09: Some forward declarations.I need this so
// that the typeid function in getUnmangledTypeName knows about them
// This way I don't need to actually include the headers and I don't
// introduce unwanted dependencies
namespace API {
class Workspace;
class WorkspaceGroup;
class MatrixWorkspace;
class ITableWorkspace;
class IMDEventWorkspace;
class IMDWorkspace;
class IEventWorkspace;
class IPeaksWorkspace;
class IMDHistoWorkspace;
class IFunction;
class IAlgorithm;
} // namespace API
namespace DataObjects {
class EventWorkspace;
class PeaksWorkspace;
class LeanElasticPeaksWorkspace;
class GroupingWorkspace;
class OffsetsWorkspace;
class MaskWorkspace;
class SpecialWorkspace2D;
class Workspace2D;
class TableWorkspace;
class SpecialWorkspace2D;
class SplittersWorkspace;
} // namespace DataObjects
namespace Kernel {
class PropertyManager;
/**
* @param lhs Thing on the left
* @param rhs Thing on the right
* @return true if they are equal
*/
bool operator==(const Property &lhs, const Property &rhs) {
if (lhs.name() != rhs.name())
return false;
if (lhs.type() != rhs.type())
return false;
// check for TimeSeriesProperty specializations
auto lhs_tsp_float = dynamic_cast<const TimeSeriesProperty<float> *>(&lhs);
if (lhs_tsp_float)
return lhs_tsp_float->operator==(rhs);
auto lhs_tsp_double = dynamic_cast<const TimeSeriesProperty<double> *>(&lhs);
if (lhs_tsp_double)
return lhs_tsp_double->operator==(rhs);
auto lhs_tsp_string = dynamic_cast<const TimeSeriesProperty<std::string> *>(&lhs);
if (lhs_tsp_string)
return lhs_tsp_string->operator==(rhs);
auto lhs_tsp_bool = dynamic_cast<const TimeSeriesProperty<bool> *>(&lhs);
if (lhs_tsp_bool)
return lhs_tsp_bool->operator==(rhs);
// use fallthrough behavior
return (lhs.value() == rhs.value());
}
/**
* @param lhs Thing on the left
* @param rhs Thing on the right
* @return true if they are not equal
*/
bool operator!=(const Property &lhs, const Property &rhs) { return (!(lhs == rhs)); }
/**
* Get the unmangled name of the given typestring for some common types that we
* use. Note that
* this is just a lookup and NOT an unmangling algorithm
* @param type :: A pointer to the type_info object for this type
* @returns An unmangled version of the name
*/
std::string getUnmangledTypeName(const std::type_info &type) {
using std::make_pair;
using std::string;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
// Compile a lookup table. This is a static local variable that
// will get initialized when the function is first used
static std::unordered_map<string, string> typestrings;
if (typestrings.empty()) {
typestrings.emplace(typeid(char).name(), string("letter"));
typestrings.emplace(typeid(int).name(), string("number"));
typestrings.emplace(typeid(long long).name(), string("number"));
typestrings.emplace(typeid(int64_t).name(), string("number"));
typestrings.emplace(typeid(double).name(), string("number"));
typestrings.emplace(typeid(bool).name(), string("boolean"));
typestrings.emplace(typeid(string).name(), string("string"));
typestrings.emplace(typeid(std::vector<string>).name(), string("str list"));
typestrings.emplace(typeid(std::vector<int>).name(), string("int list"));
typestrings.emplace(typeid(std::vector<long>).name(), string("long list"));
typestrings.emplace(typeid(std::vector<int64_t>).name(), string("int list"));
typestrings.emplace(typeid(std::vector<size_t>).name(), string("unsigned int list"));
typestrings.emplace(typeid(std::vector<double>).name(), string("dbl list"));
typestrings.emplace(typeid(std::vector<std::vector<string>>).name(), string("list of str lists"));
typestrings.emplace(typeid(OptionalBool).name(), string("optional boolean"));
// Workspaces
typestrings.emplace(typeid(std::shared_ptr<Workspace>).name(), string("Workspace"));
typestrings.emplace(typeid(std::shared_ptr<MatrixWorkspace>).name(), string("MatrixWorkspace"));
typestrings.emplace(typeid(std::shared_ptr<ITableWorkspace>).name(), string("TableWorkspace"));
typestrings.emplace(typeid(std::shared_ptr<IMDWorkspace>).name(), string("IMDWorkspace"));
typestrings.emplace(typeid(std::shared_ptr<IMDEventWorkspace>).name(), string("MDEventWorkspace"));
typestrings.emplace(typeid(std::shared_ptr<IEventWorkspace>).name(), string("IEventWorkspace"));
typestrings.emplace(typeid(std::shared_ptr<Workspace2D>).name(), string("Workspace2D"));
typestrings.emplace(typeid(std::shared_ptr<EventWorkspace>).name(), string("EventWorkspace"));
typestrings.emplace(typeid(std::shared_ptr<PeaksWorkspace>).name(), string("PeaksWorkspace"));
typestrings.emplace(typeid(std::shared_ptr<LeanElasticPeaksWorkspace>).name(), string("LeanElasticPeaksWorkspace"));
typestrings.emplace(typeid(std::shared_ptr<IPeaksWorkspace>).name(), string("IPeaksWorkspace"));
typestrings.emplace(typeid(std::shared_ptr<GroupingWorkspace>).name(), string("GroupingWorkspace"));
typestrings.emplace(typeid(std::shared_ptr<WorkspaceGroup>).name(), string("WorkspaceGroup"));
typestrings.emplace(typeid(std::shared_ptr<OffsetsWorkspace>).name(), string("OffsetsWorkspace"));
typestrings.emplace(typeid(std::shared_ptr<MaskWorkspace>).name(), string("MaskWorkspace"));
typestrings.emplace(typeid(std::shared_ptr<SpecialWorkspace2D>).name(), string("SpecialWorkspace2D"));
typestrings.emplace(typeid(std::shared_ptr<IMDHistoWorkspace>).name(), string("IMDHistoWorkspace"));
typestrings.emplace(typeid(std::shared_ptr<SplittersWorkspace>).name(), string("SplittersWorkspace"));
typestrings.emplace(typeid(std::shared_ptr<SpecialWorkspace2D>).name(), string("SpecialWorkspace2D"));
typestrings.emplace(typeid(std::shared_ptr<TableWorkspace>).name(), string("TableWorkspace"));
// FunctionProperty
typestrings.emplace(typeid(std::shared_ptr<IFunction>).name(), string("Function"));
typestrings.emplace(typeid(std::shared_ptr<IAlgorithm>).name(), string("IAlgorithm"));
typestrings.emplace(typeid(std::shared_ptr<PropertyManager>).name(), string("Dictionary"));
}
auto mitr = typestrings.find(type.name());
if (mitr != typestrings.end()) {
return mitr->second;
}
return type.name();
}
/**
* Returns if the property is set to automatically trim string unput values of
* whitespace
* @returns True/False
*/
bool Property::autoTrim() const { return m_autotrim; }
/**
* Sets if the property is set to automatically trim string unput values of
* whitespace
* @param setting The new setting value
*/
void Property::setAutoTrim(const bool &setting) { m_autotrim = setting; }
} // namespace Kernel
} // namespace Mantid