$darkmode
cloe::utility::UniqueIDTracker Class Reference

#include <uid_tracker.hpp>

Public Member Functions

 UniqueIDTracker (int min, int max, int ttl=1)
 
int assign (int id)
 
void next_cycle ()
 
void add_delete_observer (void f(int in_id, int out_id))
 

Detailed Description

UniqueIDTracker maintains a mapping from objects to unique IDs, reusing objects as far as possible.

It fulfills the following requirements:

  1. InIDs are given the same OutIDs within their time to live.
  2. If there are no free IDs, the OutID with the greatest age is removed and replaced.
  3. If there is no free OutID with an age > ttl (time-to-live), then an exception is thrown.
  4. The age for all IDs is increased by a call to Next();

Currently, the implementation does not remove a mapped ID unless it has not been seen for ttl time. Therefore, if ttl is 1, then it will be reserved for one cycle it is not seen. A ttl of smaller than one makes no sense, and is considered a programmer error.

Constructor & Destructor Documentation

◆ UniqueIDTracker()

cloe::utility::UniqueIDTracker::UniqueIDTracker ( int  min,
int  max,
int  ttl = 1 
)
inline

Create a UniqueIDTracker with the minimum and maximum output ID and the maximum TTL (time-to-live) cycles for each ID after not being seen.

Member Function Documentation

◆ add_delete_observer()

void cloe::utility::UniqueIDTracker::add_delete_observer ( void   fint in_id, int out_id)
inline

Add an observer that is called with the in and output ID when that ID is removed from the map (i.e., when it expires).

◆ assign()

int cloe::utility::UniqueIDTracker::assign ( int  id)

Return a unique persistent ID across multiple cycles (steps). The input ID is a unique number representable by int (can be random for all the package cares) and is mapped to an int that is within the range specified on creation of the class (see min and max parameters).

It is not an error to assign the same input ID twice, the same output ID will be returned.

Parameters
ida unique int for an object
Returns
the unique ID mapped between min and max of this class.
Exceptions
std::range_errorwhen no more free output IDs available

◆ next_cycle()

void cloe::utility::UniqueIDTracker::next_cycle ( )

Advance to the next cycle of the assignments. This removes all mapped IDs if they have exceeded their TTL.


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