Belle II Software development
StringToVector Class Reference

The StringToVector class converts a given string to the std::vector<T>. More...

#include <ARICHDatabaseTools.h>

Static Public Member Functions

template<typename T >
static auto convert (const std::string &rLine, const char delim=' ') -> std::vector< T >
 convert<T> converts the given string in to a std::vector<T> elements seperated by the given delimiter
 
template<typename T >
static auto parse (const std::string &rLine, const char delim=' ') -> std::string
 parse<T> expands the given string and evaluats defined operators.
 

Static Public Attributes

static const std::string m_gRangeOperator
 m_gRangeOperator is a std::wstring containing the range symbold
 

Static Private Member Functions

template<typename T >
static auto expand (std::ostream &rStream, const char delim, const std::string &rToken) -> std::ostream &
 expand<T> applies the given range and fills the given std::ostream with each element in range seperated by the delimiter char
 

Detailed Description

The StringToVector class converts a given string to the std::vector<T>.

The input string is allowed to contain white spaces and the specific types.

Definition at line 351 of file ARICHDatabaseTools.h.

Member Function Documentation

◆ convert()

static auto convert ( const std::string &  rLine,
const char  delim = ' ' 
) -> std::vector<T>
inlinestatic

convert<T> converts the given string in to a std::vector<T> elements seperated by the given delimiter

Parameters
rLinetokens seperated by the delimiter
delimdelimiter character
Returns
std::vector<T>
Exceptions
std::runtime_error()if
  • rLine contains other characters than delimiter, white space, digits or allowed characters defined by PrivateHelperClasses::TokenCast<T>
See also
PrivateHelperClasses::TokenCast<T> and its specializations

Definition at line 371 of file ARICHDatabaseTools.h.

373 {
374 const auto cast = PrivateHelperClasses::TokenCast<T>();
375 // check if line only contains white space, numbers and delimiter char.
376 if (!std::all_of(rLine.begin(), rLine.end(),
377 [&cast, delim](const unsigned char c) {
378 return std::isdigit(c) || std::isspace(c) ||
379 (c == delim) || cast.isValidChar(c);
380 }))
381 throw std::runtime_error("Detected invalid character in '" + rLine +
382 "'!");
383
384 auto iss = std::istringstream(rLine);
385 auto retval = std::vector<T>();
386 auto token = std::string();
387
388 // convert string to number and add it to vector...
389 while (std::getline(iss, token, delim))
390 try {
391 retval.emplace_back(cast(token));
392 } catch (const std::invalid_argument& rErr) {
393 throw std::runtime_error("Invalid token, got:'" + token + "'! " +
394 rErr.what());
395 } catch (const std::out_of_range& rErr) {
396 // if the values exceeds the requested type
397 throw std::runtime_error("Conversion out of range, got: '" + token +
398 "'! " + rErr.what());
399 }
400
401 return retval;
402 }

◆ expand()

static auto expand ( std::ostream &  rStream,
const char  delim,
const std::string &  rToken 
) -> std::ostream&
inlinestaticprivate

expand<T> applies the given range and fills the given std::ostream with each element in range seperated by the delimiter char

Parameters
rStream
delim
rToken
Exceptions
std::runtime_errorif invalid range definition, ie A to B with A > B
Note
the precrement operator of T has to be defined, ie T::operator++()

Definition at line 464 of file ARICHDatabaseTools.h.

466 {
467 auto itPos = std::search(rToken.begin(), rToken.end(),
468 m_gRangeOperator.begin(), m_gRangeOperator.end());
469 const auto cast = PrivateHelperClasses::TokenCast<T>();
470 auto lhs = cast(std::string(rToken.begin(), itPos));
471 std::advance(itPos, m_gRangeOperator.size());
472 auto rhs = cast(std::string(itPos, rToken.end()));
473
474 if (lhs >= rhs)
475 throw std::runtime_error(
476 "Invalid expansion! lhs ist greater then rhs, got: '" + rToken +
477 "'!");
478
479 rStream << lhs;
480 for (++lhs; lhs <= rhs; ++lhs)
481 rStream << delim << lhs;
482 return rStream;
483 }
static const std::string m_gRangeOperator
m_gRangeOperator is a std::wstring containing the range symbold

◆ parse()

static auto parse ( const std::string &  rLine,
const char  delim = ' ' 
) -> std::string
inlinestatic

parse<T> expands the given string and evaluats defined operators.

Currently only the range operator is defined and used.

Parameters
rLine
delim
Returns
the evaluated std::string.
Exceptions
std::runtime_error()if
  • invalid argument for during the operator parsing
  • out of range convertion, ie casting 0xffff into int8_t

Definition at line 415 of file ARICHDatabaseTools.h.

417 {
418 auto out = std::stringstream();
419 auto iss = std::istringstream(rLine);
420 auto token = std::string();
421
422 if (!std::getline(iss, token, delim))
423 return std::string();
424
425 if (std::search(token.begin(), token.end(), m_gRangeOperator.begin(),
426 m_gRangeOperator.end()) != token.end()) {
427 StringToVector::expand<T>(out, delim, token);
428 } else {
429 out << token;
430 }
431
432 // convert string to number and add it to vector...
433 while (std::getline(iss, token, delim))
434 try {
435 if (std::search(token.begin(), token.end(), m_gRangeOperator.begin(),
436 m_gRangeOperator.end()) != token.end()) {
437 out << ',';
438 StringToVector::expand<T>(out, delim, token);
439 } else {
440 out << delim << token;
441 }
442 } catch (const std::invalid_argument& rErr) {
443 throw std::runtime_error("Invalid token, got:'" + token + "'! " +
444 rErr.what());
445 } catch (const std::out_of_range& rErr) {
446 // if the values exceeds the requested type
447 throw std::runtime_error("Conversion out of range, got: '" + token +
448 "'! " + rErr.what());
449 }
450 return out.str();
451 }

Member Data Documentation

◆ m_gRangeOperator

const std::string m_gRangeOperator
static

m_gRangeOperator is a std::wstring containing the range symbold

Definition at line 356 of file ARICHDatabaseTools.h.


The documentation for this class was generated from the following file: