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 +
12
13#include <json/value.h>
14
15#include <sstream>
16
17namespace Mantid::Kernel {
18
19// -----------------------------------------------------------------------------
20// Public Methods
21// -----------------------------------------------------------------------------
27PropertyManagerProperty::PropertyManagerProperty(const std::string &name, unsigned int direction)
28 : PropertyManagerProperty(name, ValueType(), direction) {}
29
36PropertyManagerProperty::PropertyManagerProperty(const std::string &name, const ValueType &defaultValue,
37 unsigned int direction)
38 : BaseClass(name, defaultValue, direction), m_dataServiceKey(), m_defaultAsStr() {
39 if (name.empty()) {
40 throw std::invalid_argument("PropertyManagerProperty() requires a name");
41 }
42 if (defaultValue)
43 m_defaultAsStr = defaultValue->asString(true);
44}
45
49std::string PropertyManagerProperty::value() const {
50 if (m_dataServiceKey.empty()) {
51 auto mgr = (*this)();
52 return (mgr ? mgr->asString(true) : "");
53 } else
54 return m_dataServiceKey;
55}
56
60Json::Value PropertyManagerProperty::valueAsJson() const { return (*this)()->asJson(true); }
61
67
77std::string PropertyManagerProperty::setValue(const std::string &strValue) {
78 auto &globalPropMgrs = PropertyManagerDataService::Instance();
79 try {
80 (*this) = globalPropMgrs.retrieve(strValue);
81 m_dataServiceKey = strValue;
82 return "";
83 } catch (Exception::NotFoundError &) {
84 // try the string as json
85 }
86
87 auto pmValue = (*this)();
88 if (!pmValue) {
89 pmValue = std::make_shared<PropertyManager>();
90 (*this) = pmValue;
91 }
92 std::string strValueToSet = strValue.empty() ? "{}" : strValue;
93 std::ostringstream msg;
94 try {
95 const std::unordered_set<std::string> ignored;
96 bool createMissing{true};
97 pmValue->resetProperties();
98 pmValue->setProperties(strValueToSet, ignored, createMissing);
99 m_dataServiceKey.clear();
100 } catch (std::invalid_argument &exc) {
101 msg << "Error setting value from string.\n"
102 "String is expected to contain either the name of a global "
103 "PropertyManager or a json-formatted object.\n"
104 "Parser error: "
105 << exc.what();
106 } catch (std::exception &exc) {
107 msg << "Error setting value from string.\n" << exc.what();
108 }
109 return msg.str();
110}
111
118std::string PropertyManagerProperty::setValueFromJson(const Json::Value &value) {
119 if (value.type() == Json::objectValue) {
120 try {
122 return "";
123 } catch (std::exception &exc) {
124 return std::string("Attempt to set Json value to property failed: ") + exc.what();
125 }
126 } else {
127 return std::string("Attempt to set incorrect Json type to "
128 "PropertyManager. Expected objectValue, found " +
129 std::to_string(value.type()));
130 }
131}
132
133// -----------------------------------------------------------------------------
134// IPropertyManager::getValue instantiations
135// -----------------------------------------------------------------------------
136template <>
137MANTID_KERNEL_DLL PropertyManager_sptr IPropertyManager::getValue<PropertyManager_sptr>(const std::string &name) const {
139 if (prop) {
140 return *prop;
141 } else {
142 throw std::runtime_error(std::string("Attempt to assign property ") + name +
143 " to incorrect type. Expected shared_ptr<PropertyManager>.");
144 }
145}
146
147template <>
149IPropertyManager::getValue<PropertyManager_const_sptr>(const std::string &name) const {
150 auto const *prop = dynamic_cast<PropertyWithValue<PropertyManager_sptr> *>(getPointerToProperty(name));
151 if (prop) {
152 return prop->operator()();
153 } else {
154 std::string message =
155 "Attempt to assign property " + name + " to incorrect type. Expected const shared_ptr<PropertyManager>.";
156 throw std::runtime_error(message);
157 }
158}
159
160} // namespace Mantid::Kernel
std::string name
Definition Run.cpp:60
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:63
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)