Sane C++ Libraries
C++ Platform Abstraction Libraries
ArenaMapKey.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/PrimitiveTypes.h"
5
6namespace SC
7{
8template <typename T>
9struct ArenaMap;
10template <typename T>
11struct ArenaMapKey;
12} // namespace SC
13
16
20template <typename T>
22{
23 private:
24 struct SC_COMPILER_EXPORT Generation
25 {
26 uint32_t used : 1;
27 uint32_t generation : 31;
28 Generation()
29 {
30 used = 0;
31 generation = 0;
32 }
33 bool operator==(const Generation other) const { return used == other.used and generation == other.generation; }
34 bool operator!=(const Generation other) const { return used != other.used or generation != other.generation; }
35 };
36 Generation generation;
37 uint32_t index;
38 friend struct ArenaMap<T>;
39 template <typename U>
40 friend struct ArenaMapKey;
41
42 public:
43 ArenaMapKey() { index = 0; }
44
45 bool isValid() const { return generation.used != 0; }
46
47 template <typename U>
48 ArenaMapKey<U> cast_to()
49 {
51 key.generation.used = generation.used;
52 key.generation.generation = generation.generation;
53 key.index = index;
54 return key;
55 }
56
57 template <typename U>
58 bool operator==(ArenaMapKey<U> other) const
59 {
60 return index == other.index and generation.used == other.generation.used and
61 generation.generation == other.generation.generation;
62 }
63
64 static constexpr uint32_t MaxGenerations = (uint32_t(1) << 31) - 1;
65 static constexpr uint32_t MaxIndex = 0xffffffff;
66};
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition: Compiler.h:78
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition: PrimitiveTypes.h:38
A sparse vector keeping objects at a stable memory location.
Definition: ArenaMap.h:27
A sparse vector keeping objects at a stable memory location.
Definition: ArenaMapKey.h:22