qxLib
priority.h
Go to the documentation of this file.
1 /**
2 
3  @file priority.h
4  @author Khrapov
5  @date 3.12.2023
6  @copyright © Nick Khrapov, 2023. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <qx/containers/flags.h>
12 #include <qx/hash.h>
13 #include <qx/typedefs.h>
14 
15 namespace qx
16 {
17 
18 /**
19  @enum priority
20  @brief User may use the predefined values or the custom ones, for ex. "normal - 1",
21  this type is supposed to be compared relatively and not for equality
22  @note The base type of this enum is u8,
23  and lowest will always be the lowest value and highest will be the highest.
24  You should use std::greater<qx::priority> as a predicate in ordered containers to iterate them with decreasing priority.
25 **/
26 enum class priority : u8
27 {
28  lowest = 0,
29  very_low = 32,
30  low = 64,
31  normal = 128,
32  high = 160,
33  very_high = 224,
34  highest = 255,
35 };
36 
37 /**
38 
39  @class time_ordered_priority_key
40  @brief A class that can be used as a key in ordered containers
41  so that items are ordered in descending order of priority but ascending order of creation time.
42  @author Khrapov
43  @date 10.08.2025
44 
45 **/
47 {
48  friend std::hash<time_ordered_priority_key>;
49 
50 public:
51  constexpr time_ordered_priority_key() noexcept = default;
52 
53  /**
54  @brief time_ordered_priority_key object constructor
55  @param ePriority - key priority
56  **/
57  time_ordered_priority_key(priority ePriority) noexcept;
58 
59  /**
60  @brief Get key priority
61  @retval - key priority
62  **/
63  priority get_priority() const noexcept;
64 
65  /**
66  @brief Set key priority
67  @param ePriority - key priority
68  **/
69  constexpr void set_priority(priority ePriority) noexcept;
70 
71  constexpr bool operator==(const time_ordered_priority_key&) const noexcept = default;
72  constexpr auto operator<(const time_ordered_priority_key& other) const noexcept;
73 
74 private:
75  static inline std::atomic_size_t m_nIdCounter { 0 };
76  priority m_ePriority = priority::normal;
77  size_t m_nId = 0;
78 };
79 
80 inline time_ordered_priority_key::time_ordered_priority_key(priority ePriority) noexcept
81  : m_ePriority(ePriority)
82  , m_nId(++m_nIdCounter)
83 {
84 }
85 
87 {
88  return m_ePriority;
89 }
90 
91 constexpr void time_ordered_priority_key::set_priority(priority ePriority) noexcept
92 {
93  m_ePriority = ePriority;
94 }
95 
96 constexpr auto time_ordered_priority_key::operator<(const time_ordered_priority_key& other) const noexcept
97 {
98  if (m_ePriority != other.m_ePriority)
99  return m_ePriority > other.m_ePriority;
100  else
101  return m_nId < other.m_nId;
102 }
103 
104 } // namespace qx
105 
106 constexpr auto operator<=>(qx::priority eLeft, qx::priority eRight)
107 {
108  return static_cast<u8>(eLeft) <=> static_cast<u8>(eRight);
109 }
110 
111 template<>
112 struct std::hash<qx::time_ordered_priority_key>
113 {
114  constexpr size_t operator()(const qx::time_ordered_priority_key& timeOrderedPriorityKey) const noexcept
115  {
116  size_t nHash = 0;
117  qx::hash_combine(nHash, timeOrderedPriorityKey.m_nId);
118  qx::hash_combine(nHash, timeOrderedPriorityKey.m_ePriority);
119  return nHash;
120  }
121 };
A class that can be used as a key in ordered containers so that items are ordered in descending order...
Definition: priority.h:47
priority get_priority() const noexcept
Get key priority.
Definition: priority.h:86
constexpr void set_priority(priority ePriority) noexcept
Set key priority.
Definition: priority.h:91
priority
User may use the predefined values or the custom ones, for ex. "normal - 1", this type is supposed to...
Definition: priority.h:27
std::uint8_t u8
0 .. 65 535
Definition: typedefs.h:19