dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
dfx::Utils::MonotonicIdAllocator< T > Class Template Reference

Monotonically increasing ID allocator. More...

#include <dfx-utilities/MonotonicIdAllocator.hpp>

Public Types

using value_type = T
 Type used for allocated identifiers.

Public Member Functions

value_type next () noexcept
 Allocate and return the next identifier.
void reset () noexcept
 Reset the allocator counter back to zero.

Detailed Description

template<std::integral T>
class dfx::Utils::MonotonicIdAllocator< T >

Monotonically increasing ID allocator.

MonotonicIdAllocator hands out numeric identifiers by incrementing an internal counter.

The allocator is intentionally simple:

  • IDs are never recycled (there is no free() / release()).
  • No overflow protection is provided.
  • No persistence across process restarts.

IDs are guaranteed to be unique for the lifetime of the allocator instance as long as reset() is not called and the counter does not overflow.

Template Parameters
TIntegral type used for identifiers (e.g. uint32_t, uint64_t).
Note
This class is not inherently thread-safe: concurrent calls to next() and/or reset() require external synchronization.
Warning
When the underlying counter overflows, identifiers wrap around and uniqueness is lost. It is the caller’s responsibility to select a wide enough type and/or enforce a limit that prevents overflow.
Complexity
next() and reset() are O(1) and noexcept.
Example
auto const a = ids.next(); // 0
auto const b = ids.next(); // 1
// ...
Monotonically increasing ID allocator.
Definition MonotonicIdAllocator.hpp:52
value_type next() noexcept
Allocate and return the next identifier.
Definition MonotonicIdAllocator.hpp:64

Member Typedef Documentation

◆ value_type

template<std::integral T>
using dfx::Utils::MonotonicIdAllocator< T >::value_type = T

Type used for allocated identifiers.

Member Function Documentation

◆ next()

template<std::integral T>
value_type dfx::Utils::MonotonicIdAllocator< T >::next ( )
inlinenoexcept

Allocate and return the next identifier.

Returns the current counter value and increments it afterward (post-increment). The first call returns 0.

Returns
A new identifier value.

◆ reset()

template<std::integral T>
void dfx::Utils::MonotonicIdAllocator< T >::reset ( )
inlinenoexcept

Reset the allocator counter back to zero.

After calling reset(), subsequent calls to next() will start again from 0, which can create duplicates relative to IDs previously handed out by this same allocator instance.

Use only when you can guarantee that no previously issued IDs are still in use (or when duplicates are acceptable for your use-case).


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