Commit bedefa22 authored by Valerio Bertone's avatar Valerio Bertone
Browse files

Including util to get vector of integers from string

parent 8ff4a126
......@@ -89,6 +89,8 @@ public:
std::vector<double> toVectorDouble() const;
std::vector<int> toVectorInt() const;
/**
* Convert stream to string
* @return std::string
......
......@@ -93,13 +93,9 @@ std::vector<double> GenericType::toVectorDouble() const {
std::vector<double> vd{};
double d;
while (sstream >> d) {
vd.push_back(d);
}
while(sstream >> d || !sstream.eof()) {
if(sstream.fail()) {
throw std::runtime_error("[GenericType::toVectorDouble] cast from std::string to unsigned short failed ! ");
throw std::runtime_error("[GenericType::toVectorDouble] cast from std::string to std::vector<double> failed ! ");
}
vd.push_back(d);
}
......@@ -107,6 +103,23 @@ std::vector<double> GenericType::toVectorDouble() const {
return vd;
}
std::vector<int> GenericType::toVectorInt() const {
//create a temporary object to avoid empty m_stream after use of >> operator ; for multiple use of the same GenericType object
std::stringstream sstream;
sstream << m_stream.str();
std::vector<int> vi{};
int i;
while(sstream >> i || !sstream.eof()) {
if(sstream.fail()) {
throw std::runtime_error("[GenericType::toVectorInt] cast from std::string to std::vector<int> failed ! ");
}
vi.push_back(i);
}
return vi;
}
std::string GenericType::getString() const {
return m_stream.str();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment