Mantid
Loading...
Searching...
No Matches
PropertyManagerProperty.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 +
11
12#include <json/value.h>
13
14#include <sstream>
15
16namespace Mantid::Kernel {
17
18// -----------------------------------------------------------------------------
19// Public Methods
20// -----------------------------------------------------------------------------
26PropertyManagerProperty::PropertyManagerProperty(const std::string &name, unsigned int direction)
27 : PropertyManagerProperty(name, ValueType(), direction) {}
28
35PropertyManagerProperty::PropertyManagerProperty(const std::string &name, const ValueType &defaultValue,
36 unsigned int direction)
37 : BaseClass(name, defaultValue, direction), m_dataServiceKey(), m_defaultAsStr() {
38 if (name.empty()) {
39 throw std::invalid_argument("PropertyManagerProperty() requires a name");
40 }
41 if (defaultValue)
42 m_defaultAsStr = defaultValue->asString(true);
43}
44
48std::string PropertyManagerProperty::value() const {
49 if (m_dataServiceKey.empty()) {
50 auto mgr = (*this)();
51 return (mgr ? mgr->asString(true) : "");
52 } else
53 return m_dataServiceKey;
54}
55
59Json::Value PropertyManagerProperty::valueAsJson() const { return (*this)()->asJson(true); }
60
66
76std::string PropertyManagerProperty::setValue(const std::string &strValue) {
77 auto &globalPropMgrs = PropertyManagerDataService::Instance();
78 try {
79 (*this) = globalPropMgrs.retrieve(strValue);
80 m_dataServiceKey = strValue;
81 return "";
82 } catch (Exception::NotFoundError &) {
83 // try the string as json
84 }
85
86 auto value = (*this)();
87 if (!value) {
88 value = std::make_shared<PropertyManager>();
89 (*this) = value;
90 }
91 std::string strValueToSet = strValue.empty() ? "{}" : strValue;
92 std::ostringstream msg;
93 try {
94 const std::unordered_set<std::string> ignored;
95 bool createMissing{true};
96 value->resetProperties();
97 value->setProperties(strValueToSet, ignored, createMissing);
98 m_dataServiceKey.clear();
99 } catch (std::invalid_argument &exc) {
100 msg << "Error setting value from string.\n"
101 "String is expected to contain either the name of a global "
102 "PropertyManager or a json-formatted object.\n"
103 "Parser error: "
104 << exc.what();
105 } catch (std::exception &exc) {
106 msg << "Error setting value from string.\n" << exc.what();
107 }
108 return msg.str();
109}
110
117std::string PropertyManagerProperty::setValueFromJson(const Json::Value &value) {
118 if (value.type() == Json::objectValue) {
119 try {
121 return "";
122 } catch (std::exception &exc) {
123 return std::string("Attempt to set Json value to property failed: ") + exc.what();
124 }
125 } else {
126 return std::string("Attempt to set incorrect Json type to "
127 "PropertyManager. Expected objectValue, found " +
128 std::to_string(value.type()));
129 }
130}
131
132// -----------------------------------------------------------------------------
133// IPropertyManager::getValue instantiations
134// -----------------------------------------------------------------------------
135template <>
136MANTID_KERNEL_DLL PropertyManager_sptr IPropertyManager::getValue<PropertyManager_sptr>(const std::string &name) const {
137 auto *prop = dynamic_cast<PropertyWithValue<PropertyManager_sptr> *>(getPointerToProperty(name));
138 if (prop) {
139 return *prop;
140 } else {
141 throw std::runtime_error(std::string("Attempt to assign property ") + name +
142 " to incorrect type. Expected shared_ptr<PropertyManager>.");
143 }
144}
145
146template <>
148IPropertyManager::getValue<PropertyManager_const_sptr>(const std::string &name) const {
149 auto *prop = dynamic_cast<PropertyWithValue<PropertyManager_sptr> *>(getPointerToProperty(name));
150 if (prop) {
151 return prop->operator()();
152 } else {
153 std::string message =
154 "Attempt to assign property " + name + " to incorrect type. Expected const shared_ptr<PropertyManager>.";
155 throw std::runtime_error(message);
156 }
157}
158
159} // namespace Mantid::Kernel
double value
The value of the point.
Definition: FitMW.cpp:51
Exception for when an item is not found in a collection.
Definition: Exception.h:145
virtual Property * getPointerToProperty(const std::string &name) const =0
Get a pointer to property by name.
Json::Value valueAsJson() const override
Create a Json::Value objectValue from the PropertyManager.
std::string setValueFromJson(const Json::Value &value) override
Sets the value of the property from a Json object.
std::string setValue(const std::string &strValue) override
Overwrite the current value.
PropertyManagerProperty(const std::string &name, unsigned int direction=Direction::Input)
Constructor with a default empty pointer.
const std::string & name() const
Get the property's name.
Definition: Property.cpp:60
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< const PropertyManager > PropertyManager_const_sptr
shared pointer to Mantid::Kernel::PropertyManager(const version)
MANTID_KERNEL_DLL PropertyManager_sptr createPropertyManager(const Json::Value &keyValues)
Attempt to create a PropertyManager from the Json::Value.
std::shared_ptr< PropertyManager > PropertyManager_sptr
Typedef for a shared pointer to a PropertyManager.
std::string to_string(const wide_integer< Bits, Signed > &n)