10#include <boost/lexical_cast.hpp>
27template <
typename T> std::string
toString(
const T &
value) {
return boost::lexical_cast<std::string>(
value); }
30template <
typename T> std::string
toString(
const std::shared_ptr<T> &
value) {
32 throw boost::bad_lexical_cast();
36template <
typename T> std::string
toString(
const std::vector<T> &
value,
const std::string &delimiter =
",") {
37 std::stringstream result;
38 std::size_t vsize =
value.size();
39 for (std::size_t i = 0; i < vsize; ++i) {
49std::string
toString(
const std::vector<std::vector<T>> &
value,
const std::string &outerDelimiter =
",",
50 const std::string &innerDelimiter =
"+") {
51 std::stringstream result;
52 std::size_t vsize =
value.size();
53 for (std::size_t i = 0; i < vsize; ++i) {
54 std::size_t innervsize =
value[i].size();
55 for (std::size_t j = 0; j < innervsize; ++j) {
56 result <<
value[i][j];
57 if (j + 1 != innervsize)
58 result << innerDelimiter;
62 result << outerDelimiter;
69template <
typename T> std::string toPrettyString(
const T &
value,
size_t maxLength = 0,
bool collapseLists =
true) {
76std::string toPrettyString(
const std::shared_ptr<T> &
value,
size_t maxLength = 0,
bool collapseLists =
true) {
80 throw boost::bad_lexical_cast();
88std::string toPrettyString(
89 const std::vector<T> &
value,
size_t maxLength = 0,
bool collapseLists =
true,
const std::string &delimiter =
",",
90 const std::string &unusedDelimiter =
"+",
91 typename std::enable_if<!(std::is_integral<T>::value && std::is_arithmetic<T>::value)>::type * =
nullptr) {
106toPrettyString(
const std::vector<T> &
value,
size_t maxLength = 0,
bool collapseLists =
true,
107 const std::string &delimiter =
",",
const std::string &listDelimiter =
"-",
108 typename std::enable_if<std::is_integral<T>::value && std::is_arithmetic<T>::value>::type * =
nullptr) {
126std::
string toPrettyString(const
std::vector<
bool> &
value,
size_t maxLength,
bool collapseLists,
127 const
std::
string &delimiter, const
std::
string &unusedDelimiter,
128 typename
std::enable_if<
std::is_same<
bool,
bool>::
value>::type *) {
138std::
string toPrettyString(const
std::vector<
std::vector<T>> &
value,
size_t maxLength = 0,
bool collapseLists = true,
139 const
std::
string &outerDelimiter = ",", const
std::
string &innerDelimiter = "+") {
146template <
typename T>
int findSize(
const T &) {
return 1; }
149template <
typename T>
int findSize(
const std::vector<T> &
value) {
return static_cast<int>(
value.size()); }
152template <
typename T>
inline void appendValue(
const std::string &strvalue, std::vector<T> &
value) {
154 std::size_t pos = strvalue.find(
':');
155 std::size_t numChar = std::string::npos;
157 if (pos == std::string::npos) {
158 pos = strvalue.find(
'-', 1);
160 const auto posStep = strvalue.find(
':', pos + 1);
161 if (posStep != std::string::npos) {
162 step = boost::lexical_cast<T>(strvalue.substr(posStep + 1));
163 numChar = posStep - pos - 1;
168 if (pos == std::string::npos) {
169 value.emplace_back(boost::lexical_cast<T>(strvalue));
173 if (step ==
static_cast<T
>(0))
174 throw std::logic_error(
"Step size must be non-zero");
177 const auto start = boost::lexical_cast<T>(strvalue.substr(0, pos));
178 const auto stop = boost::lexical_cast<T>(strvalue.substr(pos + 1, numChar));
180 if (start + step < start)
181 throw std::logic_error(
"Step size is negative with increasing limits");
182 for (
auto i = start; i <= stop;) {
183 value.emplace_back(i);
186 i =
static_cast<T
>(i + step);
189 if (start + step >= start)
190 throw std::logic_error(
"Step size is positive with decreasing limits");
191 for (
auto i = start; i >= stop;) {
192 value.emplace_back(i);
195 i =
static_cast<T
>(i + step);
200template <
typename T>
void toValue(
const std::string &strvalue, T &
value) {
value = boost::lexical_cast<T>(strvalue); }
202template <
typename T>
void toValue(
const std::string &, std::shared_ptr<T> &) {
throw boost::bad_lexical_cast(); }
206template <
typename T>
void toValue(
const std::string &strvalue, std::vector<T> &
value, std::true_type) {
211 for (
const auto &token : values) {
212 appendValue(token,
value);
216template <
typename T>
void toValue(
const std::string &strvalue, std::vector<T> &
value, std::false_type) {
223 std::transform(values.
cbegin(), values.
cend(), std::back_inserter(
value),
224 [](
const std::string &str) { return boost::lexical_cast<T>(str); });
229template <
class T>
struct is_range_type :
public std::false_type {};
230template <
class T>
struct is_range_type<const T> :
public is_range_type<T> {};
231template <
class T>
struct is_range_type<volatile const T> :
public is_range_type<T> {};
232template <
class T>
struct is_range_type<volatile T> :
public is_range_type<T> {};
234template <>
struct is_range_type<unsigned short> :
public std::true_type {};
235template <>
struct is_range_type<unsigned int> :
public std::true_type {};
236template <>
struct is_range_type<unsigned long> :
public std::true_type {};
237template <>
struct is_range_type<unsigned long long> :
public std::true_type {};
239template <>
struct is_range_type<short> :
public std::true_type {};
240template <>
struct is_range_type<int> :
public std::true_type {};
241template <>
struct is_range_type<long> :
public std::true_type {};
242template <>
struct is_range_type<long long> :
public std::true_type {};
244template <
typename T>
void toValue(
const std::string &strvalue, std::vector<T> &
value) {
245 detail::toValue(strvalue,
value, detail::is_range_type<T>());
249void toValue(
const std::string &strvalue, std::vector<std::vector<T>> &
value,
const std::string &outerDelimiter =
",",
250 const std::string &innerDelimiter =
"+") {
255 value.reserve(tokens.count());
257 for (
const auto &token : tokens) {
260 vect.reserve(values.count());
261 std::transform(values.begin(), values.end(), std::back_inserter(vect),
262 [](
const std::string &str) { return boost::lexical_cast<T>(str); });
263 value.emplace_back(std::move(vect));
271template <
typename T> T extractToValueVector(
const std::string &strvalue) {
273 toValue(strvalue, valueVec);
279template <
typename T>
inline void addingOperator(T &lhs,
const T &
rhs) {
285 lhs =
static_cast<T
>(lhs +
rhs);
288template <
typename T>
inline void addingOperator(std::vector<T> &lhs,
const std::vector<T> &
rhs) {
291 lhs.insert(lhs.end(),
rhs.begin(),
rhs.end());
293 std::vector<T> rhs_copy(
rhs);
294 lhs.insert(lhs.end(), rhs_copy.begin(), rhs_copy.end());
298template <>
inline void addingOperator(
bool &,
const bool &) {
299 throw Exception::NotImplementedError(
"PropertyWithValue.h: += operator not implemented for type bool");
302template <>
inline void addingOperator(OptionalBool &,
const OptionalBool &) {
303 throw Exception::NotImplementedError(
"PropertyWithValue.h: += operator not implemented for type OptionalBool");
306template <
typename T>
inline void addingOperator(std::shared_ptr<T> &,
const std::shared_ptr<T> &) {
307 throw Exception::NotImplementedError(
"PropertyWithValue.h: += operator not implemented for std::shared_ptr");
310template <
typename T>
inline std::vector<std::string> determineAllowedValues(
const T &,
const IValidator &validator) {
311 return validator.allowedValues();
314template <>
inline std::vector<std::string> determineAllowedValues(
const OptionalBool &,
const IValidator &) {
316 std::vector<std::string> values;
317 values.reserve(enumMap.size());
318 std::transform(enumMap.cbegin(), enumMap.cend(), std::back_inserter(values),
319 [](
const std::pair<OptionalBool::Value, std::string> &str) { return str.second; });
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 ...
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
static std::map< Value, std::string > enumToStrMap()
@ TOK_IGNORE_EMPTY
ignore empty tokens
@ TOK_TRIM
remove leading and trailing whitespace from tokens
ConstIterator cend() const
Const iterator referring to the past-the-end element in the container.
ConstIterator cbegin() const
Const iterator referring to first element in the container.
std::size_t count() const
Get the total number of tokens.
DLLExport std::string joinCompress(ITERATOR_TYPE begin, ITERATOR_TYPE end, const std::string &separator=",", const std::string &listSeparator="-")
Join a set or vector of (something that turns into a string) together into one string,...
MANTID_KERNEL_DLL std::string shorten(const std::string &input, const size_t max_length)
Converts long strings into "start ... end".
DLLExport std::string join(ITERATOR_TYPE begin, ITERATOR_TYPE end, const std::string &separator, typename std::enable_if<!(std::is_same< typename std::iterator_traits< ITERATOR_TYPE >::iterator_category, std::random_access_iterator_tag >::value)>::type *=nullptr)
Join a set or vector of (something that turns into a string) together into one string,...
std::string toString(const T &value)
Convert a number to a string.
Helper class which provides the Collimation Length for SANS instruments.