Mantid
Loading...
Searching...
No Matches
EnumeratedStringProperty.hxx
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 +
7#pragma once
8
11#include <json/value.h>
12
13namespace Mantid::Kernel {
14
15//------------------------------------------------------------------------------------------------
16// Now the EnumeratedStringProperty class itself
17//------------------------------------------------------------------------------------------------
18
19// ######################################################//
20// CONSTRUCTORS
21// ######################################################//
22
29template <class E, std::vector<std::string> const *const names>
31 Direction::Type const direction)
32 : Property(name, typeid(ENUMSTRING), direction), m_value(defaultValue), m_initialValue(std::move(defaultValue)) {}
33
37template <class E, std::vector<std::string> const *const names>
39 : Property(right), m_value(right.m_value), m_initialValue(right.m_initialValue) {
40} // the default is the initial value of the original object
41
44template <class E, std::vector<std::string> const *const names>
48
57template <class E, std::vector<std::string> const *const names>
60 if (&right == this)
61 return *this;
62 this->m_value = right.m_value;
63 return *this;
64}
65
66// ######################################################//
67// GETTERS
68// ######################################################//
69
73template <class E, std::vector<std::string> const *const names>
75 return static_cast<std::string>(m_value);
76}
77
81template <class E, std::vector<std::string> const *const names>
82std::string EnumeratedStringProperty<E, names>::valueAsPrettyStr(std::size_t const maxLength,
83 bool const collapseLists) const {
84 return toPrettyString(static_cast<std::string>(m_value), maxLength, collapseLists);
85}
86
91template <class E, std::vector<std::string> const *const names>
93 return encodeAsJson((*this)());
94}
95
101template <class E, std::vector<std::string> const *const names>
103 if (this->name() != rhs.name())
104 return false;
105 return (static_cast<E>(this->m_value) == static_cast<E>(rhs.m_value));
106}
107
113template <class E, std::vector<std::string> const *const names>
115 return !(*this == rhs);
116}
117
120template <class E, std::vector<std::string> const *const names> int EnumeratedStringProperty<E, names>::size() const {
121 return 1;
122}
123
127template <class E, std::vector<std::string> const *const names>
129 return static_cast<std::string>(m_initialValue);
130}
131
136template <class E, std::vector<std::string> const *const names>
140
144template <class E, std::vector<std::string> const *const names>
146 if (m_value.size() != 0)
147 return "";
148 else
149 return "EnumeratedStringProperty was not set with valid EnumeratedString.\n";
150}
151
158template <class E, std::vector<std::string> const *const names>
160 return m_initialValue == m_value;
161}
162
168template <class E, std::vector<std::string> const *const names>
170 return *names;
171}
172
173// ######################################################//
174// SETTERS
175// ######################################################//
176
182template <class E, std::vector<std::string> const *const names>
184 this->m_value = static_cast<EnumeratedString<E, names>>(value);
185 return "";
186}
187
193template <class E, std::vector<std::string> const *const names>
195 try {
196 this->m_value = static_cast<EnumeratedString<E, names>>(value);
197 } catch (std::runtime_error &exc) {
198 const std::string msg = exc.what();
199 if (msg.empty())
200 return "value \"" + value + "\" not in allowed list";
201 else
202 return msg;
203 }
204 return ""; // everything was fine
205}
206
212template <class E, std::vector<std::string> const *const names>
217
224template <class E, std::vector<std::string> const *const names>
226 if (value.type() != Json::stringValue) {
227 try {
228 *this = decode<E>(value);
229 } catch (std::invalid_argument &exc) {
230 return exc.what();
231 }
232 return "";
233 } else {
234 return setValue(value.asString());
235 }
236}
237
244template <class E, std::vector<std::string> const *const names>
245std::string EnumeratedStringProperty<E, names>::setDataItem(std::shared_ptr<DataItem> const &data) {
246 // Pass of the helper function that is able to distinguish whether
247 // the TYPE of the PropertyWithValue can be converted to a
248 // shared_ptr<DataItem>
249 return setTypedValue(data, std::is_convertible<EnumeratedString<E, names>, std::shared_ptr<DataItem>>());
250}
251
252//--------------------------------------------------------------------------------------
259template <class E, std::vector<std::string> const *const names>
264
265//--------------------------------------------------------------------------------------
272template <class E, std::vector<std::string> const *const names>
274 this->m_value = static_cast<EnumeratedString<E, names>>(value);
275 return *this;
276}
277
278//--------------------------------------------------------------------------------------
285template <class E, std::vector<std::string> const *const names>
291
292// ######################################################//
293// MUTATORS AND SUNDRY
294// ######################################################//
295
296//--------------------------------------------------------------------------------------
301template <class E, std::vector<std::string> const *const names>
303 std::ostringstream oss;
304 oss << "Cannot add EnumeratedStringProperty " << std::hex << std::showbase << reinterpret_cast<uintptr_t>(&right)
305 << ", addition not implemented." << std::endl;
306 throw std::invalid_argument(oss.str());
307}
308
309template <class E, std::vector<std::string> const *const names>
311 // AppleClang 7.3 and later gives a -Winfinite-recursion warning if I call the
312 // base class method. The function is small enough that reimplementing it
313 // isn't a big deal.
314 throw std::invalid_argument("PropertyWithValue::saveProperty - Cannot save '" + this->name() + "', property type " +
315 typeid(ENUMSTRING).name() + " not implemented.");
316}
317
318// ######################################################//
319// PRIVATE METHODS
320// ######################################################//
321
329template <class E, std::vector<std::string> const *const names>
331
332 if (auto prop = dynamic_cast<EnumeratedStringProperty<E, names> const *>(&right)) {
333 this->m_value = prop->m_value;
334 return "";
335 } else {
336 return setValue(right.value());
337 }
338}
339
347template <class E, std::vector<std::string> const *const names>
348template <typename U>
349std::string EnumeratedStringProperty<E, names>::setTypedValue(U const &value, std::true_type const &) {
350 std::string msg;
351 try {
353 } catch (std::runtime_error &exc) {
354 msg = exc.what();
355 }
356 return msg;
357}
358
366template <class E, std::vector<std::string> const *const names>
367template <typename U>
368std::string EnumeratedStringProperty<E, names>::setTypedValue(U const &, std::false_type const &) {
369 return "Attempt to assign object of type DataItem to property (" + name() + ") of incorrect type";
370}
371
372} // namespace Mantid::Kernel
const std::string & m_value
Definition Algorithm.cpp:71
std::string name
Definition Run.cpp:60
const std::vector< double > & rhs
double value
The value of the point.
Definition FitMW.cpp:51
double right
A concrete property based on user options of a finite list of strings.
std::string isValid() const override
If the value has been set, then it is valid.
std::string setDataItem(const std::shared_ptr< DataItem > &data) override
Set a property value via a DataItem.
int size() const override
Get the size of the property.
std::string setTypedValue(U const &value, std::true_type const &)
Helper function for setValue(DataItem_sptr).
std::string getDefault() const override
Get the value the property was initialised with -its default value.
std::string valueAsPrettyStr(size_t const maxLength=0, bool const collapseLists=true) const override
Get the value of the property as a more prettier string.
Json::Value valueAsJson() const override
Attempt to construct a Json::Value object from the plain value.
EnumeratedStringProperty * clone() const override
Virtual copy constructor.
ENUMSTRING operator()() const
Allows you to get the value of the property simply by typing its name.
bool operator==(EnumeratedStringProperty const &rhs) const
Deep comparison.
EnumeratedStringProperty & operator=(EnumeratedStringProperty const &right)
Assignment operator.
EnumeratedStringProperty(std::string const &name, ENUMSTRING const &defaultValue=static_cast< E >(0), Direction::Type const direction=Direction::Input)
Constructor.
ENUMSTRING m_value
The value of the property.
std::vector< std::string > allowedValues() const override
Returns the set of valid values for this property, if such a set exists.
std::string value() const override
Get the value of the property as a string.
bool isDefault() const override
Indicates if the property's value is the same as it was when it was set N.B.
std::string setValueFromProperty(Property const &right) override
Set the value of the property via a reference to another property.
bool operator!=(EnumeratedStringProperty const &rhs) const
Deep comparison (not equal).
std::string setValue(E const value)
Set the value of the property from a string representation.
EnumeratedStringProperty & operator+=(Property const *right) override
Add the value of another property.
std::string setValueFromJson(const Json::Value &value) override
Set the value of the property from a Json representation.
Base class for properties.
Definition Property.h:94
const std::string & name() const
Get the property's name.
Definition Property.cpp:61
MANTID_KERNEL_DLL::Json::Value encodeAsJson(const OptionalBool &)
Encode an OptionalBool as a Json::Value.
std::string toPrettyString(const T &value, size_t maxLength=0, bool collapseLists=true)
Convert values to pretty strings.
STL namespace.
Type
Enum giving the possible directions.
Definition Property.h:52