Zth (libzth)
util.h File Reference
#include <libzth/macros.h>
#include <libzth/config.h>
#include <assert.h>
#include <stdarg.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cinttypes>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <libzth/zmq.h>

Go to the source code of this file.

Classes

class  zth::cow_string
 Copy-on-write string. More...
 
class  zth::UniqueIDBase
 
class  zth::UniqueID< T, ThreadSafe >
 Keeps track of a process-wide unique ID within the type T. More...
 
struct  zth::choose_type< T, WhenTIsVoid >
 
struct  zth::choose_type< void, WhenTIsVoid >
 
struct  zth::Sequence<... >
 
class  zth::safe_ptr< T >
 Wrapper for a pointer, which checks validity of the pointer upon dereference. More...
 
class  zth::Singleton< T >
 Singleton pattern. More...
 
class  zth::ThreadLocalSingleton< T >
 Singleton pattern, but only per-thread. More...
 
class  zth::small_vector< T, Prealloc, Allocator >
 A simple std::vector, which can contain Prealloc without heap allocation. More...
 
struct  zth::smallest_uint_size< size >
 
struct  zth::smallest_uint< x, smallest_uint_size< 1 > >
 
struct  zth::smallest_uint< x, smallest_uint_size< 2 > >
 
struct  zth::smallest_uint< x, smallest_uint_size< 4 > >
 

Namespaces

 zth
 

Macros

#define ZTH_STRINGIFY_(x)   #x
 Helper for ZTH_STRINGIFY() More...
 
#define ZTH_STRINGIFY(x)   ZTH_STRINGIFY_(x)
 Converts the argument to a string literal. More...
 
#define likely(expr)   __builtin_expect(!!(expr), 1)
 Marks the given expression to likely be evaluated to true. More...
 
#define unlikely(expr)   __builtin_expect(!!(expr), 0)
 Marks the given expression to likely be evaluated to true. More...
 
#define ZTH_GET_MACRO_ARGN( _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, NAME, ...)    NAME
 Helper for FOREACH and REVERSE. More...
 
#define FOREACH_0(WHAT)
 Helper for FOREACH. More...
 
#define FOREACH_1(WHAT, X)   WHAT(X)
 Helper for FOREACH. More...
 
#define FOREACH_2(WHAT, X, ...)    WHAT(X) FOREACH_1(WHAT, __VA_ARGS__)
 Helper for FOREACH. More...
 
#define FOREACH_3(WHAT, X, ...)    WHAT(X) FOREACH_2(WHAT, __VA_ARGS__)
 Helper for FOREACH. More...
 
#define FOREACH_4(WHAT, X, ...)    WHAT(X) FOREACH_3(WHAT, __VA_ARGS__)
 Helper for FOREACH. More...
 
#define FOREACH_5(WHAT, X, ...)    WHAT(X) FOREACH_4(WHAT, __VA_ARGS__)
 Helper for FOREACH. More...
 
#define FOREACH_6(WHAT, X, ...)    WHAT(X) FOREACH_5(WHAT, __VA_ARGS__)
 Helper for FOREACH. More...
 
#define FOREACH_7(WHAT, X, ...)    WHAT(X) FOREACH_6(WHAT, __VA_ARGS__)
 Helper for FOREACH. More...
 
#define FOREACH_8(WHAT, X, ...)    WHAT(X) FOREACH_7(WHAT, __VA_ARGS__)
 Helper for FOREACH. More...
 
#define FOREACH_9(WHAT, X, ...)    WHAT(X) FOREACH_8(WHAT, __VA_ARGS__)
 Helper for FOREACH. More...
 
#define FOREACH_10(WHAT, X, ...)    WHAT(X) FOREACH_9(WHAT, __VA_ARGS__)
 Helper for FOREACH. More...
 
#define FOREACH_11(WHAT, X, ...)    WHAT(X) FOREACH_10(WHAT, __VA_ARGS__)
 Helper for FOREACH. More...
 
#define FOREACH_12(WHAT, X, ...)    WHAT(X) FOREACH_11(WHAT, __VA_ARGS__)
 Helper for FOREACH. More...
 
#define FOREACH_13(WHAT, X, ...)    WHAT(X) FOREACH_12(WHAT, __VA_ARGS__)
 Helper for FOREACH. More...
 
#define FOREACH_14(WHAT, X, ...)    WHAT(X) FOREACH_13(WHAT, __VA_ARGS__)
 Helper for FOREACH. More...
 
#define FOREACH_15(WHAT, X, ...)    WHAT(X) FOREACH_14(WHAT, __VA_ARGS__)
 Helper for FOREACH. More...
 
#define FOREACH_16(WHAT, X, ...)    WHAT(X) FOREACH_15(WHAT, __VA_ARGS__)
 Helper for FOREACH. More...
 
#define FOREACH(action, ...)
 Evaluates to action(x) for every argument. More...
 
#define REVERSE_0()
 
#define REVERSE_1(a)   a
 
#define REVERSE_2(a, ...)   REVERSE_1(__VA_ARGS__), a
 
#define REVERSE_3(a, ...)   REVERSE_2(__VA_ARGS__), a
 
#define REVERSE_4(a, ...)   REVERSE_3(__VA_ARGS__), a
 
#define REVERSE_5(a, ...)   REVERSE_4(__VA_ARGS__), a
 
#define REVERSE_6(a, ...)   REVERSE_5(__VA_ARGS__), a
 
#define REVERSE_7(a, ...)   REVERSE_6(__VA_ARGS__), a
 
#define REVERSE_8(a, ...)   REVERSE_7(__VA_ARGS__), a
 
#define REVERSE_9(a, ...)   REVERSE_8(__VA_ARGS__), a
 
#define REVERSE_10(a, ...)   REVERSE_9(__VA_ARGS__), a
 
#define REVERSE_11(a, ...)   REVERSE_10(__VA_ARGS__), a
 
#define REVERSE_12(a, ...)   REVERSE_11(__VA_ARGS__), a
 
#define REVERSE_13(a, ...)   REVERSE_12(__VA_ARGS__), a
 
#define REVERSE_14(a, ...)   REVERSE_13(__VA_ARGS__), a
 
#define REVERSE_15(a, ...)   REVERSE_14(__VA_ARGS__), a
 
#define REVERSE_16(a, ...)   REVERSE_15(__VA_ARGS__), a
 
#define REVERSE(...)
 
#define ZTH_DBG_PREFIX   " > "
 Prefix for every zth_dbg() call. More...
 
#define zth_dbg(group, fmt, a...)
 Debug printf()-like function. More...
 
#define zth_assert(expr)
 assert(), but better integrated in Zth. More...
 
#define ZTH_CLASS_NOCOPY(Class)
 

Typedefs

typedef std::basic_string< char, std::char_traits< char >, Config::Allocator< char >::type > zth::string
 std::string type using Config::Allocator::type. More...
 

Functions

void zth_logv (char const *fmt, va_list arg)
 Prints the given printf()-like formatted string to stdout. More...
 
char const * zth::banner () noexcept
 Prints a banner line with version and configuration information. More...
 
void zth::abort (char const *fmt,...) noexcept
 Aborts the process after printing the given printf() formatted message. More...
 
void zth::abortv (char const *fmt, va_list args) noexcept
 Aborts the process after printing the given printf() formatted message. More...
 
void zth::assert_handler (char const *file, int line, char const *expr)
 
bool zth::log_supports_ansi_colors () noexcept
 Returns if the system supports ANSI colors. More...
 
void zth::log_colorv (int color, char const *fmt, va_list args)
 Logs a given printf()-like formatted string using an ANSI color code. More...
 
void zth::log_color (int color, char const *fmt,...)
 Logs a given printf()-like formatted string using an ANSI color code. More...
 
void zth::logv (char const *fmt, va_list arg)
 Logs a given printf()-like formatted string. More...
 
void zth::log (char const *fmt,...)
 Logs a given printf()-like formatted string. More...
 
string zth::formatv (char const *fmt, va_list args)
 Format like vsprintf(), but save the result in an zth::string. More...
 
string zth::format (char const *fmt,...)
 Format like sprintf(), but save the result in an zth::string. More...
 
template<typename T >
cow_string zth::str (T value)
 Returns an zth::string representation of the given value. More...
 
template<>
cow_string zth::str< char > (char value)
 
template<>
cow_string zth::str< signed char > (signed char value)
 
template<>
cow_string zth::str< unsigned char > (unsigned char value)
 
template<>
cow_string zth::str< short > (short value)
 
template<>
cow_string zth::str< unsigned short > (unsigned short value)
 
template<>
cow_string zth::str< int > (int value)
 
template<>
cow_string zth::str< unsigned int > (unsigned int value)
 
template<>
cow_string zth::str< long > (long value)
 
template<>
cow_string zth::str< unsigned long > (unsigned long value)
 
template<>
cow_string zth::str< long long > (long long value)
 
template<>
cow_string zth::str< unsigned long long > (unsigned long long value)
 
template<>
cow_string zth::str< float > (float value)
 
template<>
cow_string zth::str< double > (double value)
 
template<>
cow_string zth::str< long double > (long double value)
 
template<>
cow_string zth::str< string && > (string &&value)
 
string zth::err (int e)
 Return a string like strerror() does, but as a zth::string. More...
 
template<>
cow_string zth::str< UniqueIDBase const & > (UniqueIDBase const &value)
 
void zth_banner ()
 Prints a banner line with version and configuration information. More...
 
void zth_abort (char const *fmt,...)
 Aborts the process after printing the given printf() formatted message. More...
 
void zth_log_color (int color, char const *fmt,...)
 Logs a given printf()-like formatted string using an ANSI color code. More...
 
void zth_log (char const *fmt,...)
 Logs a given printf()-like formatted string. More...
 

Macro Definition Documentation

◆ FOREACH

#define FOREACH (   action,
  ... 
)
Value:
0, ##__VA_ARGS__, FOREACH_16, FOREACH_15, FOREACH_14, FOREACH_13, \
(action, ##__VA_ARGS__)
constexpr auto action(T &&a, char const *name=nullptr)
Create an action from a function.
Definition: fsm14.h:649
#define ZTH_GET_MACRO_ARGN( _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, NAME,...)
Helper for FOREACH and REVERSE.
Definition: util.h:69
#define FOREACH_3(WHAT, X,...)
Helper for FOREACH.
Definition: util.h:79
#define FOREACH_11(WHAT, X,...)
Helper for FOREACH.
Definition: util.h:103
#define FOREACH_9(WHAT, X,...)
Helper for FOREACH.
Definition: util.h:97
#define FOREACH_4(WHAT, X,...)
Helper for FOREACH.
Definition: util.h:82
#define FOREACH_13(WHAT, X,...)
Helper for FOREACH.
Definition: util.h:109
#define FOREACH_5(WHAT, X,...)
Helper for FOREACH.
Definition: util.h:85
#define FOREACH_8(WHAT, X,...)
Helper for FOREACH.
Definition: util.h:94
#define FOREACH_16(WHAT, X,...)
Helper for FOREACH.
Definition: util.h:118
#define FOREACH_12(WHAT, X,...)
Helper for FOREACH.
Definition: util.h:106
#define FOREACH_0(WHAT)
Helper for FOREACH.
Definition: util.h:74
#define FOREACH_6(WHAT, X,...)
Helper for FOREACH.
Definition: util.h:88
#define FOREACH_2(WHAT, X,...)
Helper for FOREACH.
Definition: util.h:76
#define FOREACH_7(WHAT, X,...)
Helper for FOREACH.
Definition: util.h:91
#define FOREACH_15(WHAT, X,...)
Helper for FOREACH.
Definition: util.h:115
#define FOREACH_10(WHAT, X,...)
Helper for FOREACH.
Definition: util.h:100
#define FOREACH_14(WHAT, X,...)
Helper for FOREACH.
Definition: util.h:112
#define FOREACH_1(WHAT, X)
Helper for FOREACH.
Definition: util.h:75

Evaluates to action(x) for every argument.

Definition at line 126 of file util.h.

◆ FOREACH_0

#define FOREACH_0 (   WHAT)

Helper for FOREACH.

Definition at line 74 of file util.h.

◆ FOREACH_1

#define FOREACH_1 (   WHAT,
 
)    WHAT(X)

Helper for FOREACH.

Definition at line 75 of file util.h.

◆ FOREACH_10

#define FOREACH_10 (   WHAT,
  X,
  ... 
)     WHAT(X) FOREACH_9(WHAT, __VA_ARGS__)

Helper for FOREACH.

Definition at line 102 of file util.h.

◆ FOREACH_11

#define FOREACH_11 (   WHAT,
  X,
  ... 
)     WHAT(X) FOREACH_10(WHAT, __VA_ARGS__)

Helper for FOREACH.

Definition at line 105 of file util.h.

◆ FOREACH_12

#define FOREACH_12 (   WHAT,
  X,
  ... 
)     WHAT(X) FOREACH_11(WHAT, __VA_ARGS__)

Helper for FOREACH.

Definition at line 108 of file util.h.

◆ FOREACH_13

#define FOREACH_13 (   WHAT,
  X,
  ... 
)     WHAT(X) FOREACH_12(WHAT, __VA_ARGS__)

Helper for FOREACH.

Definition at line 111 of file util.h.

◆ FOREACH_14

#define FOREACH_14 (   WHAT,
  X,
  ... 
)     WHAT(X) FOREACH_13(WHAT, __VA_ARGS__)

Helper for FOREACH.

Definition at line 114 of file util.h.

◆ FOREACH_15

#define FOREACH_15 (   WHAT,
  X,
  ... 
)     WHAT(X) FOREACH_14(WHAT, __VA_ARGS__)

Helper for FOREACH.

Definition at line 117 of file util.h.

◆ FOREACH_16

#define FOREACH_16 (   WHAT,
  X,
  ... 
)     WHAT(X) FOREACH_15(WHAT, __VA_ARGS__)

Helper for FOREACH.

Definition at line 120 of file util.h.

◆ FOREACH_2

#define FOREACH_2 (   WHAT,
  X,
  ... 
)     WHAT(X) FOREACH_1(WHAT, __VA_ARGS__)

Helper for FOREACH.

Definition at line 78 of file util.h.

◆ FOREACH_3

#define FOREACH_3 (   WHAT,
  X,
  ... 
)     WHAT(X) FOREACH_2(WHAT, __VA_ARGS__)

Helper for FOREACH.

Definition at line 81 of file util.h.

◆ FOREACH_4

#define FOREACH_4 (   WHAT,
  X,
  ... 
)     WHAT(X) FOREACH_3(WHAT, __VA_ARGS__)

Helper for FOREACH.

Definition at line 84 of file util.h.

◆ FOREACH_5

#define FOREACH_5 (   WHAT,
  X,
  ... 
)     WHAT(X) FOREACH_4(WHAT, __VA_ARGS__)

Helper for FOREACH.

Definition at line 87 of file util.h.

◆ FOREACH_6

#define FOREACH_6 (   WHAT,
  X,
  ... 
)     WHAT(X) FOREACH_5(WHAT, __VA_ARGS__)

Helper for FOREACH.

Definition at line 90 of file util.h.

◆ FOREACH_7

#define FOREACH_7 (   WHAT,
  X,
  ... 
)     WHAT(X) FOREACH_6(WHAT, __VA_ARGS__)

Helper for FOREACH.

Definition at line 93 of file util.h.

◆ FOREACH_8

#define FOREACH_8 (   WHAT,
  X,
  ... 
)     WHAT(X) FOREACH_7(WHAT, __VA_ARGS__)

Helper for FOREACH.

Definition at line 96 of file util.h.

◆ FOREACH_9

#define FOREACH_9 (   WHAT,
  X,
  ... 
)     WHAT(X) FOREACH_8(WHAT, __VA_ARGS__)

Helper for FOREACH.

Definition at line 99 of file util.h.

◆ likely

#define likely (   expr)    __builtin_expect(!!(expr), 1)

Marks the given expression to likely be evaluated to true.

This may help compiler optimization.

Returns
the evaluated expr

Definition at line 42 of file util.h.

◆ REVERSE

#define REVERSE (   ...)
Value:
0, ##__VA_ARGS__, REVERSE_16, REVERSE_15, REVERSE_14, REVERSE_13, \
(__VA_ARGS__)
#define REVERSE_16(a,...)
Definition: util.h:152
#define REVERSE_4(a,...)
Definition: util.h:140
#define REVERSE_6(a,...)
Definition: util.h:142
#define REVERSE_12(a,...)
Definition: util.h:148
#define REVERSE_7(a,...)
Definition: util.h:143
#define REVERSE_11(a,...)
Definition: util.h:147
#define REVERSE_1(a)
Definition: util.h:137
#define REVERSE_0()
Definition: util.h:136
#define REVERSE_9(a,...)
Definition: util.h:145
#define REVERSE_15(a,...)
Definition: util.h:151
#define REVERSE_8(a,...)
Definition: util.h:144
#define REVERSE_14(a,...)
Definition: util.h:150
#define REVERSE_2(a,...)
Definition: util.h:138
#define REVERSE_10(a,...)
Definition: util.h:146
#define REVERSE_5(a,...)
Definition: util.h:141
#define REVERSE_3(a,...)
Definition: util.h:139
#define REVERSE_13(a,...)
Definition: util.h:149

Definition at line 153 of file util.h.

◆ REVERSE_0

#define REVERSE_0 ( )

Definition at line 136 of file util.h.

◆ REVERSE_1

#define REVERSE_1 (   a)    a

Definition at line 137 of file util.h.

◆ REVERSE_10

#define REVERSE_10 (   a,
  ... 
)    REVERSE_9(__VA_ARGS__), a

Definition at line 146 of file util.h.

◆ REVERSE_11

#define REVERSE_11 (   a,
  ... 
)    REVERSE_10(__VA_ARGS__), a

Definition at line 147 of file util.h.

◆ REVERSE_12

#define REVERSE_12 (   a,
  ... 
)    REVERSE_11(__VA_ARGS__), a

Definition at line 148 of file util.h.

◆ REVERSE_13

#define REVERSE_13 (   a,
  ... 
)    REVERSE_12(__VA_ARGS__), a

Definition at line 149 of file util.h.

◆ REVERSE_14

#define REVERSE_14 (   a,
  ... 
)    REVERSE_13(__VA_ARGS__), a

Definition at line 150 of file util.h.

◆ REVERSE_15

#define REVERSE_15 (   a,
  ... 
)    REVERSE_14(__VA_ARGS__), a

Definition at line 151 of file util.h.

◆ REVERSE_16

#define REVERSE_16 (   a,
  ... 
)    REVERSE_15(__VA_ARGS__), a

Definition at line 152 of file util.h.

◆ REVERSE_2

#define REVERSE_2 (   a,
  ... 
)    REVERSE_1(__VA_ARGS__), a

Definition at line 138 of file util.h.

◆ REVERSE_3

#define REVERSE_3 (   a,
  ... 
)    REVERSE_2(__VA_ARGS__), a

Definition at line 139 of file util.h.

◆ REVERSE_4

#define REVERSE_4 (   a,
  ... 
)    REVERSE_3(__VA_ARGS__), a

Definition at line 140 of file util.h.

◆ REVERSE_5

#define REVERSE_5 (   a,
  ... 
)    REVERSE_4(__VA_ARGS__), a

Definition at line 141 of file util.h.

◆ REVERSE_6

#define REVERSE_6 (   a,
  ... 
)    REVERSE_5(__VA_ARGS__), a

Definition at line 142 of file util.h.

◆ REVERSE_7

#define REVERSE_7 (   a,
  ... 
)    REVERSE_6(__VA_ARGS__), a

Definition at line 143 of file util.h.

◆ REVERSE_8

#define REVERSE_8 (   a,
  ... 
)    REVERSE_7(__VA_ARGS__), a

Definition at line 144 of file util.h.

◆ REVERSE_9

#define REVERSE_9 (   a,
  ... 
)    REVERSE_8(__VA_ARGS__), a

Definition at line 145 of file util.h.

◆ unlikely

#define unlikely (   expr)    __builtin_expect(!!(expr), 0)

Marks the given expression to likely be evaluated to true.

This may help compiler optimization.

Returns
the evaluated expr

Definition at line 56 of file util.h.

◆ zth_assert

#define zth_assert (   expr)
Value:
do { \
__FILE__, __LINE__, \
? ZTH_STRINGIFY(expr) \
: nullptr); \
} while(false)
void assert_handler(char const *file, int line, char const *expr)
Definition: assert.cpp:17
static bool const EnableAssert
When true, enable zth_assert().
Definition: config.h:58
static bool const EnableFullAssert
Show failing expression in case of a failed assert.
Definition: config.h:69
#define ZTH_STRINGIFY(x)
Converts the argument to a string literal.
Definition: util.h:32
#define unlikely(expr)
Marks the given expression to likely be evaluated to true.
Definition: util.h:56

assert(), but better integrated in Zth.

Examples
zmq.cpp.

Definition at line 236 of file util.h.

◆ ZTH_CLASS_NOCOPY

#define ZTH_CLASS_NOCOPY (   Class)
Value:
public: \
Class(Class const&) = delete; \
Class(Class&&) = \
delete; /* NOLINT(misc-macro-parentheses,bugprone-macro-parentheses) \
*/ \
Class& operator=(Class const&) noexcept = delete; \
Class& operator=(Class&&) noexcept = \
delete; /* NOLINT(misc-macro-parentheses,bugprone-macro-parentheses) \
*/ \
private:

Definition at line 254 of file util.h.

◆ ZTH_DBG_PREFIX

#define ZTH_DBG_PREFIX   " > "

Prefix for every zth_dbg() call.

Definition at line 198 of file util.h.

◆ ZTH_GET_MACRO_ARGN

#define ZTH_GET_MACRO_ARGN (   _0,
  _1,
  _2,
  _3,
  _4,
  _5,
  _6,
  _7,
  _8,
  _9,
  _10,
  _11,
  _12,
  _13,
  _14,
  _15,
  _16,
  NAME,
  ... 
)     NAME

Helper for FOREACH and REVERSE.

Definition at line 69 of file util.h.

◆ ZTH_STRINGIFY

#define ZTH_STRINGIFY (   x)    ZTH_STRINGIFY_(x)

Converts the argument to a string literal.

Definition at line 32 of file util.h.

◆ ZTH_STRINGIFY_

#define ZTH_STRINGIFY_ (   x)    #x

Helper for ZTH_STRINGIFY()

Definition at line 28 of file util.h.