1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| template <int N, class... Args>
struct tuple_string_repeat {
typedef typename tuple_string_repeat<N-1, std::string, Args...>::type type;
};
template <class... Args>
struct tuple_string_repeat<1, Args...> {
typedef std::tuple<std::string, Args...> type;
};
template <class... Args>
struct tuple_string_repeat<0, Args...> {
typedef std::vector<std::string> type;
};
template <class... Types>
struct explode_impl;
template <class T, class... Types>
struct explode_impl<T, Types...> {
static std::tuple<T, Types...> call(std::istream& is, const char delimiter) {
// fixer l'ordre d'appel et recursion
const auto& tmp = explode_impl<T>::call(is, delimiter);
return std::tuple_cat(tmp, explode_impl<Types...>::call(is, delimiter));
}
};
template <class T>
struct explode_impl<T> {
static std::tuple<T> call(std::istream& str, const char delimiter) {
// get item
std::string buffer;
if(!std::getline(str, buffer, delimiter)) {
throw std::exception();
}
T t;
std::istringstream ss(buffer);
if(!(ss >> t)) {
throw std::exception();
}
return std::make_tuple(t);
}
};
template <int N>
struct explode_impl_string {
static typename tuple_string_repeat<N>::type call(std::istream& str, const char delimiter) {
// fixer l'ordre d'appel et recursion
const auto& tmp = explode_impl_string<1>::call(str, delimiter);
return std::tuple_cat(tmp, explode_impl_string<N-1>::call(str, delimiter));
}
};
template <>
struct explode_impl_string<1> {
static tuple_string_repeat<1>::type call(std::istream& str, const char delimiter) {
// get item
std::string buffer;
if(!std::getline(str, buffer, delimiter)) {
throw std::exception();
}
return std::make_tuple(buffer);
}
};
template <>
struct explode_impl_string<0> {
static tuple_string_repeat<0>::type call(std::istream& str, const char delimiter) {
std::vector<std::string> ret;
std::string buffer;
while(std::getline(str, buffer, delimiter)) {
ret.push_back(buffer);
}
return ret;
}
};
template <class... Types>
std::tuple<Types...> explode(const std::string& str, const char delimiter) {
std::istringstream is(str);
return explode_impl<Types...>::call(is, delimiter);
}
template <int N>
typename tuple_string_repeat<N>::type explode(const std::string& str, const char delimiter) {
std::istringstream is(str);
return explode_impl_string<N>::call(is, delimiter);
}
tuple_string_repeat<0>::type explode(const std::string& str, const char delimiter) {
std::istringstream is(str);
return explode_impl_string<0>::call(is, delimiter);
} |