kmMqtt 0.0.1
MQTT Client Library
Loading...
Searching...
No Matches
kmMqtt::UniqueFunction Class Reference

A move-only type-erased callable wrapper with small buffer optimization. More...

#include <UniqueFunction.h>

Public Member Functions

template<typename TFunc>
 UniqueFunction (TFunc &&func)
 Constructs UniqueFunction from any callable object.

Detailed Description

A move-only type-erased callable wrapper with small buffer optimization.

UniqueFunction allows storing and invoking any callable object (such as lambdas, functors, or function pointers) with a void() signature. It is move-only and cannot be copied. The implementation allows the use of small buffer optimization (SBO) when ENABLE_UNQIUEFUNCTION_SBO is defined.

Memory management:

  • If defined ENABLE_UNQIUEFUNCTION_SBO then small callables (≤ UNIQUEFUNCTION_SBO_MAX_SIZE bytes) are stored in an internal buffer.
  • Larger callables are stored on the heap.
  • Move operations preserve the storage strategy of the source object.

Exception guarantee: The call operator provides strong noexcept guarantee.

Example usage:

kmMqtt::UniqueFunction f = [](){ std::cout << "Hello!"; };
f(); //Invokes the stored callable
A move-only type-erased callable wrapper with small buffer optimization.
Definition UniqueFunction.h:36
Note
Only callables with a void() signature are supported.
Copying is disabled, only move operations are allowed.

Constructor & Destructor Documentation

◆ UniqueFunction()

template<typename TFunc>
kmMqtt::UniqueFunction::UniqueFunction ( TFunc && func)
inline

Constructs UniqueFunction from any callable object.

Note
Calls a private constructor that decides whether to use small buffer optimization based on the size and alignment of the callable by determining if it fits within the predefined constraints at compile time (std::integral_constant...)
Template Parameters
TFuncThe type of the callable object.
Parameters
funcThe callable object to store.

The documentation for this class was generated from the following file: