Serenity Operating System
1/*
2 * Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/Assertions.h>
10#include <AK/NumericLimits.h>
11#include <AK/Types.h>
12
13namespace Profiler {
14
15// DistinctNumeric's constructor is non-explicit which makes accidentally turning
16// an unrelated u64 into a serial number all too easy.
17class EventSerialNumber {
18public:
19 constexpr EventSerialNumber() = default;
20
21 void increment()
22 {
23 m_serial++;
24 }
25
26 size_t to_number() const
27 {
28 return m_serial;
29 }
30
31 static EventSerialNumber max_valid_serial()
32 {
33 return EventSerialNumber { NumericLimits<size_t>::max() };
34 }
35
36 bool operator==(EventSerialNumber const& rhs) const
37 {
38 return m_serial == rhs.m_serial;
39 }
40
41 bool operator<(EventSerialNumber const& rhs) const
42 {
43 return m_serial < rhs.m_serial;
44 }
45
46 bool operator>(EventSerialNumber const& rhs) const
47 {
48 return m_serial > rhs.m_serial;
49 }
50
51 bool operator<=(EventSerialNumber const& rhs) const
52 {
53 return m_serial <= rhs.m_serial;
54 }
55
56 bool operator>=(EventSerialNumber const& rhs) const
57 {
58 return m_serial >= rhs.m_serial;
59 }
60
61private:
62 size_t m_serial { 0 };
63
64 constexpr explicit EventSerialNumber(size_t serial)
65 : m_serial(serial)
66 {
67 }
68};
69
70}