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