Go to the documentation of this file.
7 #ifndef _POWERDXX_CLAS_HPP_
8 #define _POWERDXX_CLAS_HPP_
22 using namespace std::literals::string_literals;
93 size_t samples(
char const *
const str);
122 auto const valc = val - 2731;
123 return (valc + (valc >= 0) * 5 - (valc < 0) * 5) / 10;
140 template <
typename T>
141 std::pair<T, T>
range(T (& func)(
char const *
const),
char const *
const str) {
142 std::pair<T, T> result;
143 std::string first{str};
151 auto const sep = first.find(
':');
152 if (sep == std::string::npos) {
154 "missing colon separator in range: "s + str);
157 result.first = func(first.c_str());
158 result.second = func(str + sep + 1);
173 char const *
sysctlname(
char const *
const str);
193 template <
typename ... CharTs>
194 char const *
formatfields(
char const *
const fmt, CharTs
const ... fields) {
201 for (
char const expect : {fields ...,
'%'}) {
202 for (; it && *it; ++it) {
203 if (it[0] ==
'%' && it[1] ==
'%') {
207 if (it[0] ==
'%' && it[1] == expect) {
212 auto const hl =
highlight(fmt, it - fmt, 2);
213 fail(Exit::EFORMATFIELD, 0,
214 "unexpected formatting field: "s +
215 "\n\t" + hl.text +
"\n\t" + hl.line);
Contains literal operators.
Definition: utility.hpp:147
A collection of functions to process command line arguments.
Definition: clas.hpp:20
unsigned int mhz_t
Type for CPU frequencies in MHz.
Definition: types.hpp:40
types::mhz_t freq(char const *const str)
Convert string to frequency in MHz.
Definition: clas.cpp:183
A collection of type aliases.
Common error handling code.
Underlined highlight(std::string const &str, ptrdiff_t const offs, ptrdiff_t const len=1)
Underline the given number of characters.
Definition: utility.cpp:12
int decikelvin_t
Type for temperatures in dK.
Definition: types.hpp:45
const char * formatfields(char const *const fmt, CharTs const ... fields)
Sanitise user-provided formatting strings.
Definition: clas.hpp:194
Exit
Exit codes.
Definition: errors.hpp:22
std::pair< T, T > range(T(&func)(char const *const), char const *const str)
Takes a string encoded range of values and returns them.
Definition: clas.hpp:141
unsigned long cptime_t
Type for load counting.
Definition: types.hpp:35
types::cptime_t load(char const *const str)
Convert string to load in the range [0, 1024].
Definition: clas.cpp:153
void fail(Exit const exitcode, int const err, std::string const &msg)
Throws an Exception instance with the given message.
Definition: errors.hpp:94
@ ERANGEFMT
A user provided range is missing the separator.
size_t samples(char const *const str)
A string encoded number of samples.
Definition: clas.cpp:240
const char * sysctlname(char const *const str)
Verify that the given string only contains characters allowed in sysctl names.
Definition: clas.cpp:291
std::chrono::milliseconds ms
Millisecond type for polling intervals.
Definition: types.hpp:20
int celsius(types::decikelvin_t const val)
Converts dK into °C for display purposes.
Definition: clas.hpp:121
types::ms ival(char const *const str)
Convert string to time interval in milliseconds.
Definition: clas.cpp:217
Implements generally useful functions.
types::decikelvin_t temperature(char const *const str)
Convert string to temperature in dK.
Definition: clas.cpp:262