ComPPare 1.0.0
Loading...
Searching...
No Matches
helper.hpp
Go to the documentation of this file.
1/*
2
3Copyright 2025 | Leong Fan FUNG | funglf | stanleyfunglf@gmail.com
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the “Software”), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21SOFTWARE.
22
23*/
24#pragma once
25#include <charconv>
26#include <string>
27#include <string_view>
28#include <stdexcept>
29#include <cstdint>
30
32
34{
35 template <typename T>
36 T get_arg_value(std::string_view option, const char *nextArg)
37 {
38 std::string_view valstr;
39 if (auto eq = option.find('='); eq != std::string_view::npos)
40 valstr = option.substr(eq + 1);
41 else if (nextArg)
42 valstr = nextArg;
43 else
44 throw std::invalid_argument(std::string(option) + " requires a value");
45
46
47 if constexpr (std::same_as<T, std::string>)
48 {
49 return std::string(valstr);
50 }
51 else if constexpr (std::is_integral_v<T>) // if integral type
52 {
53 size_t idx = 0;
54 std::string s{valstr};
55
56 if constexpr (std::is_signed_v<T>) // if signed
57 {
58 long long tmp = std::stoll(s, &idx);
59 if (idx != s.size()) // if not all characters were processed
60 throw std::invalid_argument("invalid integer for " + std::string(option));
61
62 if (tmp < std::numeric_limits<T>::min() ||
63 tmp > std::numeric_limits<T>::max()) // if out of range for T
64 throw std::out_of_range("integer out of range for " + std::string(option));
65
66 return static_cast<T>(tmp);
67 }
68 else // unsigned
69 {
70 if (!s.empty() && s.front() == '-') // if negative -- reject
71 throw std::invalid_argument("invalid unsigned integer for " + std::string(option));
72
73 unsigned long long tmp = std::stoull(s, &idx);
74 if (idx != s.size()) // if not all characters were processed
75 throw std::invalid_argument("invalid unsigned integer for " + std::string(option));
76
77 if (tmp > std::numeric_limits<T>::max()) // if out of range for T
78 throw std::out_of_range("unsigned integer out of range for " + std::string(option));
79
80 return static_cast<T>(tmp);
81 }
82 }
83 else if constexpr (std::is_floating_point_v<T>)
84 {
85 size_t idx = 0;
86 long double tmp = std::stold(std::string(valstr), &idx);
87 if (idx != valstr.size())
88 throw std::invalid_argument("invalid floating-point for " + std::string(option));
89 return static_cast<T>(tmp);
90 }
91 else
92 {
93 static_assert(std::is_arithmetic_v<T> || std::same_as<T, std::string>,
94 "get_arg_value supports only arithmetic types or std::string");
95 }
96 }
97
98 static inline void parse_args(int argc, char **argv)
99 {
100 if (!argv)
101 return;
102
103 for (int i = 1; i < argc; ++i)
104 {
105 std::string_view arg = argv[i];
106
107 auto get_next_arg_if_needed = [&](std::string_view a) -> const char *
108 {
109 return (a.find('=') == std::string_view::npos && i + 1 < argc)
110 ? argv[++i]
111 : nullptr;
112 };
113
114 if (arg.rfind("--warmup", 0) == 0)
115 {
116 auto w = get_arg_value<std::uint64_t>(arg, get_next_arg_if_needed(arg));
118 }
119 else if (arg.rfind("--iter", 0) == 0)
120 {
121 auto n = get_arg_value<std::uint64_t>(arg, get_next_arg_if_needed(arg));
123 }
124 else if (arg.rfind("--tolerance", 0) == 0)
125 {
126 auto tol = get_arg_value<long double>(arg, get_next_arg_if_needed(arg));
128 }
129 }
130 }
131} // namespace comppare::internal::helper
static void set_warmup_iters(uint64_t v)
Set the number of warmup iterations.
Definition config.hpp:68
static void set_all_fp_tolerance(long double v)
Set the floating-point tolerance for all supported types.
Definition config.hpp:197
static void set_bench_iters(uint64_t v)
Set the number of benchmark iterations.
Definition config.hpp:73
This file contains configuration settings for the ComPPare library.
Definition helper.hpp:34
T get_arg_value(std::string_view option, const char *nextArg)
Definition helper.hpp:36
static void parse_args(int argc, char **argv)
Definition helper.hpp:98