Go to the documentation of this file.
14 #ifndef _POWERDXX_UTILITY_HPP_
15 #define _POWERDXX_UTILITY_HPP_
31 template <
typename T,
size_t Count>
32 constexpr
size_t countof(T (&)[Count]) {
return Count; }
42 template <
typename... Args>
46 static_assert(
sizeof...(Args) &&
false,
47 "Use of sprintf() is unsafe, use sprintf_safe() instead");
68 template <
size_t Size,
typename... Args>
69 inline int sprintf_safe(
char (& dst)[Size],
char const *
const format,
71 return snprintf(dst, Size, format, args...);
84 template <class ET, typename VT = typename std::underlying_type<ET>::type>
86 return static_cast<VT
>(op);
105 template <
size_t BufSize>
129 template <
typename... ArgTs>
136 }
else if (
static_cast<size_t>(count) >= BufSize) {
138 return {buf, BufSize - 1};
140 return {buf,
static_cast<size_t>(count)};
167 template <
typename T>
195 constexpr
operator T
const &()
const {
208 this->value +=
value;
219 template <
typename T>
242 constexpr
operator T
const &()
const {
266 template <
typename T>
289 constexpr
operator T
const &()
const {
343 template <
typename T>
348 auto [p, ec] = std::from_chars(this->it, this->end, dst);
352 for (; p != this->end && std::isspace(*p); ++p);
365 operator bool()
const {
366 return this->it && this->end != this->it && *this->
it;
377 for (; this->it !=
end && std::isspace(*this->it); ++this->
it);
391 template <
size_t CountV>
392 FromChars(
char const (& str)[CountV],
bool const terminator =
true) :
393 FromChars{str, str + CountV - terminator} {}
406 FromChars{str.data(), str.data() + str.size()} {}
431 operator std::string()
const {
432 return this->text +
'\n' + this->
line;
460 Underlined
highlight(std::string
const & str, ptrdiff_t
const offs,
461 ptrdiff_t
const len = 1);
std::string line
Aligned underlining characters ^~~~.
Definition: utility.hpp:424
FromChars(std::string const &str)
Construct functor from a string.
Definition: utility.hpp:405
A simple value container that provides the maximum of assigned values.
Definition: utility.hpp:267
A collection of generally useful functions.
Definition: utility.hpp:20
void sprintf(Args...)
This is a safeguard against accidentally using sprintf().
Definition: utility.hpp:43
constexpr Max(T const &value)
Construct from an initial value.
Definition: utility.hpp:281
constexpr size_t countof(T(&)[Count])
Like sizeof(), but it returns the number of elements an array consists of instead of the number of by...
Definition: utility.hpp:32
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
T value
The sum of values accumulated.
Definition: utility.hpp:173
bool operator()(T &dst)
Retrieve an integral or floating point value from the array.
Definition: utility.hpp:344
constexpr VT to_value(ET const op)
Casts an enum to its underlying value.
Definition: utility.hpp:85
constexpr Min & operator=(T const &value)
Assign a new value, if it is less than the current value.
Definition: utility.hpp:254
constexpr Sum & operator+=(T const &value)
Add a value to the sum.
Definition: utility.hpp:207
constexpr Max & operator=(T const &value)
Assign a new value, if it is greater than the current value.
Definition: utility.hpp:301
T value
The minimum of the assigned values.
Definition: utility.hpp:225
A line of text and an underlining line.
Definition: utility.hpp:415
const char * it
The next character to read.
Definition: utility.hpp:315
std::string text
The text with printf-style escapes.
Definition: utility.hpp:419
A simple value container only allowing += and copy assignment.
Definition: utility.hpp:168
constexpr Sum()
Default construct.
Definition: utility.hpp:187
const char *const end
The first character of the same array that may not be read, this should usually point to a terminatin...
Definition: utility.hpp:322
constexpr Sum(T const &value)
Construct from an initial value.
Definition: utility.hpp:182
FromChars(char const *const start, char const *const end)
Range based constructor.
Definition: utility.hpp:375
FromChars(char const (&str)[CountV], bool const terminator=true)
Construct from a character array.
Definition: utility.hpp:392
constexpr Min(T const &value)
Construct from an initial value.
Definition: utility.hpp:234
T value
The maximum of the assigned values.
Definition: utility.hpp:272
int sprintf_safe(char(&dst)[Size], char const *const format, Args const ... args)
A wrapper around snprintf() that automatically pulls in the destination buffer size.
Definition: utility.hpp:69
A simple value container that provides the minimum of assigned values.
Definition: utility.hpp:220
A functor for reading numerical values from a string or character array.
Definition: utility.hpp:311