Zth (libzth)
Loading...
Searching...
No Matches
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 <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

namespace  zth
 
namespace  zth::impl
 

Macros

#define ZTH_STRINGIFY_(x)   #x
 Helper for ZTH_STRINGIFY()
 
#define ZTH_STRINGIFY(x)   ZTH_STRINGIFY_(x)
 Converts the argument to a string literal.
 
#define likely(expr)    __builtin_expect(!!(expr) /* NOLINT(readability-simplify-boolean-expr) */, 1)
 Marks the given expression to likely be evaluated to true.
 
#define unlikely(expr)    __builtin_expect(!!(expr) /* NOLINT(readability-simplify-boolean-expr) */, 0)
 Marks the given expression to likely be evaluated to true.
 
#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.
 
#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.
 
#define zth_dbg(group, fmt, a...)
 Debug printf()-like function.
 
#define zth_assert(expr)
 assert(), but better integrated in Zth.
 
#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.
 

Functions

void zth_logv (char const *fmt, va_list arg)
 Prints the given printf()-like formatted string to stdout.
 
char const * zth::banner () noexcept
 Prints a banner line with version and configuration information.
 
void zth::abort (char const *fmt,...) noexcept
 Aborts the process after printing the given printf() formatted message.
 
void zth::abortv (char const *fmt, va_list args) noexcept
 Aborts the process after printing the given printf() formatted message.
 
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.
 
void zth::log_colorv (int color, char const *fmt, va_list args)
 Logs a given printf()-like formatted string using an ANSI color code.
 
void zth::log_color (int color, char const *fmt,...)
 Logs a given printf()-like formatted string using an ANSI color code.
 
void zth::logv (char const *fmt, va_list arg)
 Logs a given printf()-like formatted string.
 
void zth::log (char const *fmt,...)
 Logs a given printf()-like formatted string.
 
string zth::formatv (char const *fmt, va_list args)
 Format like vsprintf(), but save the result in an zth::string.
 
string zth::format (char const *fmt,...)
 Format like sprintf(), but save the result in an zth::string.
 
template<typename T >
cow_string zth::str (T value)
 Returns an zth::string representation of the given value.
 
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.
 
template<>
cow_string zth::str< UniqueIDBase const & > (UniqueIDBase const &value)
 
void zth_banner ()
 Prints a banner line with version and configuration information.
 
void zth_abort (char const *fmt,...)
 Aborts the process after printing the given printf() formatted message.
 
void zth_log_color (int color, char const *fmt,...)
 Logs a given printf()-like formatted string using an ANSI color code.
 
void zth_log (char const *fmt,...)
 Logs a given printf()-like formatted string.
 

Macro Definition Documentation

◆ likely

#define likely (   expr)     __builtin_expect(!!(expr) /* NOLINT(readability-simplify-boolean-expr) */, 1)

Marks the given expression to likely be evaluated to true.

This may help compiler optimization.

Returns
the evaluated expr

Definition at line 40 of file util.h.

◆ REVERSE

#define REVERSE (   ...)
Value:
(__VA_ARGS__)
#define REVERSE_16(a,...)
Definition util.h:132
#define REVERSE_4(a,...)
Definition util.h:120
#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:65
#define REVERSE_6(a,...)
Definition util.h:122
#define REVERSE_12(a,...)
Definition util.h:128
#define REVERSE_7(a,...)
Definition util.h:123
#define REVERSE_11(a,...)
Definition util.h:127
#define REVERSE_1(a)
Definition util.h:117
#define REVERSE_0()
Definition util.h:116
#define REVERSE_9(a,...)
Definition util.h:125
#define REVERSE_15(a,...)
Definition util.h:131
#define REVERSE_8(a,...)
Definition util.h:124
#define REVERSE_14(a,...)
Definition util.h:130
#define REVERSE_2(a,...)
Definition util.h:118
#define REVERSE_10(a,...)
Definition util.h:126
#define REVERSE_5(a,...)
Definition util.h:121
#define REVERSE_3(a,...)
Definition util.h:119
#define REVERSE_13(a,...)
Definition util.h:129

Definition at line 133 of file util.h.

◆ REVERSE_0

#define REVERSE_0 ( )

Definition at line 116 of file util.h.

◆ REVERSE_1

#define REVERSE_1 (   a)    a

Definition at line 117 of file util.h.

◆ REVERSE_10

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

Definition at line 126 of file util.h.

◆ REVERSE_11

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

Definition at line 127 of file util.h.

◆ REVERSE_12

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

Definition at line 128 of file util.h.

◆ REVERSE_13

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

Definition at line 129 of file util.h.

◆ REVERSE_14

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

Definition at line 130 of file util.h.

◆ REVERSE_15

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

Definition at line 131 of file util.h.

◆ REVERSE_16

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

Definition at line 132 of file util.h.

◆ REVERSE_2

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

Definition at line 118 of file util.h.

◆ REVERSE_3

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

Definition at line 119 of file util.h.

◆ REVERSE_4

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

Definition at line 120 of file util.h.

◆ REVERSE_5

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

Definition at line 121 of file util.h.

◆ REVERSE_6

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

Definition at line 122 of file util.h.

◆ REVERSE_7

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

Definition at line 123 of file util.h.

◆ REVERSE_8

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

Definition at line 124 of file util.h.

◆ REVERSE_9

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

Definition at line 125 of file util.h.

◆ unlikely

#define unlikely (   expr)     __builtin_expect(!!(expr) /* NOLINT(readability-simplify-boolean-expr) */, 0)

Marks the given expression to likely be evaluated to true.

This may help compiler optimization.

Returns
the evaluated expr

Definition at line 55 of file util.h.

◆ zth_assert

#define zth_assert (   expr)
Value:
do { /* NOLINT(cppcoreguidelines-avoid-do-while) */ \
::zth::assert_handler( \
__FILE__, __LINE__, \
: nullptr); \
} while(false)
static bool const EnableAssert
When true, enable zth_assert().
Definition config.h:64
static bool const EnableFullAssert
Show failing expression in case of a failed assert.
Definition config.h:75
#define ZTH_STRINGIFY(x)
Converts the argument to a string literal.
Definition util.h:30
#define unlikely(expr)
Marks the given expression to likely be evaluated to true.
Definition util.h:55

assert(), but better integrated in Zth.

Examples
zmq.cpp.

Definition at line 212 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 229 of file util.h.

◆ ZTH_DBG_PREFIX

#define ZTH_DBG_PREFIX   " > "

Prefix for every zth_dbg() call.

Definition at line 177 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 65 of file util.h.

◆ ZTH_STRINGIFY

#define ZTH_STRINGIFY (   x)    ZTH_STRINGIFY_(x)

Converts the argument to a string literal.

Definition at line 30 of file util.h.

◆ ZTH_STRINGIFY_

#define ZTH_STRINGIFY_ (   x)    #x

Helper for ZTH_STRINGIFY()

Definition at line 26 of file util.h.