Template Class timeout_queue_t

Nested Relationships

Nested Types

Inheritance Relationships

Base Type

  • public std::list< pair_t< type, timer_t > >

Class Documentation

template<typename type>
class timeout_queue_t : public std::list<pair_t<type, timer_t>>

Timeout-aware queue wrapper around std::list.

Timeout checks are performed during iteration. Expired items are collected in a side buffer and can be retrieved with timed_out().

Thread-safety is managed manually via read_lock()/unlock_read() and lock()/unlock().

Example: timeout_queue_t<item_t> q(duration_t::from_s(5)); q.read_lock(); for (const auto& v : q) { ... } q.unlock_read(); auto expired = q.timed_out();

Template Parameters:

type – Type of elements stored in the queue.

Public Types

using shared_ptr = std::shared_ptr<timeout_queue_t<type>>

Shared pointer type.

Public Functions

inline timeout_queue_t(duration_t timeOut_ = duration_t::from_s(10))

Construct a new timeout_queue_t object.

Parameters:

timeOut_ – Maximum allowed age for each element in the queue.

inline iterator begin()

Returns iterator to the beginning (first non-expired element)

Returns:

Iterator to the first non-expired element.

inline iterator end()

Returns iterator to the end.

Returns:

Iterator representing the end sentinel.

inline const_iterator begin() const

Returns const_iterator to the beginning (first non-expired element)

Returns:

Const iterator to the first non-expired element.

inline const_iterator end() const

Returns const_iterator to the end.

Returns:

Const iterator representing the end sentinel.

inline const_iterator cbegin() const

Returns const_iterator to the beginning (first non-expired element)

Returns:

Const iterator to the first non-expired element.

inline const_iterator cend() const

Returns const_iterator to the end.

Returns:

Const iterator representing the end sentinel.

inline void insert(const type &item_)

Insert item into the queue with current time.

Parameters:

item_ – Element inserted with a fresh timestamp.

inline void clear(const vector_t<type> &elements_)

Remove elements from the queue that match the given elements.

Parameters:

elements_ – Values to remove from the queue when matched by equality.

inline void clear()

Clear all elements from the queue.

inline vector_t<type> &timed_out()

Get the vector of timed out items (and clear the internal buffer)

Returns:

Reference to the vector of timed out items

inline void clear_timed_out(const vector_t<type> &elements_, bool dumpVerbose_ = false, const std::string &prefix_ = "")

Clear timed out items. If elements are given, clear only those.

Parameters:
  • elements_ – Timed-out items that should be removed from the buffer.

  • dumpVerbose_ – Enables per-item timeout messages before removal.

  • prefix_ – Text prefix prepended to each timeout message.

inline void clear_timed_out(bool dumpVerbose_ = false, const std::string &prefix_ = "")

Clear all timed out items.

Parameters:
  • dumpVerbose_ – Enables per-item timeout messages before clearing.

  • prefix_ – Text prefix prepended to each timeout message.

inline void read_lock() const

Acquire a shared (read) lock for thread safety.

inline void lock() const

Acquire an exclusive (write) lock for thread safety.

inline void unlock_read() const

Release a shared (read) lock.

inline void unlock() const

Release an exclusive (write) lock.

Private Types

using base_t = std::list<pair_t<type, timer_t>>

Private Functions

inline void __timedout_message(const std::string &prefix_, const type &item_)

Print a message for a timed out item.

Parameters:
  • prefix_ – Text prefix prepended to the timeout message.

  • item_ – Item value that just timed out.

inline std::string __item_to_string(const type &item_) const

Convert an item to string for printing.

Parameters:

item_ – Item value converted to text for diagnostic output.

Returns:

String representation of the item

Private Members

duration_t timeOut

Timeout duration for elements in the queue.

mutable vector_t<type> timedOutItems

Store timed out items (mutable for const access in iterators)

mutable std::shared_mutex mutex

Mutex for thread safety (shared/exclusive)

struct const_iterator

Const iterator that yields only non-expired elements in the queue.

Public Types

using base_iter = typename base_t::const_iterator
using iterator_category = std::forward_iterator_tag
using value_type = type
using difference_type = typename std::iterator_traits<base_iter>::difference_type
using reference = const type&
using pointer = const type*

Public Functions

const_iterator() = default

Default constructor.

inline const_iterator(const timeout_queue_t *o_, base_iter i_, base_iter e_)

Construct a const_iterator.

Parameters:
  • o_ – Queue instance that owns this iterator.

  • i_ – Current list iterator position.

  • e_ – Sentinel iterator marking the end position.

inline const_iterator(const iterator &other_)

Allow converting iterator to const_iterator.

Parameters:

other_ – Mutable iterator whose state is copied.

inline reference operator*() const

Dereferences the iterator.

inline pointer operator->() const

Accesses the current element pointer.

inline const_iterator &operator++()

Pre-increment operator.

inline const_iterator operator++(int int_)

Post-increment operator.

Parameters:

int_ – Dummy integer used to select postfix increment syntax.

Returns:

Iterator snapshot before incrementing.

Public Members

const timeout_queue_t *owner = nullptr

Pointer to the owning queue.

base_iter it = {}
base_iter end = {}

Underlying iterators.

Private Functions

inline void __skip_expired()

Skip expired elements and collect them in timedOutItems.

Friends

inline friend bool operator==(const const_iterator &a_, const const_iterator &b_)

Equality comparison.

Parameters:
  • a_ – Left iterator in the comparison.

  • b_ – Right iterator in the comparison.

Returns:

true when both iterators point to the same list position.

inline friend bool operator!=(const const_iterator &a_, const const_iterator &b_)

Inequality comparison.

Parameters:
  • a_ – Left iterator in the comparison.

  • b_ – Right iterator in the comparison.

Returns:

true when iterators point to different list positions.

struct iterator

Filtered iterator (yields only non-expired type)

Iterator that yields only non-expired elements in the queue.

Public Types

using base_iter = typename base_t::iterator
using iterator_category = std::forward_iterator_tag
using value_type = type
using difference_type = typename std::iterator_traits<base_iter>::difference_type
using reference = type&
using pointer = type*

Public Functions

iterator() = default

Default constructor.

inline iterator(timeout_queue_t *o_, base_iter i_, base_iter e_)

Construct an iterator.

Parameters:
  • o_ – Queue instance that owns this iterator.

  • i_ – Current list iterator position.

  • e_ – Sentinel iterator marking the end position.

inline reference operator*() const

Dereferences the iterator.

inline pointer operator->() const

Accesses the current element pointer.

inline iterator &operator++()

Pre-increment operator.

inline iterator operator++(int int_)

Post-increment operator.

Parameters:

int_ – Dummy integer used to select postfix increment syntax.

Returns:

Iterator snapshot before incrementing.

Public Members

timeout_queue_t *owner = nullptr

Pointer to the owning queue.

base_iter it = {}
base_iter end = {}

Underlying iterators.

Private Functions

inline void __skip_expired()

Skip expired elements and collect them in timedOutItems.

Friends

inline friend bool operator==(const iterator &a_, const iterator &b_)

Equality comparison.

Parameters:
  • a_ – Left iterator in the comparison.

  • b_ – Right iterator in the comparison.

Returns:

true when both iterators point to the same list position.

inline friend bool operator!=(const iterator &a_, const iterator &b_)

Inequality comparison.

Parameters:
  • a_ – Left iterator in the comparison.

  • b_ – Right iterator in the comparison.

Returns:

true when iterators point to different list positions.