yat  0.21pre
getvector.h
1 #ifndef _theplu_yat_utility_getvector_
2 #define _theplu_yat_utility_getvector_
3 
4 // $Id: getvector.h 4207 2022-08-26 04:36:28Z peter $
5 
6 /*
7  Copyright (C) 2017, 2020, 2021, 2022 Peter Johansson
8 
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 #include <yat/utility/split.h>
26 #include <yat/utility/utility.h>
27 
28 #include <boost/algorithm/string/classification.hpp>
29 #include <boost/type_traits/is_same.hpp>
30 
31 #include <istream>
32 #include <string>
33 #include <utility>
34 #include <vector>
35 
36 namespace theplu {
37 namespace yat {
38 namespace utility {
39 
53  template<typename T>
54  std::istream& getvector(std::istream& is, std::vector<T>& result,
55  char delim, char newlinedelim='\n');
56 
63  template<typename T>
64  std::istream& getvector(std::istream& is, std::vector<T>& result,
65  const std::string& delim, char newlinedelim='\n');
66 
67 
68  // private stuff
69  namespace detail {
70 
71  template<typename T>
72  void convert(std::vector<std::string>&& vec,
73  std::vector<T>& result, boost::true_type is_string)
74  {
75  result = std::move(vec);
76  }
77 
78 
79  template<typename T>
80  void convert(const std::vector<std::string>& vec,
81  std::vector<T>& result, boost::false_type is_string)
82  {
83  result.clear();
84  result.reserve(vec.size());
85  for (const std::string& v : vec)
86  result.push_back(utility::convert<T>(v));
87  }
88 
89 
93  template<typename Predicate, typename T>
94  std::istream& getvector(std::istream& is, std::vector<T>& result,
95  Predicate pred, char delim)
96  {
97  if (is.good() == false)
98  return is;
99  std::vector<std::string> vec;
100  char c;
101  std::string word;
102  while (is.get(c)) {
103  if (pred(c)) {
104  vec.push_back(std::move(word));
105  word = "";
106  }
107  else if (c == delim)
108  break;
109  else
110  word += c;
111  }
112  // if is contained no characters, not even delim, leave result empty
113  if (vec.empty() && word=="" && is.good()==false)
114  return is;
115  vec.push_back(std::move(word));
116 
117  typename boost::is_same<T, std::string> is_string;
118  convert(std::move(vec), result, is_string);
119  return is;
120  }
121 
122  } // end namespace detail
123 
124  // implementation of public API
125  template<typename T>
126  std::istream& getvector(std::istream& is, std::vector<T>& result,
127  char delim, char newlinedelim)
128  {
129  return detail::getvector(is, result, [delim](char c) { return c==delim; },
130  newlinedelim);
131  }
132 
133 
134  template<typename T>
135  std::istream& getvector(std::istream& is, std::vector<T>& result,
136  const std::string& delim, char newlinedelim)
137  {
138  return detail::getvector(is, result, boost::is_any_of(delim), newlinedelim);
139  }
140 
141 }}} // of namespace utility, yat, and theplu
142 
143 #endif
bool is(const std::string &s)
check if string is convertible to (numerical) type T
Definition: utility.h:733
The Department of Theoretical Physics namespace as we define it.
Some useful functions are placed here.
std::istream & getvector(std::istream &is, std::vector< T > &result, char delim, char newlinedelim='\n')
Definition: getvector.h:126
std::string convert(T input)
convert T to a string
Definition: utility.h:673

Generated on Wed Jan 25 2023 03:34:29 for yat by  doxygen 1.8.14