/
PropertyWithValue.h
585 lines (509 loc) · 17.9 KB
/
PropertyWithValue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#ifndef MANTID_KERNEL_PROPERTYWITHVALUE_H_
#define MANTID_KERNEL_PROPERTYWITHVALUE_H_
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidKernel/Property.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/NullValidator.h"
#ifndef Q_MOC_RUN
# include <boost/lexical_cast.hpp>
# include <boost/shared_ptr.hpp>
#endif
#include <Poco/StringTokenizer.h>
#include <vector>
#include "MantidKernel/IPropertySettings.h"
namespace Mantid
{
namespace Kernel
{
/** The concrete, templated class for properties.
The supported types at present are int, double, bool & std::string.
With reference to the Gaudi structure, this class can be seen as the equivalent of both the
Gaudi class of the same name and its sub-classses.
@class Mantid::Kernel::PropertyWithValue
@author Russell Taylor, Tessella Support Services plc
@author Based on the Gaudi class of the same name (see http://proj-gaudi.web.cern.ch/proj-gaudi/)
@date 14/11/2007
Copyright © 2007-2010 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
// --------------------- convert values to strings
namespace {
/// Convert values to strings.
template <typename T>
std::string toString(const T& value)
{
return boost::lexical_cast<std::string>(value);
}
/// Throw an exception if a shared pointer is converted to a string.
template <typename T>
std::string toString(const boost::shared_ptr<T>& value)
{
UNUSED_ARG(value);
throw boost::bad_lexical_cast();
}
/// Specialisation for a property of type std::vector.
template <typename T>
std::string toString(const std::vector<T>& value, const std::string & delimiter = ",")
{
std::stringstream result;
std::size_t vsize = value.size();
for (std::size_t i = 0; i < vsize; ++i)
{
result << value[i];
if (i + 1 != vsize)
result << delimiter;
}
return result.str();
}
/// Specialisation for a property of type std::vector<std::vector>.
template <typename T>
std::string toString(const std::vector<std::vector<T> >& value, const std::string & outerDelimiter = ",",
const std::string & innerDelimiter = "+")
{
std::stringstream result;
std::size_t vsize = value.size();
for (std::size_t i = 0; i < vsize; ++i)
{
std::size_t innervsize = value[i].size();
for (std::size_t j = 0; j < innervsize; ++j)
{
result << value[i][j];
if (j + 1 != innervsize)
result << innerDelimiter;
}
if (i + 1 != vsize)
result << outerDelimiter;
}
return result.str();
}
/// Specialisation for any type, should be appropriate for properties with a single value.
template <typename T>
int findSize(const T&)
{
return 1;
}
/// Specialisation for properties that are of type vector.
template <typename T>
int findSize(const std::vector<T>& value)
{
return static_cast<int>(value.size());
}
// ------------- Convert strings to values
template <typename T>
inline void appendValue(const std::string& strvalue, std::vector<T>& value)
{
// try to split the string
std::size_t pos = strvalue.find(':');
if (pos == std::string::npos)
{
pos = strvalue.find('-', 1);
}
// just convert the whole thing into a value
if (pos == std::string::npos){
value.push_back(boost::lexical_cast<T>(strvalue));
return;
}
// convert the input string into boundaries and run through a list
T start = boost::lexical_cast<T>(strvalue.substr(0, pos));
T stop = boost::lexical_cast<T>(strvalue.substr(pos + 1));
for (T i = start; i <= stop; i++)
value.push_back(i);
}
template <typename T>
void toValue(const std::string& strvalue, T& value)
{
value = boost::lexical_cast<T>( strvalue );
}
template <typename T>
void toValue(const std::string&, boost::shared_ptr<T>&)
{
throw boost::bad_lexical_cast();
}
template <typename T>
void toValue(const std::string& strvalue, std::vector<T>& value)
{
// Split up comma-separated properties
typedef Poco::StringTokenizer tokenizer;
tokenizer values(strvalue, ",", tokenizer::TOK_IGNORE_EMPTY | tokenizer::TOK_TRIM);
value.clear();
value.reserve(values.count());
for (tokenizer::Iterator it = values.begin(); it != values.end(); ++it)
{
value.push_back(boost::lexical_cast<T>(*it));
}
}
template <typename T>
void toValue(const std::string& strvalue, std::vector<std::vector<T> >& value, const std::string & outerDelimiter = ",",
const std::string & innerDelimiter = "+")
{
typedef Poco::StringTokenizer tokenizer;
tokenizer tokens(strvalue, outerDelimiter, tokenizer::TOK_IGNORE_EMPTY | tokenizer::TOK_TRIM);
value.clear();
value.reserve(tokens.count());
for (tokenizer::Iterator oIt = tokens.begin(); oIt != tokens.end(); ++oIt)
{
tokenizer values(*oIt, innerDelimiter, tokenizer::TOK_IGNORE_EMPTY | tokenizer::TOK_TRIM);
std::vector<T> vect;
for (tokenizer::Iterator iIt = values.begin(); iIt != values.end(); ++iIt)
vect.push_back(boost::lexical_cast<T>(*iIt));
value.push_back(vect);
}
}
/// Macro for the vector<int> specializations
#define PROPERTYWITHVALUE_TOVALUE(type) \
template <> \
inline void toValue<type>(const std::string& strvalue, std::vector<type>& value) \
{ \
typedef Poco::StringTokenizer tokenizer; \
tokenizer values(strvalue, ",", tokenizer::TOK_IGNORE_EMPTY | tokenizer::TOK_TRIM); \
value.clear(); \
value.reserve(values.count()); \
for (tokenizer::Iterator it = values.begin(); it != values.end(); ++it) \
{ \
appendValue(*it, value); \
} \
}
PROPERTYWITHVALUE_TOVALUE(int);
PROPERTYWITHVALUE_TOVALUE(long);
PROPERTYWITHVALUE_TOVALUE(uint32_t);
PROPERTYWITHVALUE_TOVALUE(uint64_t);
#ifdef __INTEL_COMPILER
PROPERTYWITHVALUE_TOVALUE(unsigned long);
#endif
// Clear up the namespace
#undef PROPERTYWITHVALUE_TOVALUE
//------------------------------------------------------------------------------------------------
// Templated += operator functions for specific types
template<typename T>
inline void addingOperator(T& lhs, const T& rhs)
{
// The cast here (and the expansion of the compound operator which that
// necessitates) is because if this function is created for a template
// type narrower than an int, the compiler will expande the operands to
// ints which leads to a compiler warning when it's assigned back to the
// narrower type.
lhs = static_cast<T>(lhs + rhs);
}
template<typename T>
inline void addingOperator(std::vector<T>& lhs, const std::vector<T>& rhs)
{
//This concatenates the two
if ( &lhs != &rhs)
{
lhs.insert(lhs.end(), rhs.begin(), rhs.end());
}
else
{
std::vector<T> rhs_copy(rhs);
lhs.insert(lhs.end(), rhs_copy.begin(), rhs_copy.end());
}
}
template<>
inline void addingOperator(bool&, const bool&)
{
throw Exception::NotImplementedError("PropertyWithValue.h: += operator not implemented for type bool");
}
template<typename T>
inline void addingOperator(boost::shared_ptr<T>& lhs, const boost::shared_ptr<T>& rhs)
{
UNUSED_ARG(lhs);
UNUSED_ARG(rhs);
throw Exception::NotImplementedError("PropertyWithValue.h: += operator not implemented for boost::shared_ptr");
}
}
//------------------------------------------------------------------------------------------------
// Now the PropertyWithValue class itself
//------------------------------------------------------------------------------------------------
template <typename TYPE>
class DLLExport PropertyWithValue : public Property
{
public:
/** Constructor
* @param name :: The name to assign to the property
* @param defaultValue :: Is stored initial default value of the property
* @param validator :: The validator to use for this property
* @param direction :: Whether this is a Direction::Input, Direction::Output or Direction::InOut (Input & Output) property
*/
PropertyWithValue( const std::string &name, const TYPE& defaultValue,
IValidator_sptr validator = IValidator_sptr(new NullValidator),
const unsigned int direction = Direction::Input) :
Property( name, typeid( TYPE ), direction ),
m_value( defaultValue ),
m_initialValue( defaultValue ),
m_validator( validator )
{
}
/** Constructor
* @param name :: The name to assign to the property
* @param defaultValue :: Is stored initial default value of the property
* @param direction :: Whether this is a Direction::Input, Direction::Output or Direction::InOut (Input & Output) property
*/
PropertyWithValue( const std::string &name, const TYPE& defaultValue, const unsigned int direction) :
Property( name, typeid( TYPE ), direction ),
m_value( defaultValue ),
m_initialValue( defaultValue ),
m_validator( boost::make_shared<NullValidator>())
{
}
/**Copy constructor
* Note the default value of the copied object is the initial value of original
*/
PropertyWithValue( const PropertyWithValue& right ) :
Property( right ),
m_value( right.m_value ),
m_initialValue( right.m_initialValue ), //the default is the initial value of the original object
m_validator( right.m_validator->clone() )
{
}
/// 'Virtual copy constructor'
PropertyWithValue<TYPE>* clone() const { return new PropertyWithValue<TYPE>(*this); }
/// Virtual destructor
virtual ~PropertyWithValue()
{
}
/** Get the value of the property as a string
* @return The property's value
*/
virtual std::string value() const
{
return toString(m_value);
}
/** Get the size of the property.
*/
virtual int size() const
{
return findSize(m_value);
}
/** Get the value the property was initialised with -its default value
* @return The default value
*/
virtual std::string getDefault() const
{
return toString(m_initialValue);
}
/** Set the value of the property from a string representation.
* Note that "1" & "0" must be used for bool properties rather than true/false.
* @param value :: The value to assign to the property
* @return Returns "" if the assignment was successful or a user level description of the problem
*/
virtual std::string setValue(const std::string& value )
{
try
{
TYPE result = m_value;
toValue(value, result);
//Uses the assignment operator defined below which runs isValid() and throws based on the result
*this = result;
return "";
}
catch ( boost::bad_lexical_cast&)
{
std::string error = "Could not set property " + name() +
". Can not convert \"" + value + "\" to " + type();
g_logger.debug() << error;
return error;
}
catch ( std::invalid_argument& except)
{
g_logger.debug() << "Could not set property " << name() << ": " << except.what();
return except.what();
}
return "";
}
/**
* Set a property value via a DataItem
* @param data :: A shared pointer to a data item
* @return "" if the assignment was successful or a user level description of the problem
*/
virtual std::string setDataItem(const boost::shared_ptr<DataItem> data)
{
// Pass of the helper function that is able to distinguish whether
// the TYPE of the PropertyWithValue can be converted to a shared_ptr<DataItem>
return setTypedValue(data, boost::is_convertible<TYPE, boost::shared_ptr<DataItem> >());
}
///Copy assignment operator assigns only the value and the validator not the name, default (initial) value, etc.
PropertyWithValue& operator=( const PropertyWithValue& right )
{
if ( &right == this ) return *this;
m_value = right.m_value;
m_validator = right.m_validator->clone();
return *this;
}
//--------------------------------------------------------------------------------------
/** Add the value of another property
* @param right the property to add
* @return the sum
*/
virtual PropertyWithValue& operator+=( Property const * right )
{
PropertyWithValue const * rhs = dynamic_cast< PropertyWithValue const * >(right);
if (rhs)
{
//This function basically does:
// m_value += rhs->m_value; for values
// or concatenates vectors for vectors
addingOperator(m_value, rhs->m_value);
}
else
g_logger.warning() << "PropertyWithValue " << this->name()
<< " could not be added to another property of the same name but incompatible type.\n";
return *this;
}
//--------------------------------------------------------------------------------------
/** Assignment operator.
* Allows assignment of a new value to the property by writing,
* e.g., myProperty = 3;
* @param value :: The new value to assign to the property
* @return the reference to itself
*/
virtual TYPE& operator=( const TYPE& value )
{
TYPE oldValue = m_value;
m_value = value;
std::string problem = this->isValid();
if ( problem == "" )
{
return m_value;
}
else
{
m_value = oldValue;
throw std::invalid_argument(problem);
}
}
/** Allows you to get the value of the property via an expression like myProperty()
* @returns the value of the property
*/
virtual const TYPE& operator() () const
{
return m_value;
}
/** Allows you to get the value of the property simply by typing its name.
* Means you can use an expression like: int i = myProperty;
* @return the value
*/
virtual operator const TYPE& () const
{
return m_value;
}
/** Check the value chosen for the property is OK, unless overidden it just calls the validator's isValid()
* N.B. Problems found in validator are written to the log
* if you override this function to do checking outside a validator may want to do more logging
* @returns "" if the value is valid or a discription of the problem
*/
virtual std::string isValid() const
{
return m_validator->isValid(m_value);
}
/** Indicates if the property's value is the same as it was when it was set
* N.B. Uses an unsafe comparison in the case of doubles, consider overriding if the value is a pointer or floating point type
* @return true if the value is the same as the initial value or false otherwise
*/
virtual bool isDefault() const
{
return m_initialValue == m_value;
}
/** Returns the set of valid values for this property, if such a set exists.
* If not, it returns an empty vector.
* @return Returns the set of valid values for this property, or it returns an empty vector.
*/
virtual std::vector<std::string> allowedValues() const
{
return m_validator->allowedValues();
}
/**
* Replace the current validator with the given one
* @param newValidator :: A replacement validator
*/
virtual void replaceValidator(IValidator_sptr newValidator)
{
m_validator = newValidator;
}
protected:
/// The value of the property
TYPE m_value;
///the property's default value which is also its initial value
const TYPE m_initialValue;
private:
/**
* Set the value of the property via a reference to another property.
* If the value is unacceptable the value is not changed but a string is returned.
* The value is only accepted if the other property has the same type as this
* @param right :: A reference to a property.
*/
virtual std::string setValueFromProperty( const Property& right )
{
auto prop = dynamic_cast<const PropertyWithValue<TYPE>*>(&right);
if ( !prop )
{
return "Could not set value: properties have different type.";
}
m_value = prop->m_value;
return "";
}
/**
* Helper function for setValue(DataItem_sptr). Uses boost type traits to ensure
* it is only used if U is a type that is convertible to boost::shared_ptr<DataItem>
* @param value :: A object of type convertible to boost::shared_ptr<DataItem>
*/
template<typename U>
std::string setTypedValue(const U & value, const boost::true_type &)
{
TYPE data = boost::dynamic_pointer_cast<typename TYPE::element_type>(value);
std::string msg;
if ( data )
{
try
{
(*this) = data;
}
catch(std::invalid_argument& exc)
{
msg = exc.what();
}
}
else
{
msg = "Invalid DataItem. The object type (" + std::string(typeid(value).name())
+ ") does not match the declared type of the property (" + std::string(this->type()) + ").";
}
return msg;
}
/**
* Helper function for setValue(DataItem_sptr). Uses boost type traits to ensure
* it is only used if U is NOT a type that is convertible to boost::shared_ptr<DataItem>
* @param value :: A object of type convertible to boost::shared_ptr<DataItem>
*/
template<typename U>
std::string setTypedValue(const U & value, const boost::false_type &)
{
UNUSED_ARG(value);
return "Attempt to assign object of type DataItem to property (" + name() + ") of incorrect type";
}
/// Visitor validator class
IValidator_sptr m_validator;
/// Static reference to the logger class
static Logger g_logger;
/// Private default constructor
PropertyWithValue();
};
template <typename TYPE>
Logger PropertyWithValue<TYPE>::g_logger("PropertyWithValue");
} // namespace Kernel
} // namespace Mantid
#endif /*MANTID_KERNEL_PROPERTYWITHVALUE_H_*/