From a7e376afd1992c293895582dc1d2a0cc6e8ca1ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=BCske?= <torin@git.sch.bme.hu> Date: Sat, 2 May 2015 14:59:28 +0200 Subject: [PATCH] =?UTF-8?q?PHP=20list()=20II.=20=E2=80=93=20a=20nehezebbik?= =?UTF-8?q?=20fele?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 80 ++++++++++++++++++++++++++++++++++++------------------ szorg5.cdp | 1 + 2 files changed, 55 insertions(+), 26 deletions(-) create mode 100644 szorg5.cdp diff --git a/main.cpp b/main.cpp index 08977f0..c484ce8 100644 --- a/main.cpp +++ b/main.cpp @@ -1,44 +1,72 @@ #include <iostream> #include <string> #include <sstream> +#include <vector> +#include <tuple> -template<class T> -void unpack(std::istringstream& s, T& arg) -{ - s>>arg; -} +template<class ...T> +class container; -template<class T> -void unpack(std::istringstream&& s, T& arg) -{ - s>>arg; -} +template<class ...T> +container<T...> list(T&...args); -template<class T, class ...V> -void unpack(std::istringstream& s, T& arg , V& ...args) +template<class ...T> +class container { - s>>arg; - unpack(s,args...); -} +private: + std::tuple<T&...> tu; +public: + container(T&...args):tu{args...}{} + + template<class ...F> + friend container<T...> list(T&...args); + + template<int I> + typename std::enable_if<I==1>::type readhelper(std::vector<std::string>& s) + { + std::istringstream is{s[I-1]}; + is>>std::get<I-1>(tu); + } + + template<int I> + typename std::enable_if<I!=1>::type readhelper(std::vector<std::string>& s) + { + readhelper<I-1>(s); + std::istringstream is{s[I-1]}; + is>>std::get<I-1>(tu); + } + +public: + void operator=(std::vector<std::string>&& s) + { + readhelper<std::tuple_size<decltype(tu)>::value>(s); + } +}; -template<class T, class ...V> -void unpack(std::istringstream&& s, T& arg , V& ...args) +template<class ...T> +container<T...> list(T&...args) { - s>>arg; - unpack(s,args...); + return container<T...>{args...}; } template<class ...T> -void unpack(std::string s, T& ...args) +std::vector<std::string> explode(std::string s) { - unpack(std::istringstream{s},args...); + std::vector<std::string> vec; + std::istringstream d{s}; + while(d) + { + std::string st; + d>>st; + vec.push_back(st); + } + return std::move(vec); } int main(int argc, char **argv) { - int a; - double b; - std::string c; - unpack("3 4.5 bla", a, b, c); // a = 3; b = 4.5; - std::cout<<a<<":"<<b<<":"<<c; + int a=0; + double b=0; + list(a, b) = explode("3 4.5"); + std::cout<<a<<":"<<b; return 0; } \ No newline at end of file diff --git a/szorg5.cdp b/szorg5.cdp new file mode 100644 index 0000000..7774d3c --- /dev/null +++ b/szorg5.cdp @@ -0,0 +1 @@ +{"compileCppOptions":["-std\u003dc++11"],"requirements":[]} \ No newline at end of file -- GitLab