yat  0.15.2pre
getvector.h
1 #ifndef _theplu_yat_utility_getvector_
2 #define _theplu_yat_utility_getvector_
3 
4 // $Id: getvector.h 3639 2017-04-26 02:12:08Z peter $
5 
6 /*
7  Copyright (C) 2017 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/type_traits/is_same.hpp>
29 
30 #include <istream>
31 #include <string>
32 #include <vector>
33 
34 namespace theplu {
35 namespace yat {
36 namespace utility {
37 
51  template<typename T>
52  std::istream& getvector(std::istream& is, std::vector<T>& result,
53  char delim, char newlinedelim='\n');
54 
61  template<typename T>
62  std::istream& getvector(std::istream& is, std::vector<T>& result,
63  const std::string& delim, char newlinedelim='\n');
64 
65 
66  // private stuff
67  namespace detail {
68 
69  template<typename T>
70  void convert(std::vector<std::string>& vec,
71  std::vector<T>& result, boost::true_type is_string)
72  {
73  // swap only swaps couple pointers, hence almost free. Do not us
74  // vec after this point.
75  std::swap(vec, result);
76  }
77 
78 
79  template<typename T>
80  void convert(std::vector<std::string>& vec,
81  std::vector<T>& result, boost::false_type is_string)
82  {
83  result.reserve(vec.size());
84  for (size_t i=0; i<vec.size(); ++i)
85  result.push_back(utility::convert<T>(vec[i]));
86  }
87 
88 
92  template<typename DELIM, typename T>
93  std::istream& getvector(std::istream& is, std::vector<T>& result,
94  DELIM delim, char nl_delim)
95  {
96  std::string line;
97  if (!getline(is, line, nl_delim))
98  return is;
99  std::vector<std::string> vec;
100  split(vec, line, delim);
101  typename boost::is_same<T, std::string> is_string;
102  convert(vec, result, is_string);
103  return is;
104  }
105 
106  } // end namespace detail
107 
108  // implementation of public API
109  template<typename T>
110  std::istream& getvector(std::istream& is, std::vector<T>& result,
111  char delim, char newlinedelim)
112  {
113  return detail::getvector(is, result, delim, newlinedelim);
114  }
115 
116 
117  template<typename T>
118  std::istream& getvector(std::istream& is, std::vector<T>& result,
119  const std::string& delim, char newlinedelim)
120  {
121  return detail::getvector(is, result, delim, newlinedelim);
122  }
123 
124 }}} // of namespace utility, yat, and theplu
125 
126 #endif
bool is(const std::string &s)
check if string is convertible to (numerical) type T
Definition: utility.h:485
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:110
void split(std::vector< std::string > &result, const std::string &str, char delim)
split a string into several substrings
std::string convert(T input)
convert T to a string
Definition: utility.h:465

Generated on Fri Jul 13 2018 02:33:27 for yat by  doxygen 1.8.11