From 4486edee607f7e5ae953842372a90fb7d80f809e Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 3 Oct 2023 00:43:08 -0400 Subject: [PATCH] add .get for argparse (does a get_cast), add rdtsc for profilers --- include/blt/parse/argparse.h | 109 ++++++++++++++++++----------------- include/blt/std/system.h | 15 +++++ 2 files changed, 72 insertions(+), 52 deletions(-) diff --git a/include/blt/parse/argparse.h b/include/blt/parse/argparse.h index 15ea9a7..2a37ba1 100755 --- a/include/blt/parse/argparse.h +++ b/include/blt/parse/argparse.h @@ -255,6 +255,57 @@ namespace blt class arg_parse { + public: + + template + static inline bool holds_alternative(const arg_data_t& v) + { + if constexpr (std::is_same_v) + return std::holds_alternative(v); + else + return std::holds_alternative(v) && std::holds_alternative(std::get(v)); + } + + + template + static inline T& get(arg_data_t& v) + { + if constexpr (std::is_same_v) + return std::get(v); + else + return std::get(std::get(v)); + } + + /** + * Attempt to cast the variant stored in the arg results to the requested type + * if user is requesting an int, but holds a string, we are going to make the assumption the data can be converted + * it is up to the user to deal with the variant if they do not want this behaviour! + * @tparam T type to convert to + * @param v + * @return + */ + template + static inline T get_cast(arg_data_t& v) + { + if constexpr (std::is_same_v) + return std::get(v); + auto t = std::get(v); + // user is requesting an int, but holds a string, we are going to make the assumption the data can be converted + // it is up to the user to deal with the variant if they do not want this behaviour! + if constexpr (!std::is_arithmetic_v) + return std::get(t); + // ugly! + if (std::holds_alternative(t)) + return static_cast(std::get(t)); + if (std::holds_alternative(t)) + return static_cast(std::get(t)); + auto s = std::get(t); + if constexpr (std::is_floating_point_v) + return static_cast(std::stod(s)); + if constexpr (std::is_signed_v) + return static_cast(std::stoll(s)); + return static_cast(std::stoull(s)); + } private: struct { @@ -287,6 +338,12 @@ namespace blt return data[key]; } + template + inline T get(const std::string& key) + { + return blt::arg_parse::get_cast(data[key]); + } + inline auto begin() { return data.begin(); @@ -341,58 +398,6 @@ namespace blt return use_full_name ? loaded_args.program_name : filename(loaded_args.program_name); } - public: - - template - static inline bool holds_alternative(const arg_data_t& v) - { - if constexpr (std::is_same_v) - return std::holds_alternative(v); - else - return std::holds_alternative(v) && std::holds_alternative(std::get(v)); - } - - - template - static inline T& get(arg_data_t& v) - { - if constexpr (std::is_same_v) - return std::get(v); - else - return std::get(std::get(v)); - } - - /** - * Attempt to cast the variant stored in the arg results to the requested type - * if user is requesting an int, but holds a string, we are going to make the assumption the data can be converted - * it is up to the user to deal with the variant if they do not want this behaviour! - * @tparam T type to convert to - * @param v - * @return - */ - template - static inline T get_cast(arg_data_t& v) - { - if constexpr (std::is_same_v) - return std::get(v); - auto t = std::get(v); - // user is requesting an int, but holds a string, we are going to make the assumption the data can be converted - // it is up to the user to deal with the variant if they do not want this behaviour! - if constexpr (!std::is_arithmetic_v) - return std::get(t); - // ugly! - if (std::holds_alternative(t)) - return static_cast(std::get(t)); - if (std::holds_alternative(t)) - return static_cast(std::get(t)); - auto s = std::get(t); - if constexpr (std::is_floating_point_v) - return static_cast(std::stod(s)); - if constexpr (std::is_signed_v) - return static_cast(std::stoll(s)); - return static_cast(std::stoull(s)); - } - public: arg_parse(const std::string& helpMessage = "show this help menu and exit") { diff --git a/include/blt/std/system.h b/include/blt/std/system.h index 96e13a1..1aca8a9 100755 --- a/include/blt/std/system.h +++ b/include/blt/std/system.h @@ -7,7 +7,22 @@ #ifndef BLT_SYSTEM_H #define BLT_SYSTEM_H +#ifdef _WIN32 +#include +#else +#include +#endif +#include + namespace blt::system { +//#ifdef __GNUC__ +// #define GNU_INLINE __attribute__((__gnu_inline__, __always_inline__)) +//#else +// #define GNU_INLINE +//#endif + inline std::uint64_t rdtsc(){ + return __rdtsc(); + } // TODO: system memory and current CPU usage. (Linux Only currently) }