Mantid
Loading...
Searching...
No Matches
IPropertyManager.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2// Mantid Repository : https://github.com/mantidproject/mantid
3//
4// Copyright © 2007 ISIS Rutherford Appleton Laboratory UKRI,
5// NScD Oak Ridge National Laboratory, European Spallation Source,
6// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
7// SPDX - License - Identifier: GPL - 3.0 +
8#pragma once
9
10#include "MantidKernel/DllConfig.h"
14
15#ifndef Q_MOC_RUN
16#include <memory>
17#endif
18
19#include <memory>
20#include <stdexcept>
21#include <string>
22#include <type_traits>
23#include <unordered_set>
24#include <vector>
25
26namespace Json {
27class Value;
28}
29
30namespace Mantid {
31namespace Types {
32namespace Core {
33class DateAndTime;
34}
35} // namespace Types
36namespace Kernel {
37
38class DataItem;
39class IPropertySettings;
40class OptionalBool;
41class Property;
42class PropertyManager;
43class SplittingInterval;
44template <typename T> class TimeSeriesProperty;
45template <typename T> class Matrix;
46
58class MANTID_KERNEL_DLL IPropertyManager {
59public:
60 virtual ~IPropertyManager() = default;
61
63 virtual void declareProperty(std::unique_ptr<Property> p, const std::string &doc = "") = 0;
64
66 virtual void declareOrReplaceProperty(std::unique_ptr<Property> p, const std::string &doc = "") = 0;
67
79 template <typename T>
80 void declareProperty(const std::string &name, T value, IValidator_sptr validator = std::make_shared<NullValidator>(),
81 const std::string &doc = "", const unsigned int direction = Direction::Input) {
82 std::unique_ptr<PropertyWithValue<T>> p = std::make_unique<PropertyWithValue<T>>(name, value, validator, direction);
83 declareProperty(std::move(p), doc);
84 }
85
96 template <typename T>
97 void declareProperty(const std::string &name, T value, const std::string &doc,
98 const unsigned int direction = Direction::Input) {
99 std::unique_ptr<PropertyWithValue<T>> p =
100 std::make_unique<PropertyWithValue<T>>(name, value, std::make_shared<NullValidator>(), direction);
101 declareProperty(std::move(p), doc);
102 }
103
112 template <typename T> void declareProperty(const std::string &name, T value, const unsigned int direction) {
113 std::unique_ptr<PropertyWithValue<T>> p =
114 std::make_unique<PropertyWithValue<T>>(name, value, std::make_shared<NullValidator>(), direction);
115 declareProperty(std::move(p));
116 }
117
136 void declareProperty(const std::string &name, const char *value,
137 IValidator_sptr validator = std::make_shared<NullValidator>(),
138 const std::string &doc = std::string(), const unsigned int direction = Direction::Input) {
139 if (value == nullptr)
140 throw std::invalid_argument("Attempted to set " + name + " to nullptr");
141 // Simply call templated method, converting character array to a string
142 declareProperty(name, std::string(value), std::move(validator), doc, direction);
143 }
144
165 void declareProperty(const std::string &name, const char *value, const std::string &doc,
166 IValidator_sptr validator = std::make_shared<NullValidator>(),
167 const unsigned int direction = Direction::Input) {
168 if (value == nullptr)
169 throw std::invalid_argument("Attempted to set " + name + " to nullptr");
170 // Simply call templated method, converting character array to a string
171 declareProperty(name, std::string(value), std::move(validator), doc, direction);
172 }
173
182 void declareProperty(const std::string &name, const char *value, const unsigned int direction) {
183 declareProperty(name, std::string(value), std::make_shared<NullValidator>(), "", direction);
184 }
185
187 virtual void removeProperty(const std::string &name, const bool delproperty = true) = 0;
188
190 virtual std::unique_ptr<Property> takeProperty(const size_t index) = 0;
191
192 virtual void resetProperties() = 0;
193
201 const std::string &propertiesString,
202 const std::unordered_set<std::string> &ignoreProperties = std::unordered_set<std::string>()) = 0;
203
211 virtual void
212 setProperties(const std::string &propertiesJson,
213 const std::unordered_set<std::string> &ignoreProperties = std::unordered_set<std::string>(),
214 bool createMissing = false) = 0;
215
222 virtual void
223 setProperties(const ::Json::Value &jsonValue,
224 const std::unordered_set<std::string> &ignoreProperties = std::unordered_set<std::string>(),
225 bool createMissing = false) = 0;
226
231 virtual void setPropertyValue(const std::string &name, const std::string &value) = 0;
232
237 virtual void setPropertyValueFromJson(const std::string &name, const Json::Value &value) = 0;
238
240 virtual void setPropertyOrdinal(const int &index, const std::string &value) = 0;
241
244 virtual bool existsProperty(const std::string &name) const = 0;
245
247 virtual bool validateProperties() const = 0;
249 virtual size_t propertyCount() const = 0;
251 virtual std::string getPropertyValue(const std::string &name) const = 0;
252
254 virtual const std::vector<Property *> &getProperties() const = 0;
255
257 virtual std::vector<std::string> getDeclaredPropertyNames() const noexcept = 0;
258
266 template <typename T> IPropertyManager *setProperty(const std::string &name, const T &value) {
267 return doSetProperty(name, value);
268 }
269
278 template <typename T> IPropertyManager *setProperty(const std::string &name, std::unique_ptr<T> value) {
279 setTypedProperty(name, std::move(value), std::is_convertible<std::unique_ptr<T>, std::shared_ptr<DataItem>>());
280 this->afterPropertySet(name);
281 return this;
282 }
283
292 IPropertyManager *setProperty(const std::string &name, const char *value) {
293 if (value == nullptr)
294 throw std::invalid_argument("Attempted to set " + name + " to nullptr");
295 return setProperty(name, std::string(value));
296 }
297
305 IPropertyManager *setProperty(const std::string &name, const std::string &value) {
306 this->setPropertyValue(name, value);
307 return this;
308 }
309
312
314 virtual std::string asString(bool withDefaultValues = false) const = 0;
315
317 virtual ::Json::Value asJson(bool withDefaultValues = false) const = 0;
318
319 void setPropertySettings(const std::string &name, std::unique_ptr<IPropertySettings> settings);
320
324 void setPropertyGroup(const std::string &name, const std::string &group) {
325 Property *prop = getPointerToProperty(name);
326 if (prop)
327 prop->setGroup(group);
328 }
329
331 std::vector<Property *> getPropertiesInGroup(const std::string &group) const;
332
333 virtual void filterByTime(const Types::Core::DateAndTime & /*start*/, const Types::Core::DateAndTime & /*stop*/) = 0;
334 virtual void splitByTime(std::vector<SplittingInterval> & /*splitter*/,
335 std::vector<PropertyManager *> /* outputs*/) const = 0;
336
337 virtual void filterByProperty(const TimeSeriesProperty<bool> & /*filter*/, const std::vector<std::string> &
338 /* excludedFromFiltering */) = 0;
339
340protected:
342 virtual Property *getPointerToPropertyOrdinal(const int &index) const = 0;
343
354 template <typename T> T getValue(const std::string &name) const;
355
357 virtual void clear() = 0;
361 virtual void afterPropertySet(const std::string &) {}
362
365 struct MANTID_KERNEL_DLL TypedValue {
369 const std::string prop;
370
372 TypedValue(const IPropertyManager &p, const std::string &name) : pm(p), prop(name) {}
373
374 // Unfortunately, MSVS2010 can't handle just having a single templated cast
375 // operator T()
376 // (it has problems with the string one). This operator is needed to convert
377 // a TypedValue
378 // into what we actually want. It can't even handle just having a
379 // specialization for strings.
380 // So we have to explicitly define an operator for each type of property
381 // that we have.
382
383 operator int16_t();
384 operator uint16_t();
385 operator int32_t();
386 operator uint32_t();
387 operator int64_t();
388 operator uint64_t();
389 operator OptionalBool();
390
391#ifdef __APPLE__
392 operator unsigned long();
393#endif
395 operator bool();
397 operator double();
399 operator std::string();
401 operator Property *();
402
404 template <typename T> operator std::vector<T>() { return pm.getValue<std::vector<T>>(prop); }
406 template <typename T> operator std::vector<std::vector<T>>() {
407 return pm.getValue<std::vector<std::vector<T>>>(prop);
408 }
410 template <typename T> operator std::shared_ptr<T>() { return pm.getValue<std::shared_ptr<T>>(prop); }
412 template <typename T> operator std::shared_ptr<const T>() { return pm.getValue<std::shared_ptr<T>>(prop); }
414 template <typename T> operator Matrix<T>() { return pm.getValue<Matrix<T>>(prop); }
415 };
416
417public:
419 virtual TypedValue getProperty(const std::string &name) const = 0;
421 virtual Property *getPointerToProperty(const std::string &name) const = 0;
422
423private:
431 template <typename T> IPropertyManager *doSetProperty(const std::string &name, const T &value) {
432 setTypedProperty(name, value, std::is_convertible<T, std::shared_ptr<DataItem>>());
433 this->afterPropertySet(name);
434 return this;
435 }
436
446 template <typename T> IPropertyManager *doSetProperty(const std::string &name, const std::shared_ptr<T> &value) {
447 // CAREFUL: is_convertible has undefined behavior for incomplete types. If T
448 // is forward-declared in the calling code, e.g., an algorithm that calls
449 // setProperty, compilation and linking do work. However, the BEHAVIOR IS
450 // UNDEFINED and the compiler will not complain, but things crash or go
451 // wrong badly. To circumvent this we call `sizeof` here to force a compiler
452 // error if T is an incomplete type.
453 static_cast<void>(sizeof(T)); // DO NOT REMOVE, enforces complete type
454 setTypedProperty(name, value, std::is_convertible<T *, DataItem *>());
455 this->afterPropertySet(name);
456 return this;
457 }
458
467 template <typename T>
468 IPropertyManager *setTypedProperty(const std::string &name, const T &value, const std::false_type &) {
469 PropertyWithValue<T> *prop = dynamic_cast<PropertyWithValue<T> *>(getPointerToProperty(name));
470 if (prop) {
471 *prop = value;
472 } else {
473 throw std::invalid_argument("Attempt to assign to property (" + name + ") of incorrect type");
474 }
475 return this;
476 }
485 template <typename T>
486 IPropertyManager *setTypedProperty(const std::string &name, const T &value, const std::true_type &) {
487 // T is convertible to DataItem_sptr
488 std::shared_ptr<DataItem> data = std::static_pointer_cast<DataItem>(value);
489 std::string error = getPointerToProperty(name)->setDataItem(data);
490 if (!error.empty()) {
491 throw std::invalid_argument(error);
492 }
493 return this;
494 }
495
505 template <typename T>
506 IPropertyManager *setTypedProperty(const std::string &name, std::unique_ptr<T> value, const std::true_type &) {
507 // T is convertible to DataItem_sptr
508 std::shared_ptr<DataItem> data(std::move(value));
509 std::string error = getPointerToProperty(name)->setDataItem(data);
510 if (!error.empty()) {
511 throw std::invalid_argument(error);
512 }
513 return this;
514 }
515};
516
517} // namespace Kernel
518} // namespace Mantid
519
523#define DEFINE_IPROPERTYMANAGER_GETVALUE(type) \
524 namespace Mantid { \
525 namespace Kernel { \
526 template <> DLLExport type IPropertyManager::getValue<type>(const std::string &name) const { \
527 PropertyWithValue<type> *prop = dynamic_cast<PropertyWithValue<type> *>(getPointerToProperty(name)); \
528 if (prop) { \
529 return *prop; \
530 } else { \
531 std::string message = "Attempt to assign property " + name + " to incorrect type. Expected type " #type; \
532 throw std::runtime_error(message); \
533 } \
534 } \
535 } \
536 }
double value
The value of the point.
Definition: FitMW.cpp:51
double error
Definition: IndexPeaks.cpp:133
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
Interface to PropertyManager.
void declareProperty(const std::string &name, const char *value, IValidator_sptr validator=std::make_shared< NullValidator >(), const std::string &doc=std::string(), const unsigned int direction=Direction::Input)
Specialised version of declareProperty template method to prevent the creation of a PropertyWithValue...
virtual void clear()=0
Clears all properties under management.
virtual Property * getPointerToPropertyOrdinal(const int &index) const =0
Get a property by an index.
IPropertyManager * doSetProperty(const std::string &name, const T &value)
Helper method to set the value of a PropertyWithValue.
IPropertyManager * setProperty(const std::string &name, const char *value)
Specialised version of setProperty template method to handle const char *.
virtual void setPropertyValue(const std::string &name, const std::string &value)=0
Sets property value from a string.
virtual bool validateProperties() const =0
Validates all the properties in the collection.
void declareProperty(const std::string &name, T value, const unsigned int direction)
Add a property of the template type to the list of managed properties.
IPropertyManager * setProperty(const std::string &name, const std::string &value)
Specialised version of setProperty template method to handle std::string.
virtual ::Json::Value asJson(bool withDefaultValues=false) const =0
Return the property manager serialized as a json object.
virtual void setProperties(const ::Json::Value &jsonValue, const std::unordered_set< std::string > &ignoreProperties=std::unordered_set< std::string >(), bool createMissing=false)=0
Sets all the properties from a json object.
std::vector< Property * > getPropertiesInGroup(const std::string &group) const
Get the list of managed properties in a given group.
IPropertyManager * setTypedProperty(const std::string &name, std::unique_ptr< T > value, const std::true_type &)
Set a property value from std::unique_ptr that is convertible to a DataItem_sptr.
void setPropertySettings(const std::string &name, std::unique_ptr< IPropertySettings > settings)
virtual bool existsProperty(const std::string &name) const =0
Checks whether the named property is already in the list of managed property.
virtual void setPropertyOrdinal(const int &index, const std::string &value)=0
Set the value of a property by an index.
virtual void setPropertiesWithString(const std::string &propertiesString, const std::unordered_set< std::string > &ignoreProperties=std::unordered_set< std::string >())=0
Sets all the declared properties from a string.
void declareProperty(const std::string &name, T value, const std::string &doc, const unsigned int direction=Direction::Input)
Add a property to the list of managed properties with no validator.
virtual void removeProperty(const std::string &name, const bool delproperty=true)=0
Removes the property from management.
virtual ~IPropertyManager()=default
IPropertyManager * doSetProperty(const std::string &name, const std::shared_ptr< T > &value)
Helper method to set the value of a PropertyWithValue, variant for shared_ptr types.
virtual std::string asString(bool withDefaultValues=false) const =0
Return the property manager serialized as a string.
void declareProperty(const std::string &name, const char *value, const unsigned int direction)
Add a property of string type to the list of managed properties.
virtual void filterByTime(const Types::Core::DateAndTime &, const Types::Core::DateAndTime &)=0
virtual void declareProperty(std::unique_ptr< Property > p, const std::string &doc="")=0
Function to declare properties (i.e. store them)
IPropertyManager * setTypedProperty(const std::string &name, const T &value, const std::false_type &)
Set a property value that is not convertible to a DataItem_sptr.
virtual void declareOrReplaceProperty(std::unique_ptr< Property > p, const std::string &doc="")=0
Function to declare properties (i.e. store them)
virtual Property * getPointerToProperty(const std::string &name) const =0
Get a pointer to property by name.
virtual std::vector< std::string > getDeclaredPropertyNames() const noexcept=0
Get the list of managed property names.
virtual TypedValue getProperty(const std::string &name) const =0
Get the value of a property.
virtual void setProperties(const std::string &propertiesJson, const std::unordered_set< std::string > &ignoreProperties=std::unordered_set< std::string >(), bool createMissing=false)=0
Sets all properties from a string.
virtual void filterByProperty(const TimeSeriesProperty< bool > &, const std::vector< std::string > &)=0
virtual std::string getPropertyValue(const std::string &name) const =0
Get the value of a property as a string.
void declareProperty(const std::string &name, const char *value, const std::string &doc, IValidator_sptr validator=std::make_shared< NullValidator >(), const unsigned int direction=Direction::Input)
Specialised version of declareProperty template method to prevent the creation of a PropertyWithValue...
virtual size_t propertyCount() const =0
Returns the number of properties under management.
virtual std::unique_ptr< Property > takeProperty(const size_t index)=0
Removes the property from management and returns a pointer to it.
virtual void afterPropertySet(const std::string &)
Override this method to perform a custom action right after a property was set.
virtual void splitByTime(std::vector< SplittingInterval > &, std::vector< PropertyManager * >) const =0
void declareProperty(const std::string &name, T value, IValidator_sptr validator=std::make_shared< NullValidator >(), const std::string &doc="", const unsigned int direction=Direction::Input)
Add a property of the template type to the list of managed properties.
void updatePropertyValues(const IPropertyManager &other)
Update values of the existing properties.
void setPropertyGroup(const std::string &name, const std::string &group)
Set the group for a given property.
virtual const std::vector< Property * > & getProperties() const =0
Get the list of managed properties.
IPropertyManager * setProperty(const std::string &name, std::unique_ptr< T > value)
Templated method to set the value of a PropertyWithValue from a std::unique_ptr.
IPropertyManager * setTypedProperty(const std::string &name, const T &value, const std::true_type &)
Set a property value that is convertible to a DataItem_sptr.
T getValue(const std::string &name) const
Templated method to get the value of a property.
virtual void setPropertyValueFromJson(const std::string &name, const Json::Value &value)=0
Sets property value from a Json::Value.
Numerical Matrix class.
Definition: Matrix.h:42
OptionalBool : Tri-state bool.
Definition: OptionalBool.h:25
The concrete, templated class for properties.
Base class for properties.
Definition: Property.h:94
void setGroup(const std::string &group)
Set the group this property belongs to.
Definition: Property.h:194
A specialised Property class for holding a series of time-value pairs.
Definition: Algorithm.h:38
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
Definition: IValidator.h:26
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.
Utility class that enables the getProperty() method to effectively be templated on the return type.
const IPropertyManager & pm
Reference to the containing PropertyManager.
TypedValue(const IPropertyManager &p, const std::string &name)
Constructor.
const std::string prop
The name of the property desired.