Mantid
Loading...
Searching...
No Matches
FunctionProperty.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 +
10
11#include <json/value.h>
12
13namespace Mantid::API {
20FunctionProperty::FunctionProperty(const std::string &name, const unsigned int direction,
21 const PropertyMode::Type optional)
22 : Kernel::PropertyWithValue<std::shared_ptr<IFunction>>(name, std::shared_ptr<IFunction>(),
23 std::make_shared<Kernel::NullValidator>(), direction),
24 m_optional(optional) {}
25
31FunctionProperty &FunctionProperty::operator=(const std::shared_ptr<IFunction> &value) {
33 return *this;
34}
35
36//--------------------------------------------------------------------------------------
39 throw Kernel::Exception::NotImplementedError("+= operator is not implemented for FunctionProperty.");
40}
41
45std::string FunctionProperty::value() const {
46 if (m_value)
47 return m_value->asString();
48 else
49 return getDefault();
50}
51
56Json::Value FunctionProperty::valueAsJson() const { return Json::Value(value()); }
57
61std::string FunctionProperty::getDefault() const { return ""; }
62
67
73std::string FunctionProperty::setValue(const std::string &value) {
74 std::string error;
75
76 if (isOptional() && value.empty()) {
77 // No error message when the function string is empty and the function is optional
78 m_value = std::shared_ptr<IFunction>();
80 return error;
81 }
82
83 try {
84 m_value = std::shared_ptr<IFunction>(FunctionFactory::Instance().createInitialized(value));
86 } catch (std::exception &e) {
87 error = e.what();
88 }
89 return error;
90}
91
98std::string FunctionProperty::setValueFromJson(const Json::Value &value) {
99 try {
100 return setValue(value.asString());
101 } catch (std::exception &exc) {
102 return exc.what();
103 }
104}
105
110std::string FunctionProperty::isValid() const {
112 return "";
113 } else {
114 return isDefault() ? "Function is empty." : "";
115 }
116}
117
121bool FunctionProperty::isDefault() const { return m_value == std::shared_ptr<IFunction>(); }
122
126
127} // namespace Mantid::API
double value
The value of the point.
Definition: FitMW.cpp:51
double error
Definition: IndexPeaks.cpp:133
A property class for functions.
std::string setValue(const std::string &value) override
Set the value of the property.
std::string m_definition
The function definition string (as used by the FunctionFactory)
std::string value() const override
Get the function definition string.
Json::Value valueAsJson() const override
Return a Json::Value object encoding the function string.
PropertyMode::Type m_optional
A flag indicating whether the property should be considered optional.
std::string isValid() const override
Checks whether the entered function is valid.
FunctionProperty & operator+=(Kernel::Property const *) override
Add the value of another property.
bool isDefault() const override
Is default.
std::string setValueFromJson(const Json::Value &value) override
Set the value of the property as a Json representation.
FunctionProperty(const std::string &name, const unsigned int direction=Kernel::Direction::Input, const PropertyMode::Type optional=PropertyMode::Type::Mandatory)
Constructor.
std::string getDefault() const override
Get the value the property was initialised with -its default value.
FunctionProperty & operator=(const FunctionProperty &right)=default
Copy assignment operator.
const Kernel::PropertyHistory createHistory() const override
Create a history record.
bool isOptional() const
Return true if the property is optional.
This is an interface to a fitting function - a semi-abstarct class.
Definition: IFunction.h:163
Marks code as not implemented yet.
Definition: Exception.h:138
NullValidator is a validator that doesn't.
Definition: NullValidator.h:20
This class stores information about the parameters used by an algorithm.
The concrete, templated class for properties.
std::shared_ptr< IFunction > m_value
The value of the property.
Base class for properties.
Definition: Property.h:94
unsigned int direction() const
returns the direction of the property
Definition: Property.h:172
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
STL namespace.
@ Output
An output workspace.
Definition: Property.h:54