18#include <boost/algorithm/string/trim.hpp>
24#include "MantidNexus/NexusFile.h"
25#include <json/value.h>
43template <
typename TYPE>
45 unsigned int direction)
55template <
typename TYPE>
74template <
typename TYPE>
77 unsigned int direction)
87template <
typename TYPE>
102 throw std::invalid_argument(
"PropertyWithValue::saveProperty - Cannot save '" + this->
name() +
"', property type " +
103 typeid(
TYPE).
name() +
" not implemented.");
114template <
typename TYPE>
119 }
catch (boost::bad_lexical_cast &) {
149 return !(*
this ==
rhs);
171 std::string valueCopy =
value;
173 boost::trim(valueCopy);
180 }
catch (boost::bad_lexical_cast &) {
181 std::string
error =
"Could not set property " +
name() +
". Can not convert \"" +
value +
"\" to " + type();
182 g_logger.debug() <<
error;
184 }
catch (std::invalid_argument &except) {
185 g_logger.debug() <<
"Could not set property " <<
name() <<
": " << except.what();
186 return except.what();
197 if (
value.type() != Json::stringValue) {
199 *
this = decode<TYPE>(
value);
200 }
catch (std::invalid_argument &exc) {
205 return setValue(
value.asString());
219 return setTypedValue(data, std::is_convertible<
TYPE, std::shared_ptr<DataItem>>());
247 g_logger.warning() <<
"PropertyWithValue " << this->
name()
248 <<
" could not be added to another property of the "
249 "same name but incompatible type.\n";
263 if constexpr (std::is_same<TYPE, std::string>::value) {
266 boost::trim(valueCopy);
272 std::string problem = this->isValid();
273 if (problem.empty()) {
275 }
else if (problem ==
"_alias") {
280 throw std::invalid_argument(
"When setting value of property \"" + this->
name() +
"\": " + problem);
352 return setValue(
right.value());
363template <
typename TYPE>
366 TYPE data = std::dynamic_pointer_cast<typename TYPE::element_type>(
value);
371 }
catch (std::invalid_argument &exc) {
375 msg =
"Invalid DataItem. The object type (" + std::string(
typeid(
value).
name()) +
376 ") does not match the declared type of the property (" + std::string(this->type()) +
").";
388template <
typename TYPE>
392 return "Attempt to assign object of type DataItem to property (" +
name() +
") of incorrect type";
401 std::string strAlias =
toString(alias);
402 std::string strValue =
m_validator->getValueForAlias(strAlias);
const std::string & m_value
Kernel::IValidator_sptr m_validator
const std::vector< double > & rhs
double value
The value of the point.
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
NullValidator is a validator that doesn't.
The concrete, templated class for properties.
virtual void replaceValidator(IValidator_sptr newValidator)
Replace the current validator with the given one.
bool isDefault() const override
Indicates if the property's value is the same as it was when it was set N.B.
int size() const override
Get the size of the property.
PropertyWithValue & operator=(const PropertyWithValue &right)
Copy assignment operator assigns only the value and the validator not the name, default (initial) val...
std::string setTypedValue(const U &value, const std::true_type &)
Helper function for setValue(DataItem_sptr).
std::string setValue(const std::string &value) override
Set the value of the property from a string representation.
PropertyWithValue & operator+=(Property const *right) override
Add the value of another property.
std::string valueAsPrettyStr(const size_t maxLength=0, const bool collapseLists=true) const override
Get the value of the property as a string.
Json::Value valueAsJson() const override
Attempt to construct a Json::Value object from the plain value.
bool operator==(const PropertyWithValue< TYPE > &rhs) const
Deep comparison.
bool isMultipleSelectionAllowed() override
Returns the set of valid values for this property, if such a set exists.
void saveProperty(Nexus::File *file) override
PropertyWithValue< TYPE > * clone() const override
'Virtual copy constructor'
PropertyWithValue()=delete
const TYPE getValueForAlias(const TYPE &alias) const
Return value for a given alias.
virtual const TYPE & operator()() const
Allows you to get the value of the property via an expression like myProperty()
std::string setValueFromJson(const Json::Value &value) override
Set the value of the property from a Json representation.
IValidator_sptr getValidator() const
Returns the validator as a constant variable so it cannot be changed.
std::vector< std::string > allowedValues() const override
Returns the set of valid values for this property, if such a set exists.
TYPE m_value
The value of the property.
std::string value() const override
Get the value of the property as a string.
std::string isValid() const override
Check the value chosen for the property is OK, unless overidden it just calls the validator's isValid...
bool operator!=(const PropertyWithValue< TYPE > &rhs) const
Deep comparison (not equal).
std::string setValueFromProperty(const Property &right) override
Set the value of the property via a reference to another property.
std::string setDataItem(const std::shared_ptr< DataItem > &data) override
Set a property value via a DataItem.
std::string getDefault() const override
Get the value the property was initialised with -its default value.
Base class for properties.
const std::string & name() const
Get the property's name.
MANTID_KERNEL_DLL std::string shorten(const std::string &input, const size_t max_length)
Converts long strings into "start ... end".
T extractToValueVector(const std::string &strvalue)
void toValue(const std::string &strvalue, T &value)
std::string toString(const T &value)
Convert values to strings.
std::vector< std::string > determineAllowedValues(const T &, const IValidator &validator)
void addingOperator(T &lhs, const T &rhs)
int findSize(const T &)
Specialization for any type, should be appropriate for properties with a single value.
MANTID_KERNEL_DLL::Json::Value encodeAsJson(const OptionalBool &)
Encode an OptionalBool as a Json::Value.
std::shared_ptr< IValidator > IValidator_sptr
A shared_ptr to an IValidator.
std::string toPrettyString(const T &value, size_t maxLength=0, bool collapseLists=true)
Convert values to pretty strings.
Helper class which provides the Collimation Length for SANS instruments.