2
// Copyright 2019 The ANGLE Project Authors. All rights reserved.
3
// Use of this source code is governed by a BSD-style license that can be
4
// found in the LICENSE file.
7
// Utilities for generating unique IDs for resources in ANGLE.
10
#ifndef LIBANGLE_RENDERER_SERIAL_UTILS_H_
11
#define LIBANGLE_RENDERER_SERIAL_UTILS_H_
15
#include "common/angleutils.h"
16
#include "common/debug.h"
23
constexpr ResourceSerial() : mValue(kDirty) {}
24
explicit constexpr ResourceSerial(uintptr_t value) : mValue(value) {}
25
constexpr bool operator==(ResourceSerial other) const { return mValue == other.mValue; }
26
constexpr bool operator!=(ResourceSerial other) const { return mValue != other.mValue; }
28
void dirty() { mValue = kDirty; }
29
void clear() { mValue = kEmpty; }
31
constexpr bool valid() const { return mValue != kEmpty && mValue != kDirty; }
32
constexpr bool empty() const { return mValue == kEmpty; }
35
constexpr static uintptr_t kDirty = std::numeric_limits<uintptr_t>::max();
36
constexpr static uintptr_t kEmpty = 0;
44
constexpr Serial() : mValue(kInvalid) {}
45
constexpr Serial(const Serial &other) = default;
46
Serial &operator=(const Serial &other) = default;
48
constexpr bool operator==(const Serial &other) const
50
return mValue != kInvalid && mValue == other.mValue;
52
constexpr bool operator==(uint32_t value) const
54
return mValue != kInvalid && mValue == static_cast<uint64_t>(value);
56
constexpr bool operator!=(const Serial &other) const
58
return mValue == kInvalid || mValue != other.mValue;
60
constexpr bool operator>(const Serial &other) const { return mValue > other.mValue; }
61
constexpr bool operator>=(const Serial &other) const { return mValue >= other.mValue; }
62
constexpr bool operator<(const Serial &other) const { return mValue < other.mValue; }
63
constexpr bool operator<=(const Serial &other) const { return mValue <= other.mValue; }
65
constexpr bool operator<(uint32_t value) const { return mValue < static_cast<uint64_t>(value); }
67
// Useful for serialization.
68
constexpr uint64_t getValue() const { return mValue; }
72
friend class SerialFactoryBase;
73
constexpr explicit Serial(uint64_t value) : mValue(value) {}
75
static constexpr uint64_t kInvalid = 0;
78
template <typename SerialBaseType>
79
class SerialFactoryBase final : angle::NonCopyable
82
SerialFactoryBase() : mSerial(1) {}
86
ASSERT(mSerial + 1 > mSerial);
87
return Serial(mSerial++);
91
SerialBaseType mSerial;
94
using SerialFactory = SerialFactoryBase<uint64_t>;
95
using AtomicSerialFactory = SerialFactoryBase<std::atomic<uint64_t>>;
98
#endif // LIBANGLE_RENDERER_SERIAL_UTILS_H_