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/typedefs.h>
13 
14 namespace qx
15 {
16 
17 /**
18  @enum priority
19  @brief User may use the predefined values or the custom ones, for ex. "normal - 1",
20  this type is supposed to be compared relatively and not for equality
21  @note The base type of this enum is u8,
22  and lowest will always be the lowest value and highest will be the highest.
23  You should use std::greater<qx::priority> as a predicate in ordered containers to iterate them with decreasing priority.
24 **/
25 enum class priority : u8
26 {
27  lowest = 0,
28  very_low = 32,
29  low = 64,
30  normal = 128,
31  high = 160,
32  very_high = 224,
33  highest = 255,
34 };
35 
36 /**
37  @struct time_ordered_priority_key
38  @brief A structure that can be used as a key in ordered containers
39  so that items are ordered in descending order of priority but ascending order of creation time.
40 **/
42 {
43  constexpr time_ordered_priority_key() noexcept = default;
44  time_ordered_priority_key(priority ePriority) noexcept;
45  constexpr bool operator==(const time_ordered_priority_key&) const noexcept = default;
46  constexpr auto operator<(const time_ordered_priority_key& other) const noexcept;
47 
48  priority ePriority = priority::normal;
49  size_t nId = 0;
50 
51 private:
52  static inline std::atomic_size_t nIdCounter { 0 };
53 };
54 
55 inline time_ordered_priority_key::time_ordered_priority_key(priority ePriority) noexcept
56  : ePriority(ePriority)
57  , nId(++nIdCounter)
58 {
59 }
60 
61 constexpr auto time_ordered_priority_key::operator<(const time_ordered_priority_key& other) const noexcept
62 {
63  if (ePriority != other.ePriority)
64  return ePriority > other.ePriority;
65  else
66  return nId < other.nId;
67 }
68 
69 } // namespace qx
70 
71 constexpr auto operator<=>(qx::priority eLeft, qx::priority eRight)
72 {
73  return static_cast<u8>(eLeft) <=> static_cast<u8>(eRight);
74 }
priority
User may use the predefined values or the custom ones, for ex. "normal - 1", this type is supposed to...
Definition: priority.h:26
A structure that can be used as a key in ordered containers so that items are ordered in descending o...
Definition: priority.h:42
std::uint8_t u8
0 .. 65 535
Definition: typedefs.h:19