Utilities/STL/LineStringTo.h
Go to the documentation of this file.
1 #ifndef _utl_LineStringTo_h_
2 #define _utl_LineStringTo_h_
3 
4 #include <boost/lexical_cast.hpp>
5 #include <boost/algorithm/string/trim.hpp>
6 #include <string>
7 #include <iostream>
8 
9 
10 namespace utl {
11 
18  template<class T, bool throwOnError = true, bool complain = true>
19  struct LineStringTo : public T {
20  LineStringTo& operator=(const T& t)
21  { T::operator=(t); return *this; }
22 
23  friend
24  std::istream&
25  operator>>(std::istream& is, LineStringTo& obj)
26  {
27  std::string line;
28  if (std::getline(is, line)) {
29  boost::algorithm::trim(line);
30  try {
31  obj = boost::lexical_cast<T>(line);
32  } catch (const boost::bad_lexical_cast& e) {
33  if (complain)
34  std::cerr << "line '" << line << "' could not be converted" << std::endl;
35  obj = T();
36  if (throwOnError)
37  throw e;
38  }
39  }
40  return is;
41  }
42  };
43 
44 }
45 
46 
47 #endif
friend std::istream & operator>>(std::istream &is, LineStringTo &obj)
bool is(const double a, const double b)
Definition: testlib.cc:113
LineStringTo & operator=(const T &t)

, generated on Tue Sep 26 2023.