18#include <boost/algorithm/string/trim.hpp>
24#include "MantidNexus/NexusFile.h"
25#include <json/value.h>
43template <
typename TYPE>
45 unsigned int direction)
47 m_initialValue(
std::move(defaultValue)),
56template <
typename TYPE>
75template <
typename TYPE>
78 unsigned int direction)
88template <
typename TYPE>
100 if constexpr (std::is_same_v<TYPE, float> || std::is_same_v<TYPE, double> || std::is_same_v<TYPE, int32_t> ||
101 std::is_same_v<TYPE, uint32_t> || std::is_same_v<TYPE, int64_t> || std::is_same_v<TYPE, uint64_t> ||
102 std::is_same_v<TYPE, long> || std::is_same_v<TYPE, std::string> ||
103 std::is_same_v<TYPE, std::vector<double>> || std::is_same_v<TYPE, std::vector<int32_t>>) {
104 file->makeGroup(this->
name(),
"NXlog",
true);
105 file->writeData(
"value",
m_value);
106 file->openData(
"value");
107 file->putAttr(
"units", this->units());
114 throw std::invalid_argument(
"PropertyWithValue::saveProperty - Cannot save '" + this->
name() +
"', property type " +
115 typeid(
TYPE).
name() +
" not implemented.");
127template <
typename TYPE>
132 }
catch (boost::bad_lexical_cast &) {
162 return !(*
this ==
rhs);
184 std::string valueCopy =
value;
186 boost::trim(valueCopy);
193 }
catch (boost::bad_lexical_cast &) {
194 std::string
error =
"Could not set property " +
name() +
". Can not convert \"" +
value +
"\" to " + type();
195 g_logger.debug() <<
error;
197 }
catch (std::invalid_argument &except) {
198 g_logger.debug() <<
"Could not set property " <<
name() <<
": " << except.what();
199 return except.what();
210 if (
value.type() != Json::stringValue) {
212 *
this = decode<TYPE>(
value);
213 }
catch (std::invalid_argument &exc) {
218 return setValue(
value.asString());
232 return setTypedValue(data, std::is_convertible<
TYPE, std::shared_ptr<DataItem>>());
260 g_logger.warning() <<
"PropertyWithValue " << this->
name()
261 <<
" could not be added to another property of the "
262 "same name but incompatible type.\n";
276 if constexpr (std::is_same<TYPE, std::string>::value) {
279 boost::trim(valueCopy);
285 std::string problem = this->isValid();
286 if (problem.empty()) {
288 }
else if (problem ==
"_alias") {
293 throw std::invalid_argument(
"When setting value of property \"" + this->
name() +
"\": " + problem);
365 return setValue(
right.value());
376template <
typename TYPE>
379 TYPE data = std::dynamic_pointer_cast<typename TYPE::element_type>(
value);
384 }
catch (std::invalid_argument &exc) {
388 msg =
"Invalid DataItem. The object type (" + std::string(
typeid(
value).
name()) +
389 ") does not match the declared type of the property (" + std::string(this->type()) +
").";
401template <
typename TYPE>
405 return "Attempt to assign object of type DataItem to property (" +
name() +
") of incorrect type";
414 std::string strAlias =
toString(alias);
415 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.