Newer
Older
#ifndef MANTID_KERNEL_PROPERTYHELPER_H_
#define MANTID_KERNEL_PROPERTYHELPER_H_
#ifndef Q_MOC_RUN
#include <boost/lexical_cast.hpp>
#include <boost/make_shared.hpp>
#endif
#include "MantidKernel/OptionalBool.h"
#include "MantidKernel/StringTokenizer.h"
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
namespace Mantid {
namespace Kernel {
// --------------------- 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();
}
namespace detail {
// vector<int> specializations
template <typename T>
void toValue(const std::string &strvalue, std::vector<T> &value,
std::true_type) {
typedef Mantid::Kernel::StringTokenizer tokenizer;
tokenizer values(strvalue, ",",
tokenizer::TOK_IGNORE_EMPTY | tokenizer::TOK_TRIM);
value.clear();
value.reserve(values.count());
for (const auto &token : values) {
appendValue(token, value);
}
}
template <typename T>
void toValue(const std::string &strvalue, std::vector<T> &value,
std::false_type) {
// Split up comma-separated properties
typedef Mantid::Kernel::StringTokenizer tokenizer;
tokenizer values(strvalue, ",",
tokenizer::TOK_IGNORE_EMPTY | tokenizer::TOK_TRIM);
value.clear();
value.reserve(values.count());
std::transform(
values.cbegin(), values.cend(), std::back_inserter(value),
[](const std::string &str) { return boost::lexical_cast<T>(str); });
}
// bool and char don't make sense as types to generate a range of values.
// This is similar to std::is_integral<T>, but bool and char are std::false_type
template <class T> struct is_range_type : public std::false_type {};
template <class T> struct is_range_type<const T> : public is_range_type<T> {};
template <class T>
struct is_range_type<volatile const T> : public is_range_type<T> {};
template <class T>
struct is_range_type<volatile T> : public is_range_type<T> {};
template <> struct is_range_type<uint16_t> : public std::true_type {};
template <> struct is_range_type<uint32_t> : public std::true_type {};
template <> struct is_range_type<uint64_t> : public std::true_type {};
template <> struct is_range_type<int16_t> : public std::true_type {};
template <> struct is_range_type<int32_t> : public std::true_type {};
template <> struct is_range_type<int64_t> : public std::true_type {};
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
}
template <typename T>
void toValue(const std::string &strvalue, std::vector<T> &value) {
detail::toValue(strvalue, value, detail::is_range_type<T>());
}
template <typename T>
void toValue(const std::string &strvalue, std::vector<std::vector<T>> &value,
const std::string &outerDelimiter = ",",
const std::string &innerDelimiter = "+") {
typedef Mantid::Kernel::StringTokenizer tokenizer;
tokenizer tokens(strvalue, outerDelimiter,
tokenizer::TOK_IGNORE_EMPTY | tokenizer::TOK_TRIM);
value.clear();
value.reserve(tokens.count());
for (const auto &token : tokens) {
tokenizer values(token, innerDelimiter,
tokenizer::TOK_IGNORE_EMPTY | tokenizer::TOK_TRIM);
std::vector<T> vect;
vect.reserve(values.count());
std::transform(
values.begin(), values.end(), std::back_inserter(vect),
[](const std::string &str) { return boost::lexical_cast<T>(str); });
value.push_back(std::move(vect));
}
}
/*Used specifically to retrieve a vector of type T populated with values
* given to it from strvalue parameter, Using toValue method.
(See constructor used specifically for vector assignments)
*/
template <typename T> T extractToValueVector(const std::string &strvalue) {
T valueVec;
toValue(strvalue, valueVec);
return valueVec;
}
//------------------------------------------------------------------------------------------------
// 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 <> inline void addingOperator(OptionalBool &, const OptionalBool &) {
throw Exception::NotImplementedError(
"PropertyWithValue.h: += operator not implemented for type OptionalBool");
}
template <typename T>
inline void addingOperator(boost::shared_ptr<T> &,
const boost::shared_ptr<T> &) {
throw Exception::NotImplementedError(
"PropertyWithValue.h: += operator not implemented for boost::shared_ptr");
}
template <typename T>
inline std::vector<std::string>
determineAllowedValues(const T &, const IValidator &validator) {
return validator.allowedValues();
}
template <>
inline std::vector<std::string> determineAllowedValues(const OptionalBool &,
const IValidator &) {
auto enumMap = OptionalBool::enumToStrMap();
std::vector<std::string> values;
values.reserve(enumMap.size());
std::transform(enumMap.cbegin(), enumMap.cend(), std::back_inserter(values),
[](const std::pair<OptionalBool::Value, std::string> &str) {
return str.second;
});
return values;
}
}
} // namespace Kernel
} // namespace Mantid
#endif /* MANTID_KERNEL_PROPERTYHELPER_H_ */