My Project
svector.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef mia_core_svector_hh
22 #define mia_core_svector_hh
23 
24 #include <istream>
25 #include <ostream>
26 #include <sstream>
27 #include <vector>
28 #include <stdexcept>
29 
30 #include <mia/core/errormacro.hh>
31 #include <mia/core/typedescr.hh>
32 
34 
39 template <typename T>
40 std::ostream& operator << (std::ostream& os, const std::vector<T>& v)
41 {
42  auto i = v.begin();
43  auto e = v.end();
44 
45  if (i != e)
46  os << *i++;
47 
48  while (i != e)
49  os << "," << *i++;
50 
51  return os;
52 }
53 
54 template <typename T>
56  static bool apply(const std::string& str, T& v)
57  {
58  char c;
59  std::istringstream s(str);
60  s >> v;
61 
62  if (s.fail())
63  return false;
64 
65  while (!s.eof() && s.peek() == ' ')
66  s >> c;
67 
68  return s.eof();
69  }
70 };
71 
72 template <>
73 struct __dispatch_translate<std::string> {
74  static bool apply(const std::string& s, std::string& str)
75  {
76  str = s;
77  return true;
78  }
79 };
80 
81 
82 template <typename T>
83 std::istream& operator >> (std::istream& is, std::vector<T>& v)
84 {
85  std::vector<T> values;
86  std::string token;
87  T val;
88 
89  while (std::getline(is, token, ',')) {
90  if (__dispatch_translate<T>::apply(token, val))
91  values.push_back(val);
92  else {
93  throw create_exception<std::invalid_argument>("Reading vector: value, '", token,
94  "' could not be translate to ",
95  mia::__type_descr<T>::value);
96  }
97  }
98 
99  if (!v.empty() && v.size() != values.size()) {
100  throw create_exception<std::invalid_argument>("Reading vector: expected ",
101  v.size(), " values, but got ", values.size());
102  }
103 
104  v.swap(values);
105  return is;
106 }
107 
108 
110 
111 #endif
112 
operator<<
std::ostream & operator<<(std::ostream &os, const std::vector< T > &v)
implements the direct streaming of std::vectors.
Definition: svector.hh:40
NS_MIA_BEGIN
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
typedescr.hh
NS_MIA_END
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36
errormacro.hh
__dispatch_translate< std::string >::apply
static bool apply(const std::string &s, std::string &str)
Definition: svector.hh:74
operator>>
std::istream & operator>>(std::istream &is, std::vector< T > &v)
Definition: svector.hh:83
std
Definition: gsl_iterator.hh:324
__dispatch_translate::apply
static bool apply(const std::string &str, T &v)
Definition: svector.hh:56
__dispatch_translate
Definition: svector.hh:55