commons 0.1.5
Header-only C++23 library of common/shared types for the C++ libraries
Loading...
Searching...
No Matches
prioritized.hpp File Reference

Priorities for orderable things (adapters, transports, …) plus the helpers to sort them deterministically — the C++ analog of Spring's Ordered interface. More...

#include <commons/types.hpp>
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <functional>
#include <initializer_list>
#include <iterator>
#include <limits>
#include <memory>
#include <set>
#include <type_traits>
#include <utility>
Include dependency graph for prioritized.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  comms::Prioritized
 The orderable interface. More...
 
struct  comms::PrioritizedCompare
 Orders std::shared_ptr<Prioritized> by ascending priority. More...
 
struct  comms::LenientPrioritizedCompare< T >
 Like PrioritizedCompare, but over std::shared_ptr<T> for an arbitrary T that need not derive from Prioritized: the priority is resolved through get_priority (which handles Prioritizable types and a dynamic_cast probe). More...
 
class  comms::PrioritizedSet< T >
 A set that behaves like std::set<T> from the outside — unique by T value, iterators yield const T&, every method is expressed in terms of T — but whose iteration order is (priority asc, insertion-order asc) rather than by T value. More...
 
class  comms::PrioritizedSet< T >::const_iterator
 A thin bidirectional adapter over std::set<Item>::const_iterator that projects each Item to its const T& value — the private Item is never observable, so iteration looks exactly like a std::set<T>'s. More...
 
class  comms::PrioritizedBuilder< Derived >
 CRTP mixin that makes Derived a Prioritized carrying a mutable priority, with fluent setters returning Derived& for chaining. More...
 
class  comms::WithPriority< T >
 A value of T carrying a priority. More...
 

Concepts

concept  comms::Prioritizable
 Any type exposing a priority() convertible to int — satisfied by types deriving from Prioritized and by any standalone type with the method.
 
concept  comms::detail::PointerReturningGet
 A type whose .get() yields something pointer-like (a smart pointer).
 

Macros

#define COMMONS_PRIORITIZED_HIGHEST_PRECEDENCE   (std::numeric_limits<int>::min())
 Override for comms::Prioritized::HIGHEST_PRECEDENCE (default INT_MIN).
 
#define COMMONS_PRIORITIZED_LOWEST_PRECEDENCE   (std::numeric_limits<int>::max())
 Override for comms::Prioritized::LOWEST_PRECEDENCE (default INT_MAX).
 
#define COMMONS_PRIORITIZED_DEFAULT_PRIORITY   0
 Override for comms::Prioritized::DEFAULT_PRIORITY (default 0).
 

Functions

template<typename T >
int comms::detail::get_priority_ptr (const T *ptr) noexcept
 Priority of a pointee.
 
template<typename T >
requires (!std::is_pointer_v<T>)
int comms::get_priority (const T &value) noexcept
 Priority of a value or reference.
 
template<typename T >
int comms::get_priority (const T *ptr) noexcept
 Priority behind a raw pointer (null-safe).
 
template<typename T >
WithPriority< std::remove_cvref_t< T > > comms::with_priority (int p, T &&item)
 Wrap item with priority p.
 
template<typename T , typename... Args>
std::shared_ptr< WithPriority< T > > comms::make_prioritized (int p, Args &&... args)
 Make a shared_ptr<WithPriority<T>> (for use with the comparators / a std::set).
 

Variables

template<typename T >
constexpr bool comms::with_priority_inherits = std::is_class_v<T> && !std::is_final_v<T>
 Whether WithPriority<T> attaches its priority by inheritance (a true is-a T): only for non-final class types.
 

Detailed Description

Priorities for orderable things (adapters, transports, …) plus the helpers to sort them deterministically — the C++ analog of Spring's Ordered interface.

Lower priority value sorts first (higher precedence), mirroring Spring: HIGHEST_PRECEDENCE is INT_MIN, LOWEST_PRECEDENCE is INT_MAX, and the neutral DEFAULT_PRIORITY is 0. The three sentinels are static constexpr members of comms::Prioritized; a build system or consumer may override them by predefining the matching COMMONS_PRIORITIZED_* macro (see below).

What this header provides:

  • comms::Prioritized — a virtual-with-default interface. A bare struct Adapter : comms::Prioritized {}; already works at DEFAULT_PRIORITY; override priority() to change it.
  • comms::Prioritizable<T> — concept for any type exposing .priority().
  • comms::get_priority(x) — uniform priority lookup over values, references, raw pointers and smart pointers (null-safe), falling back to DEFAULT_PRIORITY when no priority is discoverable.
  • comms::PrioritizedCompare / comms::LenientPrioritizedCompare<T> — strict-weak comparators over std::shared_ptr (lower value first, with a stable pointer tie-break).
  • comms::PrioritizedSet<T> — a transparent std::set<T> (unique by T value) whose iteration order is (priority asc, insertion-order asc).
  • comms::PrioritizedBuilder<Derived> — a CRTP mixin adding fluent priority() / highest_priority() / lowest_priority() setters.
  • comms::WithPriority<T> + with_priority / make_prioritized — attach a priority to an existing value, by inheritance for non-final classes (a true is-a T) or by composition otherwise.

Serialization (in commons/json.hpp, gated by COMMONS_WITH_NLOHMANN_JSON): WithPriority<T> travels as {"priority":N,"value":<T>} and a PrioritizedSet<T> as a JSON array in sorted order, both only when T is itself json-serializable. The interface and mixins carry no JSON hooks.

Function Documentation

◆ get_priority() [1/2]

template<typename T >
requires (!std::is_pointer_v<T>)
int comms::get_priority ( const T &  value)
inlinenoexcept

Priority of a value or reference.

The Prioritized/Prioritizable branch is checked first so a Prioritizable type that also exposes .get() is not misrouted into the smart-pointer branch. Smart pointers forward to their pointee; anything else reports DEFAULT_PRIORITY. Constrained off raw pointers so those select the more specific overload below (template partial ordering alone does not reliably prefer it).

◆ get_priority() [2/2]

template<typename T >
int comms::get_priority ( const T *  ptr)
inlinenoexcept

Priority behind a raw pointer (null-safe).

This overload is more specialized than the value/reference one, so a raw pointer argument selects it.

◆ get_priority_ptr()

template<typename T >
int comms::detail::get_priority_ptr ( const T *  ptr)
inlineprotectednoexcept

Priority of a pointee.

nullptr → default; a Prioritizable pointee answers directly; an unrelated polymorphic pointee is probed via dynamic_cast (the is_polymorphic_v guard keeps the cast well-formed for non-polymorphic T); otherwise the default. noexcept: a pointer dynamic_cast cannot throw.

◆ make_prioritized()

template<typename T , typename... Args>
std::shared_ptr< WithPriority< T > > comms::make_prioritized ( int  p,
Args &&...  args 
)

Make a shared_ptr<WithPriority<T>> (for use with the comparators / a std::set).

The first argument is the priority; the rest construct the T.

◆ with_priority()

template<typename T >
WithPriority< std::remove_cvref_t< T > > comms::with_priority ( int  p,
T &&  item 
)

Wrap item with priority p.

The wrapper's T is std::remove_cvref_t<T>, chosen by with_priority_inherits between the inheritance and composition flavors.

Variable Documentation

◆ with_priority_inherits

template<typename T >
constexpr bool comms::with_priority_inherits = std::is_class_v<T> && !std::is_final_v<T>
inlineconstexpr

Whether WithPriority<T> attaches its priority by inheritance (a true is-a T): only for non-final class types.

Final classes and fundamentals fall back to composition.