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  priority ePriority = priority::normal;
44  size_t nId = ++nIdCounter;
45 
46  constexpr bool operator==(const time_ordered_priority_key&) const noexcept = default;
47  constexpr auto operator<(const time_ordered_priority_key& other) const noexcept
48  {
49  if (ePriority != other.ePriority)
50  return ePriority > other.ePriority;
51  else
52  return nId < other.nId;
53  }
54 
55 private:
56  static inline std::atomic_size_t nIdCounter { 0 };
57 };
58 
59 } // namespace qx
60 
61 constexpr auto operator<=>(qx::priority eLeft, qx::priority eRight)
62 {
63  return static_cast<u8>(eLeft) <=> static_cast<u8>(eRight);
64 }
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