qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
Miscellaneous Utilities

Other small helper utilities. More...

Collaboration diagram for Miscellaneous Utilities:

Files

file  uuid.h
 Universally Unique Identifier (UUID) support.

Classes

struct  qb::nocopy
 Base class to make derived classes non-copyable. More...

Functions

bool qb::likely (bool expr)
 Hint for branch prediction when a condition is expected to be true.
bool qb::unlikely (bool expr)
 Hint for branch prediction when a condition is expected to be false.
template<typename... Types>
size_t qb::hash_combine (const Types &...args)
 Combines the hash values of multiple objects into a single hash value.
template<typename T>
std::remove_reference_t< T > && mv (T &&t) noexcept
 Alias for std::move with a concise syntax.
template<typename T>
T && fwd (std::remove_reference_t< T > &t) noexcept
 Alias for std::forward with a concise syntax (lvalue overload).
template<typename T>
T && fwd (std::remove_reference_t< T > &&t) noexcept
 Alias for std::forward with a concise syntax (rvalue overload).

Detailed Description

Other small helper utilities.

Includes `qb::nocopy`, `qb::functional`, branch prediction hints, etc.

Function Documentation

◆ likely()

bool qb::likely ( bool expr)
inline

Hint for branch prediction when a condition is expected to be true.

Parameters
exprBoolean expression to evaluate.
Returns
The result of evaluating expr.

Use this function to indicate to the compiler that the expression expr is expected to evaluate to true most of the time. This can help the compiler optimize code paths that are frequently taken, potentially improving performance by reducing branch mispredictions. Typically implemented using __builtin_expect on GCC/Clang.

if (qb::likely(common_case_condition)) {
// Optimized path for likely true
} else {
// Path for less common false case
}
bool likely(bool expr)
Hint for branch prediction when a condition is expected to be true.
Definition branch_hints.h:49

◆ unlikely()

bool qb::unlikely ( bool expr)
inline

Hint for branch prediction when a condition is expected to be false.

Parameters
exprBoolean expression to evaluate.
Returns
The result of evaluating expr.

Use this function to indicate to the compiler that the expression expr is expected to evaluate to false most of the time. This can help the compiler optimize for the more common case where the branch is not taken, or the alternative path is taken. Typically implemented using __builtin_expect on GCC/Clang.

if (qb::unlikely(error_condition)) {
// Path for rare error case
} else {
// Optimized path for likely non-error
}
bool unlikely(bool expr)
Hint for branch prediction when a condition is expected to be false.
Definition branch_hints.h:76

◆ hash_combine()

template<typename... Types>
size_t qb::hash_combine ( const Types &... args)

Combines the hash values of multiple objects into a single hash value.

Template Parameters
TypesVariadic template parameter pack of the types of objects to hash.
Parameters
argsThe values whose hash codes are to be combined.
Returns
A single size_t hash value representing the combination of all input values.

This function is particularly useful for creating custom hash functions for composite objects (structs or classes) to be used as keys in hash-based containers like qb::unordered_map or std::unordered_map. It iteratively combines the hash of each argument into a seed.

struct MyKey {
int id;
std::string name;
double value;
bool operator==(const MyKey& other) const {
return id == other.id && name == other.name && value == other.value;
}
};
namespace std {
template <>
struct hash<MyKey> {
std::size_t operator()(const MyKey& k) const {
return qb::hash_combine(k.id, k.name, k.value);
}
};
} // namespace std
// ... later ...
// qb::unordered_map<MyKey, SomeData> my_map;
size_t hash_combine(const Types &...args)
Combines the hash values of multiple objects into a single hash value.
Definition functional.h:83

◆ mv()

template<typename T>
std::remove_reference_t< T > && mv ( T && t)
inlinenoexcept

Alias for std::move with a concise syntax.

Template Parameters
TType of the value to move.
Parameters
tValue to move.
Returns
Value cast to an rvalue reference (std::remove_reference_t<T>&&).

◆ fwd() [1/2]

template<typename T>
T && fwd ( std::remove_reference_t< T > & t)
inlinenoexcept

Alias for std::forward with a concise syntax (lvalue overload).

Template Parameters
TType to forward.
Parameters
tLvalue reference to forward.
Returns
Forwarded reference, preserving value category and const/volatile qualifiers of T.

◆ fwd() [2/2]

template<typename T>
T && fwd ( std::remove_reference_t< T > && t)
inlinenoexcept

Alias for std::forward with a concise syntax (rvalue overload).

Template Parameters
TType to forward.
Parameters
tRvalue reference to forward.
Returns
Forwarded reference, preserving value category and const/volatile qualifiers of T.