powerd++  0.4.4
Namespaces | Classes | Functions
utility Namespace Reference

A collection of generally useful functions. More...

Namespaces

 literals
 Contains literal operators.
 

Classes

class  Formatter
 A formatting wrapper around string literals. More...
 
struct  FromChars
 A functor for reading numerical values from a string or character array. More...
 
class  Max
 A simple value container that provides the maximum of assigned values. More...
 
class  Min
 A simple value container that provides the minimum of assigned values. More...
 
class  Sum
 A simple value container only allowing += and copy assignment. More...
 
struct  Underlined
 A line of text and an underlining line. More...
 

Functions

template<typename T , size_t Count>
constexpr size_t countof (T(&)[Count])
 Like sizeof(), but it returns the number of elements an array consists of instead of the number of bytes. More...
 
template<typename... Args>
void sprintf (Args...)
 This is a safeguard against accidentally using sprintf(). More...
 
template<size_t Size, typename... Args>
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. More...
 
template<class ET , typename VT = typename std::underlying_type<ET>::type>
constexpr VT to_value (ET const op)
 Casts an enum to its underlying value. More...
 
Underlined highlight (std::string const &str, ptrdiff_t const offs, ptrdiff_t const len=1)
 Underline the given number of characters. More...
 

Detailed Description

A collection of generally useful functions.

Function Documentation

◆ countof()

template<typename T , size_t Count>
constexpr size_t utility::countof ( T(&)  [Count])
constexpr

Like sizeof(), but it returns the number of elements an array consists of instead of the number of bytes.

Template Parameters
T,CountThe type and number of array elements
Returns
The number of array entries

◆ highlight()

utility::Underlined utility::highlight ( std::string const &  str,
ptrdiff_t const  offs,
ptrdiff_t const  len = 1 
)

Underline the given number of characters.

The given length and offset use byte-addressing, the resulting text is sanitised for printing, which may affect the actual number of underlining characters:

  • Control characters, multi-byte character fragments and invalid code points are substituted by printf-style escapes
  • Multi-byte characters are underlined with a single character

Double width characters are not supported (i.e. the resulting underline is too short).

The underlining string is only as long as it needs to be, i.e. it is not right-padded with white space.

Parameters
strThe string to sanitise and underline
offs,lenThe byte-offset and length of the underline
Returns
The sanitised text and the underline

◆ sprintf()

template<typename... Args>
void utility::sprintf ( Args...  )
inline

This is a safeguard against accidentally using sprintf().

Using it triggers a static_assert(), preventing compilation.

Template Parameters
ArgsCatch all arguments

◆ sprintf_safe()

template<size_t Size, typename... Args>
int utility::sprintf_safe ( char(&)  dst[Size],
char const *const  format,
Args const ...  args 
)
inline

A wrapper around snprintf() that automatically pulls in the destination buffer size.

Template Parameters
SizeThe destination buffer size
ArgsThe types of the arguments
Parameters
dstA reference to the destination buffer
formatA printf style formatting string
argsThe printf arguments
Returns
The number of characters in the resulting string, regardless of the available space

◆ to_value()

template<class ET , typename VT = typename std::underlying_type<ET>::type>
constexpr VT utility::to_value ( ET const  op)
constexpr

Casts an enum to its underlying value.

Template Parameters
ET,VTThe enum and value type
Parameters
opThe operand to convert
Returns
The integer representation of the operand