OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
IDManager.h
Go to the documentation of this file.
1/*
2 * IDManager.h
3 *
4 * Created on: 23/09/2022
5 * Author: Alexander Kuester
6 * Copyright (c) 2022, OpenTwin
7 */
8
9#pragma once
10
11// OpenTwin header
12#include "OTCore/OTAssert.h"
13
14// C++ header
15#include <map>
16
17namespace ot {
18
19 template<class T> class IDManager {
20 public:
22 virtual ~IDManager();
23
25 T grabNextID(void);
26
28 void freeID(T _id);
29
30 private:
31 T m_currentId;
32 std::map<T, bool> m_occupiedIDs;
33 };
34
35 template <class T> IDManager<T>::IDManager() : m_currentId(0) {}
36 template <class T> IDManager<T>::~IDManager() {}
37
38 template <class T> T IDManager<T>::grabNextID(void) {
39 T id = ++m_currentId;
40 bool end = false;
41 while (m_occupiedIDs.find(id) != m_occupiedIDs.end() || id == 0) {
42 if (id == 0) {
43 if (end) {
44 OTAssert(0, "There is no free ID available");
45 return 0;
46 }
47 else {
48 end = true;
49 }
50 }
51 id++;
52 }
53 return id;
54 }
55
56 template <class T> void IDManager<T>::freeID(T _id) { m_occupiedIDs.erase(_id); }
57}
#define OTAssert(___expression, ___message)
Adds the ability to provide a custom text to the cassert macro (assertion only in debug mode)
Definition OTAssert.h:16
Definition IDManager.h:19
T grabNextID(void)
Will find the next free id and return it.
Definition IDManager.h:38
virtual ~IDManager()
Definition IDManager.h:36
IDManager()
Definition IDManager.h:35
void freeID(T _id)
Will set the provided id as free.
Definition IDManager.h:56
Definition Connector.h:8