Sane C++ Libraries
C++ Platform Abstraction Libraries
StrongID.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/PrimitiveTypes.h"
5namespace SC
6{
7template <typename TagType, typename IDType = int32_t, IDType InvalidValue = -1>
8struct StrongID;
9} // namespace SC
12
17template <typename TagType, typename IDType, IDType InvalidValue>
19{
20 using NumericType = IDType;
21 IDType identifier;
22
23 constexpr StrongID() : identifier(InvalidValue) {}
24
25 explicit constexpr StrongID(IDType value) : identifier(value) {}
26
27 [[nodiscard]] constexpr bool operator==(StrongID other) const { return identifier == other.identifier; }
28
29 [[nodiscard]] constexpr bool operator!=(StrongID other) const { return identifier != other.identifier; }
30
32 [[nodiscard]] constexpr bool isValid() const { return identifier != InvalidValue; }
33
38 template <typename Container>
39 [[nodiscard]] constexpr static StrongID generateUniqueKey(const Container& container)
40 {
41 StrongID test = StrongID({});
42 while (container.contains(test))
43 {
44 ++test.identifier;
45 }
46 return test;
47 }
48};
49
int int32_t
Platform independent (4) bytes signed int.
Definition: PrimitiveTypes.h:46
Strongly typed ID (cannot be assigned incorrectly to another ID)
Definition: StrongID.h:19
constexpr static StrongID generateUniqueKey(const Container &container)
Generates an unique StrongID for a given container.
Definition: StrongID.h:39
constexpr bool isValid() const
Check if StrongID is valid.
Definition: StrongID.h:32