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;
44class LogFilter;
45template <typename T> class TimeSeriesProperty;
46template <typename T> class Matrix;
47
59class MANTID_KERNEL_DLL IPropertyManager {
60public:
61 virtual ~IPropertyManager() = default;
62
64 virtual void declareProperty(std::unique_ptr<Property> p, const std::string &doc = "") = 0;
65
67 virtual void declareOrReplaceProperty(std::unique_ptr<Property> p, const std::string &doc = "") = 0;
68
80 template <typename T>
81 void declareProperty(const std::string &name, T value, IValidator_sptr validator = std::make_shared<NullValidator>(),
82 const std::string &doc = "", const unsigned int direction = Direction::Input) {
83 std::unique_ptr<PropertyWithValue<T>> p = std::make_unique<PropertyWithValue<T>>(name, value, validator, direction);
84 declareProperty(std::move(p), doc);
85 }
86
97 template <typename T>
98 void declareProperty(const std::string &name, T value, const std::string &doc,
99 const unsigned int direction = Direction::Input) {
100 std::unique_ptr<PropertyWithValue<T>> p =
101 std::make_unique<PropertyWithValue<T>>(name, value, std::make_shared<NullValidator>(), direction);
102 declareProperty(std::move(p), doc);
103 }
104
113 template <typename T> void declareProperty(const std::string &name, T value, const unsigned int direction) {
114 std::unique_ptr<PropertyWithValue<T>> p =
115 std::make_unique<PropertyWithValue<T>>(name, value, std::make_shared<NullValidator>(), direction);
116 declareProperty(std::move(p));
117 }
118
137 void declareProperty(const std::string &name, const char *value,
138 IValidator_sptr validator = std::make_shared<NullValidator>(),
139 const std::string &doc = std::string(), const unsigned int direction = Direction::Input) {
140 if (value == nullptr)
141 throw std::invalid_argument("Attempted to set " + name + " to nullptr");
142 // Simply call templated method, converting character array to a string
143 declareProperty(name, std::string(value), std::move(validator), doc, direction);
144 }
145
166 void declareProperty(const std::string &name, const char *value, const std::string &doc,
167 IValidator_sptr validator = std::make_shared<NullValidator>(),
168 const unsigned int direction = Direction::Input) {
169 if (value == nullptr)
170 throw std::invalid_argument("Attempted to set " + name + " to nullptr");
171 // Simply call templated method, converting character array to a string
172 declareProperty(name, std::string(value), std::move(validator), doc, direction);
173 }
174
183 void declareProperty(const std::string &name, const char *value, const unsigned int direction) {
184 if (value == nullptr)
185 throw std::invalid_argument("Attempted to set " + name + " to nullptr");
186
187 declareProperty(name, std::string(value), std::make_shared<NullValidator>(), "", direction);
188 }
189
191 virtual void removeProperty(const std::string &name, const bool delproperty = true) = 0;
192
194 virtual std::unique_ptr<Property> takeProperty(const size_t index) = 0;
195
196 virtual void resetProperties() = 0;
197
205 const std::string &propertiesString,
206 const std::unordered_set<std::string> &ignoreProperties = std::unordered_set<std::string>()) = 0;
207
215 virtual void
216 setProperties(const std::string &propertiesJson,
217 const std::unordered_set<std::string> &ignoreProperties = std::unordered_set<std::string>(),
218 bool createMissing = false) = 0;
219
226 virtual void
227 setProperties(const ::Json::Value &jsonValue,
228 const std::unordered_set<std::string> &ignoreProperties = std::unordered_set<std::string>(),
229 bool createMissing = false) = 0;
230
235 virtual void setPropertyValue(const std::string &name, const std::string &value) = 0;
236
241 virtual void setPropertyValueFromJson(const std::string &name, const Json::Value &value) = 0;
242
244 virtual void setPropertyOrdinal(const int &index, const std::string &value) = 0;
245
248 virtual bool existsProperty(const std::string &name) const = 0;
249
251 virtual bool validateProperties() const = 0;
253 virtual size_t propertyCount() const = 0;
255 virtual std::string getPropertyValue(const std::string &name) const = 0;
256
258 virtual const std::vector<Property *> &getProperties() const = 0;
259
261 virtual std::vector<std::string> getDeclaredPropertyNames() const noexcept = 0;
262
270 template <typename T> IPropertyManager *setProperty(const std::string &name, const T &value) {
271 return doSetProperty(name, value);
272 }
273
282 template <typename T> IPropertyManager *setProperty(const std::string &name, std::unique_ptr<T> value) {
283 setTypedProperty(name, std::move(value), std::is_convertible<std::unique_ptr<T>, std::shared_ptr<DataItem>>());
284 this->afterPropertySet(name);
285 return this;
286 }
287
296 IPropertyManager *setProperty(const std::string &name, const char *value) {
297 if (value == nullptr)
298 throw std::invalid_argument("Attempted to set " + name + " to nullptr");
299 return setProperty(name, std::string(value));
300 }
301
309 IPropertyManager *setProperty(const std::string &name, const std::string &value) {
310 this->setPropertyValue(name, value);
311 return this;
312 }
313
316
318 virtual std::string asString(bool withDefaultValues = false) const = 0;
319
321 virtual ::Json::Value asJson(bool withDefaultValues = false) const = 0;
322
323 void setPropertySettings(const std::string &name, std::unique_ptr<IPropertySettings> settings);
324
328 void setPropertyGroup(const std::string &name, const std::string &group) {
329 Property *prop = getPointerToProperty(name);
330 if (prop)
331 prop->setGroup(group);
332 }
333
335 std::vector<Property *> getPropertiesInGroup(const std::string &group) const;
336
337 virtual void filterByProperty(Mantid::Kernel::LogFilter * /*logFilter*/, const std::vector<std::string> &
338 /* excludedFromFiltering */) {
339 throw std::logic_error("Not implemented yet.");
340 }
341
342protected:
344 virtual Property *getPointerToPropertyOrdinal(const int &index) const = 0;
345
356 template <typename T> T getValue(const std::string &name) const;
357
359 virtual void clear() = 0;
363 virtual void afterPropertySet(const std::string &) {}
364
367 struct MANTID_KERNEL_DLL TypedValue {
371 const std::string prop;
372
374 TypedValue(const IPropertyManager &p, const std::string &name) : pm(p), prop(name) {}
375
376 // Unfortunately, MSVS2010 can't handle just having a single templated cast
377 // operator T()
378 // (it has problems with the string one). This operator is needed to convert
379 // a TypedValue
380 // into what we actually want. It can't even handle just having a
381 // specialization for strings.
382 // So we have to explicitly define an operator for each type of property
383 // that we have.
384
385 operator int16_t();
386 operator uint16_t();
387 operator int32_t();
388 operator uint32_t();
389 operator int64_t();
390 operator uint64_t();
391 operator OptionalBool();
392
393#ifdef __APPLE__
394 operator unsigned long();
395#endif
397 operator bool();
399 operator double();
401 operator std::string();
403 operator Property *();
404
406 template <typename T> operator std::vector<T>() { return pm.getValue<std::vector<T>>(prop); }
408 template <typename T> operator std::vector<std::vector<T>>() {
409 return pm.getValue<std::vector<std::vector<T>>>(prop);
410 }
412 template <typename T> operator std::shared_ptr<T>() { return pm.getValue<std::shared_ptr<T>>(prop); }
414 template <typename T> operator std::shared_ptr<const T>() { return pm.getValue<std::shared_ptr<T>>(prop); }
416 template <typename T> operator Matrix<T>() { return pm.getValue<Matrix<T>>(prop); }
417 };
418
419public:
421 virtual TypedValue getProperty(const std::string &name) const = 0;
423 virtual Property *getPointerToProperty(const std::string &name) const = 0;
424
425private:
433 template <typename T> IPropertyManager *doSetProperty(const std::string &name, const T &value) {
434 setTypedProperty(name, value, std::is_convertible<T, std::shared_ptr<DataItem>>());
435 this->afterPropertySet(name);
436 return this;
437 }
438
448 template <typename T> IPropertyManager *doSetProperty(const std::string &name, const std::shared_ptr<T> &value) {
449 // CAREFUL: is_convertible has undefined behavior for incomplete types. If T
450 // is forward-declared in the calling code, e.g., an algorithm that calls
451 // setProperty, compilation and linking do work. However, the BEHAVIOR IS
452 // UNDEFINED and the compiler will not complain, but things crash or go
453 // wrong badly. To circumvent this we call `sizeof` here to force a compiler
454 // error if T is an incomplete type.
455 static_cast<void>(sizeof(T)); // DO NOT REMOVE, enforces complete type
456 setTypedProperty(name, value, std::is_convertible<T *, DataItem *>());
457 this->afterPropertySet(name);
458 return this;
459 }
460
469 template <typename T>
470 IPropertyManager *setTypedProperty(const std::string &name, const T &value, const std::false_type &) {
471 PropertyWithValue<T> *prop = dynamic_cast<PropertyWithValue<T> *>(getPointerToProperty(name));
472 if (prop) {
473 *prop = value;
474 } else {
475 throw std::invalid_argument("Attempt to assign to property (" + name + ") of incorrect type");
476 }
477 return this;
478 }
487 template <typename T>
488 IPropertyManager *setTypedProperty(const std::string &name, const T &value, const std::true_type &) {
489 // T is convertible to DataItem_sptr
490 std::shared_ptr<DataItem> data = std::static_pointer_cast<DataItem>(value);
491 std::string error = getPointerToProperty(name)->setDataItem(data);
492 if (!error.empty()) {
493 throw std::invalid_argument(error);
494 }
495 return this;
496 }
497
507 template <typename T>
508 IPropertyManager *setTypedProperty(const std::string &name, std::unique_ptr<T> value, const std::true_type &) {
509 // T is convertible to DataItem_sptr
510 std::shared_ptr<DataItem> data(std::move(value));
511 std::string error = getPointerToProperty(name)->setDataItem(data);
512 if (!error.empty()) {
513 throw std::invalid_argument(error);
514 }
515 return this;
516 }
517};
518
519} // namespace Kernel
520} // namespace Mantid
521
525#define DEFINE_IPROPERTYMANAGER_GETVALUE(type) \
526 namespace Mantid { \
527 namespace Kernel { \
528 template <> DLLExport type IPropertyManager::getValue<type>(const std::string &name) const { \
529 PropertyWithValue<type> *prop = dynamic_cast<PropertyWithValue<type> *>(getPointerToProperty(name)); \
530 if (prop) { \
531 return *prop; \
532 } else { \
533 std::string message = "Attempt to assign property " + name + " to incorrect type. Expected type " #type; \
534 throw std::runtime_error(message); \
535 } \
536 } \
537 } \
538 }
std::string name
Definition Run.cpp:60
double value
The value of the point.
Definition FitMW.cpp:51
double error
std::map< DeltaEMode::Type, std::string > index
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 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 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.
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.
virtual void filterByProperty(Mantid::Kernel::LogFilter *, const std::vector< std::string > &)
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.
This class is for filtering TimeSeriesProperty data.
Definition LogFilter.h:32
Numerical Matrix class.
Definition Matrix.h:42
OptionalBool : Tri-state bool.
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:196
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.