From 186145c29712672a48d48085b3a25c7fd7846842 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Sat, 5 Sep 2020 13:54:04 +0800 Subject: [PATCH 1/2] improve args.h, make it shorter and clean. --- benchmark/util/args.h | 138 ++++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 78 deletions(-) diff --git a/benchmark/util/args.h b/benchmark/util/args.h index d7a8eb2c..4b001a50 100644 --- a/benchmark/util/args.h +++ b/benchmark/util/args.h @@ -7,96 +7,78 @@ namespace details { - template - bool extract(const char *, T &); + inline bool extract(const char * p, size_t & t) + { + char * e; + long long ll = std::strtoll(p, &e, 0); + if (*e || ll < 0) + { + return false; + } + t = static_cast(ll); + return true; + } - template - int parse(int todo, char ** p, ARGS & ... args); + inline bool extract(const char * p, unsigned short & t) + { + char * e; + long long ll = std::strtoll(p, &e, 0); + if (*e + || ll < static_cast(std::numeric_limits::min()) + || ll > static_cast(std::numeric_limits::max()) + ) + { + return false; + } + t = static_cast(ll); + return true; + } - template <> - int parse(int todo, char ** p) - { - return 0; - } + inline bool extract(const char * p, std::string & t) + { + t = p; + return true; + } - template - int parse(int todo, char ** p, T & t, ARGS & ... args) - { - if (todo != 0 && extract(*p, t)) - { - return parse(todo - 1, p + 1, args ...) + 1; - } - return 0; - } + inline bool extract(const char * p, const char *& t) + { + t = p; + return true; + } - template - int parse_args(int & argc, char ** argv, ARGS & ... args) - { - if (argc <= 1) - { - return 0; - } + template + inline int parse(int todo, char ** p, ARGS & ... args) + { + int index = 0; + (void)std::initializer_list{(index+=extract(*p, args), p++, 0)...}; + return index; + } - int length = argc - 1; - char ** begin = argv + 1; - char ** end = begin + length; + template + inline int parse_args(int & argc, char ** argv, ARGS & ... args) + { + if (argc <= 1) + { + return 0; + } - int done = parse(length, begin, args ...); - std::rotate(begin, begin + done, end); - std::reverse(end - done, end); + int length = argc - 1; + char ** begin = argv + 1; + char ** end = begin + length; - argc -= done; - return done; - } + int done = parse(length, begin, args ...); + std::rotate(begin, begin + done, end); + std::reverse(end - done, end); - template <> - bool extract(const char * p, size_t & t) - { - char * e; - long long ll = std::strtoll(p, &e, 0); - if (*e || ll < 0) - { - return false; - } - t = static_cast(ll); - return true; - } - - template <> - bool extract(const char * p, unsigned short & t) - { - char * e; - long long ll = std::strtoll(p, &e, 0); - if (*e - || ll < static_cast(std::numeric_limits::min()) - || ll > static_cast(std::numeric_limits::max()) - ) - { - return false; - } - t = static_cast(ll); - return true; - } - - template <> - bool extract(const char * p, std::string & t) - { - t = p; - return true; - } - - template <> - bool extract(const char * p, const char *& t) - { - t = p; - return true; - } + argc -= done; + return done; + } } template -static int parse_args(int & argc, char ** argv, ARGS & ... args) +inline static int parse_args(int & argc, char ** argv, ARGS & ... args) { - return details::parse_args(argc, argv, args ...); + return details::parse_args(argc, argv, args ...); } #endif //_BENCHMARK_ARGS_H_ From 33f50ca609fb52ea0f4c5c9797e5037ceced5277 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Tue, 8 Sep 2020 10:16:43 +0800 Subject: [PATCH 2/2] improve args.h. --- benchmark/util/args.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/benchmark/util/args.h b/benchmark/util/args.h index 4b001a50..e17f70af 100644 --- a/benchmark/util/args.h +++ b/benchmark/util/args.h @@ -46,6 +46,33 @@ namespace details return true; } + template + inline bool extract_impl(size_t todo, int& index, bool& r, char**& p, Arg& arg){ + if(!r||index>=todo){ + return false; + } + + r = extract(*p, arg); + + if(!r){ + return false; + } + + index++; + p++; + + return true; + } + + template + inline int parse(size_t todo, char ** p, ARGS & ... args) + { + int index = 0; + bool r = true; + (void)std::initializer_list{(extract_impl(todo, index, r, p, args), 0)...}; + return index; + } + template inline int parse(int todo, char ** p, ARGS & ... args) {