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"
13
14#ifndef Q_MOC_RUN
15#include <memory>
16#endif
17
18#include <memory>
19#include <stdexcept>
20#include <string>
21#include <type_traits>
22#include <unordered_set>
23#include <vector>
24
25namespace Json {
26class Value;
27}
28
29namespace Mantid {
30namespace Types {
31namespace Core {
32class DateAndTime;
33}
34} // namespace Types
35namespace Kernel {
36
37class DataItem;
38class IPropertySettings;
39class OptionalBool;
40class Property;
41class PropertyManager;
42class SplittingInterval;
43class LogFilter;
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 = nullptr,
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 = std::make_unique<PropertyWithValue<T>>(name, value, direction);
100 declareProperty(std::move(p), doc);
101 }
102
111 template <typename T> void declareProperty(const std::string &name, T value, const unsigned int direction) {
112 std::unique_ptr<PropertyWithValue<T>> p = std::make_unique<PropertyWithValue<T>>(name, value, direction);
113 declareProperty(std::move(p));
114 }
115
134 void declareProperty(const std::string &name, const char *value, IValidator_sptr validator = nullptr,
135 const std::string &doc = std::string(), const unsigned int direction = Direction::Input) {
136 if (value == nullptr)
137 throw std::invalid_argument("Attempted to set " + name + " to nullptr");
138 // Simply call templated method, converting character array to a string
139 declareProperty(name, std::string(value), std::move(validator), doc, direction);
140 }
141
162 void declareProperty(const std::string &name, const char *value, const std::string &doc,
163 IValidator_sptr validator = nullptr, const unsigned int direction = Direction::Input) {
164 if (value == nullptr)
165 throw std::invalid_argument("Attempted to set " + name + " to nullptr");
166 // Simply call templated method, converting character array to a string
167 declareProperty(name, std::string(value), std::move(validator), doc, direction);
168 }
169
178 void declareProperty(const std::string &name, const char *value, const unsigned int direction) {
179 if (value == nullptr)
180 throw std::invalid_argument("Attempted to set " + name + " to nullptr");
181
182 declareProperty(name, std::string(value), nullptr, "", direction);
183 }
184
186 virtual void removeProperty(const std::string &name, const bool delproperty = true) = 0;
187
189 virtual std::unique_ptr<Property> takeProperty(const size_t index) = 0;
190
191 virtual void resetProperties() = 0;
192
200 const std::string &propertiesString,
201 const std::unordered_set<std::string> &ignoreProperties = std::unordered_set<std::string>()) = 0;
202
210 virtual void
211 setProperties(const std::string &propertiesJson,
212 const std::unordered_set<std::string> &ignoreProperties = std::unordered_set<std::string>(),
213 bool createMissing = false) = 0;
214
221 virtual void
222 setProperties(const ::Json::Value &jsonValue,
223 const std::unordered_set<std::string> &ignoreProperties = std::unordered_set<std::string>(),
224 bool createMissing = false) = 0;
225
230 virtual void setPropertyValue(const std::string &name, const std::string &value) = 0;
231
236 virtual void setPropertyValueFromJson(const std::string &name, const Json::Value &value) = 0;
237
239 virtual void setPropertyOrdinal(const int &index, const std::string &value) = 0;
240
243 virtual bool existsProperty(const std::string &name) const = 0;
244
246 virtual bool validateProperties() const = 0;
248 virtual size_t propertyCount() const = 0;
250 virtual std::string getPropertyValue(const std::string &name) const = 0;
251
253 virtual const std::vector<Property *> &getProperties() const = 0;
254
256 virtual std::vector<std::string> getDeclaredPropertyNames() const noexcept = 0;
257
265 template <typename T> IPropertyManager *setProperty(const std::string &name, const T &value) {
266 return doSetProperty(name, value);
267 }
268
277 template <typename T> IPropertyManager *setProperty(const std::string &name, std::unique_ptr<T> value) {
278 setTypedProperty(name, std::move(value), std::is_convertible<std::unique_ptr<T>, std::shared_ptr<DataItem>>());
279 this->afterPropertySet(name);
280 return this;
281 }
282
291 IPropertyManager *setProperty(const std::string &name, const char *value) {
292 if (value == nullptr)
293 throw std::invalid_argument("Attempted to set " + name + " to nullptr");
294 return setProperty(name, std::string(value));
295 }
296
304 IPropertyManager *setProperty(const std::string &name, const std::string &value) {
305 this->setPropertyValue(name, value);
306 return this;
307 }
308
311
313 virtual std::string asString(bool withDefaultValues = false) const = 0;
314
316 virtual ::Json::Value asJson(bool withDefaultValues = false) const = 0;
317
319 void setPropertySettings(const std::string &name, std::unique_ptr<IPropertySettings const> settings);
320
322 bool isPropertyEnabled(const std::string &name) const;
323
325 bool isPropertyVisible(const std::string &name) const;
326
330 void setPropertyGroup(const std::string &name, const std::string &group) {
331 Property *prop = getPointerToProperty(name);
332 if (prop)
333 prop->setGroup(group);
334 }
335
337 std::vector<Property *> getPropertiesInGroup(const std::string &group) const;
338
339 virtual void filterByProperty(Mantid::Kernel::LogFilter * /*logFilter*/, const std::vector<std::string> &
340 /* excludedFromFiltering */) {
341 throw std::logic_error("Not implemented yet.");
342 }
343
344protected:
346 virtual Property *getPointerToPropertyOrdinal(const int &index) const = 0;
347
358 template <typename T> T getValue(const std::string &name) const;
359
361 virtual void clear() = 0;
365 virtual void afterPropertySet(const std::string &) {}
366
369 struct MANTID_KERNEL_DLL TypedValue {
373 const std::string prop;
374
376 TypedValue(const IPropertyManager &p, const std::string &name) : pm(p), prop(name) {}
377
378 // Unfortunately, MSVS2010 can't handle just having a single templated cast
379 // operator T()
380 // (it has problems with the string one). This operator is needed to convert
381 // a TypedValue
382 // into what we actually want. It can't even handle just having a
383 // specialization for strings.
384 // So we have to explicitly define an operator for each type of property
385 // that we have.
386
387 operator int16_t();
388 operator uint16_t();
389 operator int32_t();
390 operator uint32_t();
391 operator int64_t();
392 operator uint64_t();
393 operator OptionalBool();
394
395#ifdef __APPLE__
396 operator unsigned long();
397#endif
399 operator bool();
401 operator double();
403 operator std::string();
405 operator Property *();
406
408 template <typename T> operator std::vector<T>() { return pm.getValue<std::vector<T>>(prop); }
410 template <typename T> operator std::vector<std::vector<T>>() {
411 return pm.getValue<std::vector<std::vector<T>>>(prop);
412 }
414 template <typename T> operator std::shared_ptr<T>() { return pm.getValue<std::shared_ptr<T>>(prop); }
416 template <typename T> operator std::shared_ptr<const T>() { return pm.getValue<std::shared_ptr<T>>(prop); }
418 template <typename T> operator Matrix<T>() { return pm.getValue<Matrix<T>>(prop); }
419 };
420
421public:
423 virtual TypedValue getProperty(const std::string &name) const = 0;
425 virtual Property *getPointerToProperty(const std::string &name) const = 0;
426
427private:
435 template <typename T> IPropertyManager *doSetProperty(const std::string &name, const T &value) {
436 setTypedProperty(name, value, std::is_convertible<T, std::shared_ptr<DataItem>>());
437 this->afterPropertySet(name);
438 return this;
439 }
440
450 template <typename T> IPropertyManager *doSetProperty(const std::string &name, const std::shared_ptr<T> &value) {
451 // CAREFUL: is_convertible has undefined behavior for incomplete types. If T
452 // is forward-declared in the calling code, e.g., an algorithm that calls
453 // setProperty, compilation and linking do work. However, the BEHAVIOR IS
454 // UNDEFINED and the compiler will not complain, but things crash or go
455 // wrong badly. To circumvent this we call `sizeof` here to force a compiler
456 // error if T is an incomplete type.
457 static_cast<void>(sizeof(T)); // DO NOT REMOVE, enforces complete type
458 setTypedProperty(name, value, std::is_convertible<T *, DataItem *>());
459 this->afterPropertySet(name);
460 return this;
461 }
462
471 template <typename T>
472 IPropertyManager *setTypedProperty(const std::string &name, const T &value, const std::false_type &) {
473 PropertyWithValue<T> *prop = dynamic_cast<PropertyWithValue<T> *>(getPointerToProperty(name));
474 if (prop) {
475 *prop = value;
476 } else {
477 throw std::invalid_argument("Attempt to assign to property (" + name + ") of incorrect type");
478 }
479 return this;
480 }
489 template <typename T>
490 IPropertyManager *setTypedProperty(const std::string &name, const T &value, const std::true_type &) {
491 // T is convertible to DataItem_sptr
492 std::shared_ptr<DataItem> data = std::static_pointer_cast<DataItem>(value);
493 std::string error = getPointerToProperty(name)->setDataItem(data);
494 if (!error.empty()) {
495 throw std::invalid_argument(error);
496 }
497 return this;
498 }
499
509 template <typename T>
510 IPropertyManager *setTypedProperty(const std::string &name, std::unique_ptr<T> value, const std::true_type &) {
511 // T is convertible to DataItem_sptr
512 std::shared_ptr<DataItem> data(std::move(value));
513 std::string error = getPointerToProperty(name)->setDataItem(data);
514 if (!error.empty()) {
515 throw std::invalid_argument(error);
516 }
517 return this;
518 }
519};
520
521} // namespace Kernel
522} // namespace Mantid
523
527#define DEFINE_IPROPERTYMANAGER_GETVALUE(type) \
528 namespace Mantid { \
529 namespace Kernel { \
530 template <> MANTID_KERNEL_DLL type IPropertyManager::getValue<type>(const std::string &name) const { \
531 PropertyWithValue<type> *prop = dynamic_cast<PropertyWithValue<type> *>(getPointerToProperty(name)); \
532 if (prop) { \
533 return *prop; \
534 } else { \
535 std::string message = "Attempt to assign property " + name + " to incorrect type. Expected type " #type; \
536 throw std::runtime_error(message); \
537 } \
538 } \
539 } \
540 }
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.
virtual void clear()=0
Clears all properties under management.
void declareProperty(const std::string &name, const char *value, IValidator_sptr validator=nullptr, 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 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.
bool isPropertyVisible(const std::string &name) const
Test if a given property should be visible.
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.
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.
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 updatePropertyValues(const IPropertyManager &other)
Update values of the existing properties.
void declareProperty(const std::string &name, T value, IValidator_sptr validator=nullptr, const std::string &doc="", const unsigned int direction=Direction::Input)
Add a property of the template type to the list of managed properties.
virtual void filterByProperty(Mantid::Kernel::LogFilter *, const std::vector< std::string > &)
void setPropertySettings(const std::string &name, std::unique_ptr< IPropertySettings const > settings)
Add a PropertySettings instance to the chain of settings for a given property.
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.
bool isPropertyEnabled(const std::string &name) const
Test if a given property should be enabled.
T getValue(const std::string &name) const
Templated method to get the value of a property.
void declareProperty(const std::string &name, const char *value, const std::string &doc, IValidator_sptr validator=nullptr, const unsigned int direction=Direction::Input)
Specialised version of declareProperty template method to prevent the creation of a PropertyWithValue...
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:41
OptionalBool : Tri-state bool.
The concrete, templated class for properties.
Base class for properties.
Definition Property.h:95
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.
class MANTID_KERNEL_DLL PropertyManager
This file provides forward declarations for Mantid::Kernel::PropertyManager.
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.